gb_sets(3)

MODULE
gb_sets - General Balanced Trees

DESCRIPTION

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

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 between 10 and 100 times. This implementation is particularly suited for accumulating elements a few at a time, building up a large set (more than 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

All of the following functions in this module also exist and do the same thing in the sets and ordsets modules. That is, by only changing the module name for each call, you can try out different set representations.

    *   add_element/2

    *   del_element/2

    *   fold/3

    *   from_list/1

    *   intersection/1

    *   intersection/2

    *   is_element/2

    *   is_set/1

    *   is_subset/2

    *   new/0

    *   size/1

    *   subtract/2

    *   to_list/1

    *   union/1

    *   union/2

EXPORTS

empty()

    Returns a new, empty set.

new()

    Returns a new, empty set.

is_empty(S)

    Returns 'true' if S is an empty set, and 'false' otherwise.

size(S)

    Returns the number of nodes in the set as an integer. Returns 0 (zero) if the set is empty.

singleton(X)

    Returns a set containing only the element X.

is_member(X, S)

    Returns true if element X is a member of set S, and false otherwise.

is_element(X, S)

    Returns true if element X is a member of set S, and false otherwise.

insert(X, S)

    Inserts element X into set S, returns the new set. Assumes that the element is not present in S.

add(X, S)

    Adds element X to set S, returns the new set. If X is already an element in S, nothing is changed.

add_element(X, S)

    Adds element X to set S, returns the new set. If X is already an element in S, nothing is changed.

delete(X, S)

    Removes element X from set S, returns new set. Assumes that the element exists in the set.

delete_any(X, T)

    Removes key X from set S if the key is present in the set, otherwise does nothing; returns new set.

del_element(X, T)

    Removes key X from set S if the key is present in the set, otherwise does nothing; returns new set.

balance(S)

    Rebalances the tree representation of S. Note that this is rarely necessary, but may be motivated when a large number of elements have been deleted from the tree without further insertions. Rebalancing could then be forced in order to minimise lookup times, since deletion only does not rebalance the tree.

union(S1, S2)

    Returns a new set that contains each element that is in either S1 or S2 or both, and no other elements.

union(Ss)

    Returns a new set that contains each element that is in at least one of the sets in the list Ss, and no other elements.

intersection(S1, S2)

    Returns a new set that contains each element that is in both S1 and S2, and no other elements.

intersection(Ss)

    Returns a new set that contains each element that is in all of the sets in the list Ss, and no other elements.

difference(S1, S2)

    Returns a new set that contains each element in S1 that is not also in S2, and no other elements.

subtract(S1, S2)

    Returns a new set that contains each element in S1 that is not also in S2, and no other elements.

is_subset(S1, S2)

    Returns `true' if each element in S1 is also a member of S2, and `false' otherwise.

to_list(S)

    Returns an ordered list of all elements in set S. The list never contains duplicates (of course).

from_list(List)

    Creates a set containing all elements in List, where List may be unordered and contain duplicates.

from_ordset(L)

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

smallest(S)

    Returns the smallest element in set S. Assumes that the set S is nonempty.

largest(S)

    Returns the largest element in set S. Assumes that the set S is nonempty.

take_smallest(S)

    Returns {X, S1}, where X is the smallest element in set S, and S1 is the set S with element X deleted. Assumes that the set S is nonempty.

take_largest(S)

    Returns {X, S1}, where X is the largest element in set S, and S1 is the set S with element X deleted. Assumes that the set S is nonempty.

iterator(S)

    Returns an iterator that can be used for traversing the entries of set S; see `next'. The implementation of this is very efficient; traversing the whole set using `next' is only slightly slower than getting the list of all elements using `to_list' 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.

next(T)

    Returns {X, T1} where X is the smallest element referred to by the iterator T, and T1 is the new iterator to be used for traversing the remaining elements, or the atom `none' if no elements remain.

filter(P, S)

    Filters set S using predicate function P. Included for compatibility with `sets'.

fold(F, A, S)

    Folds function F over set S with A as the initial accumulator. Included for compatibility with `sets'.

is_set(S)

    Returns 'true' if S appears to be a set, and 'false' otherwise. Not recommended; included for compatibility with `sets'.

SEE ALSO

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

AUTHOR
Richard Carlsson - support@erlang.ericsson.se