Search documentation of stdlib

# View Source gb_sets (stdlib v6.1.2)

Sets represented by general balanced trees.

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.

The data representing a set as used by this module is to be regarded as opaque by other modules. In abstract terms, the representation is a composite type of existing Erlang terms. See note on data types. Any code assuming knowledge of the format is running on thin ice.

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

See the Compatibility Section in the `sets` module for information about the compatibility of the different implementations of sets in the Standard Library.

## See Also

`gb_trees`, `ordsets`, `sets`

# Summary

## Types

iter()
iter(Element)

A general balanced set iterator.

set()
set(Element)

A general balanced set.

## Functions

add(Element, Set1)

Equivalent to `add_element(Element, Set1)`.

add_element(Element, Set1)

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

balance(Set1)

Rebalances the tree representation of `Set1`.

del_element(Element, Set1)

Equivalent to `delete_any(Element, Set1)`.

delete(Element, Set1)

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

delete_any(Element, Set1)

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

difference(Set1, Set2)

Equivalent to `subtract(Set1, Set2)`.

empty()

Returns a new empty set.

filter(Pred, Set1)

Filters elements in `Set1` using predicate function `Pred`.

filtermap(Fun, Set1)

Filters and maps elements in `Set1` using function `Fun`.

fold(Function, Acc0, Set)

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

from_list(List)

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

from_ordset(List)

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

insert(Element, Set1)

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

intersection(SetList)

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

intersection(Set1, Set2)

Returns the intersection of `Set1` and `Set2`.

is_disjoint(Set1, Set2)

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

is_element(Element, Set)

Equivalent to `is_member(Element, Set)`.

is_empty(Set)

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

is_equal(Set1, Set2)

Returns `true` if `Set1` and `Set2` are equal, that is when every element of one set is also a member of the respective other set, otherwise `false`.

is_member(Element, Set)

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

is_set(Term)

Returns `true` if `Term` appears to be a set, otherwise `false`. This function will return `true` for any term that coincides with the representation of a `gb_set`, while not really being a `gb_set`, thus it might return false positive results. See also note on data types.

is_subset(Set1, Set2)

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

iterator(Set)

Returns an iterator that can be used for traversing the entries of `Set`; see `next/1`.

iterator(Set, Order)

Returns an iterator that can be used for traversing the entries of `Set` in either `ordered` or `reversed` direction; see `next/1`.

iterator_from(Element, Set)

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 iterator starts with the first element greater than or equal to `Element`.

iterator_from(Element, Set, Order)

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/2` is that the iterator starts with the first element next to or equal to `Element`.

larger(Element1, Set)

Returns `{found, Element2}`, where `Element2` is the least element strictly greater than `Element1`.

largest(Set)

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

map(Fun, Set1)

Maps elements in `Set1` using mapping function `Fun`.

new()

Returns a new empty set.

next(Iter1)

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.

singleton(Element)

Returns a set containing only element `Element`.

size(Set)

Returns the number of elements in `Set`.

smaller(Element1, Set)

Returns `{found, Element2}`, where `Element2` is the greatest element strictly less than `Element1`.

smallest(Set)

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

subtract(Set1, Set2)

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

take_largest(Set1)

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.

take_smallest(Set1)

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.

to_list(Set)

Returns the elements of `Set` as a list.

union(SetList)

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

union(Set1, Set2)

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

# Types

Link to this type

# iter()

View Source

    -type iter() :: iter(_).

Link to this opaque

# iter(Element)

View Source

    -opaque iter(Element)

A general balanced set iterator.

Link to this type

# set()

View Source

    -type set() :: set(_).

Link to this opaque

# set(Element)

View Source

    -opaque set(Element)

A general balanced set.

# Functions

Link to this function

# add(Element, Set1)

View Source

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

Equivalent to `add_element(Element, Set1)`.

Link to this function

# add_element(Element, Set1)

View Source

    -spec add_element(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.

Link to this function

# balance(Set1)

View Source

    -spec 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.

Link to this function

# del_element(Element, Set1)

View Source

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

Equivalent to `delete_any(Element, Set1)`.

Link to this function

# delete(Element, Set1)

View Source

    -spec 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`.

Link to this function

# delete_any(Element, Set1)

View Source

    -spec 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.

Link to this function

# difference(Set1, Set2)

View Source

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

Equivalent to `subtract(Set1, Set2)`.

Link to this function

# empty()

View Source

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

Returns a new empty set.

Link to this function

# filter(Pred, Set1)

View Source

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

Filters elements in `Set1` using predicate function `Pred`.

Link to this function

# filtermap(Fun, Set1)

View Source (since OTP 27.0)

    -spec filtermap(Fun, Set1) -> Set2
                       when
                           Fun :: fun((Element1) -> boolean() | {true, Element2}),
                           Set1 :: set(Element1),
                           Set2 :: set(Element1 | Element2).

Filters and maps elements in `Set1` using function `Fun`.

Link to this function

# fold(Function, Acc0, Set)

View Source

    -spec 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.

Link to this function

# from_list(List)

View Source

    -spec 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.

Link to this function

# from_ordset(List)

View Source

    -spec 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.

Link to this function

# insert(Element, Set1)

View Source

    -spec 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`.

Link to this function

# intersection(SetList)

View Source

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

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

Link to this function

# intersection(Set1, Set2)

View Source

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

Returns the intersection of `Set1` and `Set2`.

Link to this function

# is_disjoint(Set1, Set2)

View Source

    -spec 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`.

Link to this function

# is_element(Element, Set)

View Source

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

Equivalent to `is_member(Element, Set)`.

Link to this function

# is_empty(Set)

View Source

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

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

Link to this function

# is_equal(Set1, Set2)

View Source (since OTP 27.0)

    -spec is_equal(Set1, Set2) -> boolean() when Set1 :: set(), Set2 :: set().

Returns `true` if `Set1` and `Set2` are equal, that is when every element of one set is also a member of the respective other set, otherwise `false`.

Link to this function

# is_member(Element, Set)

View Source

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

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

Link to this function

# is_set(Term)

View Source

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

Returns `true` if `Term` appears to be a set, otherwise `false`. This function will return `true` for any term that coincides with the representation of a `gb_set`, while not really being a `gb_set`, thus it might return false positive results. See also note on data types.

Link to this function

# is_subset(Set1, Set2)

View Source

    -spec 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`.

Link to this function

# iterator(Set)

View Source

    -spec 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`.

Equivalent to `iterator(Set, ordered)`.

Link to this function

# iterator(Set, Order)

View Source (since OTP 27.0)

    -spec iterator(Set, Order) -> Iter
                      when Set :: set(Element), Iter :: iter(Element), Order :: ordered | reversed.

Returns an iterator that can be used for traversing the entries of `Set` in either `ordered` or `reversed` direction; 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(Element, Set)

View Source (since OTP 18.0)

    -spec 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 iterator starts with the first element greater than or equal to `Element`.

Equivalent to `iterator_from(Element, Set, ordered)`.

Link to this function

# iterator_from(Element, Set, Order)

View Source (since OTP 27.0)

    -spec iterator_from(Element, Set, Order) -> Iter
                           when Set :: set(Element), Iter :: iter(Element), Order :: ordered | reversed.

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/2` is that the iterator starts with the first element next to or equal to `Element`.

Link to this function

# larger(Element1, Set)

View Source (since OTP 27.0)

    -spec larger(Element1, Set) -> none | {found, Element2}
                    when Element1 :: Element, Element2 :: Element, Set :: set(Element).

Returns `{found, Element2}`, where `Element2` is the least element strictly greater than `Element1`.

Returns `none` if no such element exists.

Link to this function

# largest(Set)

View Source

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

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

Link to this function

# map(Fun, Set1)

View Source (since OTP 27.0)

    -spec map(Fun, Set1) -> Set2
                 when Fun :: fun((Element1) -> Element2), Set1 :: set(Element1), Set2 :: set(Element2).

Maps elements in `Set1` using mapping function `Fun`.

Link to this function

# new()

View Source

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

Returns a new empty set.

Link to this function

# next(Iter1)

View Source

    -spec 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.

Link to this function

# singleton(Element)

View Source

    -spec singleton(Element) -> set(Element).

Returns a set containing only element `Element`.

Link to this function

# size(Set)

View Source

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

Returns the number of elements in `Set`.

Link to this function

# smaller(Element1, Set)

View Source (since OTP 27.0)

    -spec smaller(Element1, Set) -> none | {found, Element2}
                     when Element1 :: Element, Element2 :: Element, Set :: set(Element).

Returns `{found, Element2}`, where `Element2` is the greatest element strictly less than `Element1`.

Returns `none` if no such element exists.

Link to this function

# smallest(Set)

View Source

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

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

Link to this function

# subtract(Set1, Set2)

View Source

    -spec 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`.

Link to this function

# take_largest(Set1)

View Source

    -spec 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(Set1)

View Source

    -spec 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.

Link to this function

# to_list(Set)

View Source

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

Returns the elements of `Set` as a list.

Link to this function

# union(SetList)

View Source

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

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

Link to this function

# union(Set1, Set2)

View Source

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

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