gb_sets

General Balanced Trees

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.

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

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(Element, Set1) -> Set2

balance(Set1) -> Set2

Rebalances the tree representation of Set1. 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.

is_subset(Set1, Set2) -> boolean()

iterator(Set) -> Iter

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.

iterator_from(Element, Set) -> Iter

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.

largest(Set) -> Element

next(Iter1) -> {Element, Iter2} | none

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