gb_sets (stdlib v3.15.2)

This module provides ordered sets using Prof. Arne Andersson's General Balanced Trees. Ordered sets can be much more efficient than using ordered lists, for larger sets, but depends on the application.

This module considers two elements as different if and only if they do not compare equal (==).

Complexity Note

The complexity on set operations is bounded by either O(|S|) or O(|T| * log(|S|)), where S is the largest given set, depending on which is fastest for any particular function call. For operating on sets of almost equal size, this implementation is about 3 times slower than using ordered-list sets directly. For sets of very different sizes, however, this solution can be arbitrarily much faster; in practical cases, often 10-100 times. This implementation is particularly suited for accumulating elements a few at a time, building up a large set (> 100-200 elements), and repeatedly testing for membership in the current set.

As with normal tree structures, lookup (membership testing), insertion, and deletion have logarithmic complexity.

Compatibility

The following functions in this module also exist and provides the same functionality in the sets(3) and ordsets(3) modules. That is, by only changing the module name for each call, you can try out different set representations.

See Also

gb_trees(3), ordsets(3), sets(3)

Link to this section Summary

Functions

Returns a new set formed from Set1 with Element inserted. If Element is already an element in Set1, nothing is changed.

Rebalances the tree representation of Set1. Notice that this is rarely necessary, but can be motivated when a large number of elements have been deleted from the tree without further insertions. Rebalancing can then be forced to minimise lookup times, as deletion does not rebalance the tree.

Returns a new set formed from Set1 with Element removed. If Element is not an element in Set1, nothing is changed.

Returns a new set formed from Set1 with Element removed. Assumes that Element is present in Set1.

Returns a new set formed from Set1 with Element removed. If Element is not an element in Set1, nothing is changed.

Returns only the elements of Set1 that are not also elements of Set2.

Returns a new empty set.

Filters elements in Set1 using predicate function Pred.

Folds Function over every element in Set returning the final value of the accumulator.

Returns a set of the elements in List, where List can be unordered and contain duplicates.

Turns an ordered-set list List into a set. The list must not contain duplicates.

Returns a new set formed from Set1 with Element inserted. Assumes that Element is not present in Set1.

Returns the intersection of the non-empty list of sets.

Returns the intersection of Set1 and Set2.

Returns true if Set1 and Set2 are disjoint (have no elements in common), otherwise false.

Returns true if Element is an element of Set, otherwise false.

Returns true if Set is an empty set, otherwise false.

Returns true if Element is an element of Set, otherwise false.

Returns true if Term appears to be a set, otherwise false.

Returns true when every element of Set1 is also a member of Set2, otherwise false.

Returns an iterator that can be used for traversing the entries of Set; see next/1. The implementation of this is very efficient; traversing the whole set using next/1 is only slightly slower than getting the list of all elements using to_list/1 and traversing that. The main advantage of the iterator approach is that it does not require the complete list of all elements to be built in memory at one time.

Returns an iterator that can be used for traversing the entries of Set; see next/1. The difference as compared to the iterator returned by iterator/1 is that the first element greater than or equal to Element is returned.

Returns the largest element in Set. Assumes that Set is not empty.

Returns a new empty set.

Returns {Element, Iter2}, where Element is the smallest element referred to by iterator Iter1, and Iter2 is the new iterator to be used for traversing the remaining elements, or the atom none if no elements remain.

Returns a set containing only element Element.

Returns the number of elements in Set.

Returns the smallest element in Set. Assumes that Set is not empty.

Returns only the elements of Set1 that are not also elements of Set2.

Returns {Element, Set2}, where Element is the largest element in Set1, and Set2 is this set with Element deleted. Assumes that Set1 is not empty.

Returns {Element, Set2}, where Element is the smallest element in Set1, and Set2 is this set with Element deleted. Assumes that Set1 is not empty.

Returns the elements of Set as a list.

Returns the merged (union) set of the list of sets.

Returns the merged (union) set of Set1 and Set2.

Link to this section Types

Link to this type

-type iter() :: term().

Specs

iter() :: iter(_).
Link to this opaque

-type iter(Arg1) :: term().

(opaque)

Specs

iter(Element)

A general balanced set iterator.

Link to this type

-type set() :: term().

Specs

set() :: set(_).
Link to this opaque

-type set(Arg1) :: term().

(opaque)

Specs

set(Element)

A general balanced set.

Link to this section Functions

Specs

add(Element, Set1) -> Set2 when Set1 :: set(Element), Set2 :: set(Element).

Returns a new set formed from Set1 with Element inserted. If Element is already an element in Set1, nothing is changed.

Specs

add_element(Element, Set1) -> Set2 when Set1 :: set(Element), Set2 :: set(Element).

Specs

balance(Set1) -> Set2 when Set1 :: set(Element), Set2 :: set(Element).

Rebalances the tree representation of Set1. Notice that this is rarely necessary, but can be motivated when a large number of elements have been deleted from the tree without further insertions. Rebalancing can then be forced to minimise lookup times, as deletion does not rebalance the tree.

Specs

del_element(Element, Set1) -> Set2 when Set1 :: set(Element), Set2 :: set(Element).

Returns a new set formed from Set1 with Element removed. If Element is not an element in Set1, nothing is changed.

Specs

delete(Element, Set1) -> Set2 when Set1 :: set(Element), Set2 :: set(Element).

Returns a new set formed from Set1 with Element removed. Assumes that Element is present in Set1.

Specs

delete_any(Element, Set1) -> Set2 when Set1 :: set(Element), Set2 :: set(Element).

Returns a new set formed from Set1 with Element removed. If Element is not an element in Set1, nothing is changed.

Specs

difference(Set1, Set2) -> Set3
              when Set1 :: set(Element), Set2 :: set(Element), Set3 :: set(Element).

Returns only the elements of Set1 that are not also elements of Set2.

Specs

empty() -> Set when Set :: set().

Returns a new empty set.

Specs

filter(Pred, Set1) -> Set2
          when Pred :: fun((Element) -> boolean()), Set1 :: set(Element), Set2 :: set(Element).

Filters elements in Set1 using predicate function Pred.

Specs

fold(Function, Acc0, Set) -> Acc1
        when
            Function :: fun((Element, AccIn) -> AccOut),
            Acc0 :: Acc,
            Acc1 :: Acc,
            AccIn :: Acc,
            AccOut :: Acc,
            Set :: set(Element).

Folds Function over every element in Set returning the final value of the accumulator.

Specs

from_list(List) -> Set when List :: [Element], Set :: set(Element).

Returns a set of the elements in List, where List can be unordered and contain duplicates.

Specs

from_ordset(List) -> Set when List :: [Element], Set :: set(Element).

Turns an ordered-set list List into a set. The list must not contain duplicates.

Specs

insert(Element, Set1) -> Set2 when Set1 :: set(Element), Set2 :: set(Element).

Returns a new set formed from Set1 with Element inserted. Assumes that Element is not present in Set1.

Specs

intersection(SetList) -> Set when SetList :: [set(Element), ...], Set :: set(Element).

Returns the intersection of the non-empty list of sets.

Specs

intersection(Set1, Set2) -> Set3
                when Set1 :: set(Element), Set2 :: set(Element), Set3 :: set(Element).

Returns the intersection of Set1 and Set2.

Specs

is_disjoint(Set1, Set2) -> boolean() when Set1 :: set(Element), Set2 :: set(Element).

Returns true if Set1 and Set2 are disjoint (have no elements in common), otherwise false.

Specs

is_element(Element, Set) -> boolean() when Set :: set(Element).

Returns true if Element is an element of Set, otherwise false.

Specs

is_empty(Set) -> boolean() when Set :: set().

Returns true if Set is an empty set, otherwise false.

Specs

is_member(Element, Set) -> boolean() when Set :: set(Element).

Returns true if Element is an element of Set, otherwise false.

Specs

is_set(Term) -> boolean() when Term :: term().

Returns true if Term appears to be a set, otherwise false.

Specs

is_subset(Set1, Set2) -> boolean() when Set1 :: set(Element), Set2 :: set(Element).

Returns true when every element of Set1 is also a member of Set2, otherwise false.

Specs

iterator(Set) -> Iter when Set :: set(Element), Iter :: iter(Element).

Returns an iterator that can be used for traversing the entries of Set; see next/1. The implementation of this is very efficient; traversing the whole set using next/1 is only slightly slower than getting the list of all elements using to_list/1 and traversing that. The main advantage of the iterator approach is that it does not require the complete list of all elements to be built in memory at one time.

Link to this function

iterator_from/2

(since OTP 18.0)

Specs

iterator_from(Element, Set) -> Iter when Set :: set(Element), Iter :: iter(Element).

Returns an iterator that can be used for traversing the entries of Set; see next/1. The difference as compared to the iterator returned by iterator/1 is that the first element greater than or equal to Element is returned.

Specs

largest(Set) -> Element when Set :: set(Element).

Returns the largest element in Set. Assumes that Set is not empty.

Specs

new() -> Set when Set :: set().

Returns a new empty set.

Specs

next(Iter1) -> {Element, Iter2} | none when Iter1 :: iter(Element), Iter2 :: iter(Element).

Returns {Element, Iter2}, where Element is the smallest element referred to by iterator Iter1, and Iter2 is the new iterator to be used for traversing the remaining elements, or the atom none if no elements remain.

Specs

singleton(Element) -> set(Element).

Returns a set containing only element Element.

Specs

size(Set) -> non_neg_integer() when Set :: set().

Returns the number of elements in Set.

Specs

smallest(Set) -> Element when Set :: set(Element).

Returns the smallest element in Set. Assumes that Set is not empty.

Specs

subtract(Set1, Set2) -> Set3 when Set1 :: set(Element), Set2 :: set(Element), Set3 :: set(Element).

Returns only the elements of Set1 that are not also elements of Set2.

Specs

take_largest(Set1) -> {Element, Set2} when Set1 :: set(Element), Set2 :: set(Element).

Returns {Element, Set2}, where Element is the largest element in Set1, and Set2 is this set with Element deleted. Assumes that Set1 is not empty.

Link to this function

take_smallest/1

Specs

take_smallest(Set1) -> {Element, Set2} when Set1 :: set(Element), Set2 :: set(Element).

Returns {Element, Set2}, where Element is the smallest element in Set1, and Set2 is this set with Element deleted. Assumes that Set1 is not empty.

Specs

to_list(Set) -> List when Set :: set(Element), List :: [Element].

Returns the elements of Set as a list.

Specs

union(SetList) -> Set when SetList :: [set(Element), ...], Set :: set(Element).

Returns the merged (union) set of the list of sets.

Specs

union(Set1, Set2) -> Set3 when Set1 :: set(Element), Set2 :: set(Element), Set3 :: set(Element).

Returns the merged (union) set of Set1 and Set2.