# gb_sets

gb_sets

### MODULE SUMMARY

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/2`
• `size/1`
• `subtract/2`
• `to_list/1`
• `union/1`
• `union/2`

### EXPORTS

Returns a new, empty set.

Returns a new, empty set.

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

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

Returns a set containing only the element X.

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

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

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

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

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

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

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

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

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.

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

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.

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

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

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

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

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

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

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

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

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

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

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.

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.

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.

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.

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

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

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