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 orderedlist 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 100200 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

filter/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
Functions
add(Element, Set1) > Set2
Element = term()
Set1 = Set2 = gb_set()
add_element(Element, Set1) > Set2
Element = term()
Set1 = Set2 = gb_set()
Returns a new gb_set formed from
with
inserted. If
is already an
element in
, nothing is changed.
balance(Set1) > Set2
Set1 = Set2 = gb_set()
Rebalances the tree representation of
. 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.
delete(Element, Set1) > Set2
Element = term()
Set1 = Set2 = gb_set()
Returns a new gb_set formed from
with
removed. Assumes that
is present
in
.
delete_any(Element, Set1) > Set2
Element = term()
Set1 = Set2 = gb_set()
del_element(Element, Set1) > Set2
Element = term()
Set1 = Set2 = gb_set()
Returns a new gb_set formed from
with
removed. If
is not an element
in
, nothing is changed.
difference(Set1, Set2) > Set3
Set1 = Set2 = Set3 = gb_set()
subtract(Set1, Set2) > Set3
Set1 = Set2 = Set3 = gb_set()
Returns only the elements of
which are not also
elements of
.
empty() > Set
Set = gb_set()
new() > Set
Set = gb_set()
Returns a new empty gb_set.
filter(Pred, Set1) > Set2
Pred = fun((E :: term()) > boolean())
Set1 = Set2 = gb_set()
Filters elements in
using predicate function
.
fold(Function, Acc0, Set) > Acc1
Function = fun((E :: term(), AccIn) > AccOut)
Acc0 = Acc1 = AccIn = AccOut = term()
Set = gb_set()
Folds
over every element in
returning the final value of the accumulator.
from_list(List) > Set
List = [term()]
Set = gb_set()
Returns a gb_set of the elements in
, where
may be unordered and contain duplicates.
from_ordset(List) > Set
List = [term()]
Set = gb_set()
Turns an orderedset list
into a gb_set. The list
must not contain duplicates.
insert(Element, Set1) > Set2
Element = term()
Set1 = Set2 = gb_set()
Returns a new gb_set formed from
with
inserted. Assumes that
is not
present in
.
intersection(Set1, Set2) > Set3
Set1 = Set2 = Set3 = gb_set()
Returns the intersection of
and
.
intersection(SetList) > Set
SetList = [gb_set(), ...]
Set = gb_set()
Returns the intersection of the nonempty list of gb_sets.
is_disjoint(Set1, Set2) > boolean()
Set1 = Set2 = gb_set()
Returns true
if
and
are disjoint (have no elements in common),
and false
otherwise.
is_empty(Set) > boolean()
Set = gb_set()
Returns true
if
is an empty set, and
false
otherwise.
is_member(Element, Set) > boolean()
Element = term()
Set = gb_set()
is_element(Element, Set) > boolean()
Element = term()
Set = gb_set()
Returns true
if
is an element of
, otherwise false
.
is_set(Term) > boolean()
Term = term()
Returns true
if
appears to be a gb_set,
otherwise false
.
is_subset(Set1, Set2) > boolean()
Set1 = Set2 = gb_set()
Returns true
when every element of
is
also a member of
, otherwise false
.
iterator(Set) > Iter
Set = gb_set()
Iter = iter()
Returns an iterator that can be used for traversing the
entries of
; 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.
largest(Set) > term()
Set = gb_set()
Returns the largest element in
. Assumes that
is nonempty.
next(Iter1) > {Element, Iter2}  none
Iter1 = Iter2 = iter()
Element = term()
Returns {
where
is the
smallest element referred to by the iterator
,
and
is the new iterator to be used for
traversing the remaining elements, or the atom none
if
no elements remain.
singleton(Element) > gb_set()
Element = term()
Returns a gb_set containing only the element
.
size(Set) > integer() >= 0
Set = gb_set()
Returns the number of elements in
.
smallest(Set) > term()
Set = gb_set()
Returns the smallest element in
. Assumes that
is nonempty.
take_largest(Set1) > {Element, Set2}
Set1 = Set2 = gb_set()
Element = term()
Returns {
, where
is the
largest element in
, and
is this set
with
deleted. Assumes that
is
nonempty.
take_smallest(Set1) > {Element, Set2}
Set1 = Set2 = gb_set()
Element = term()
Returns {
, where
is the
smallest element in
, and
is this set
with
deleted. Assumes that
is
nonempty.
to_list(Set) > List
Set = gb_set()
List = [term()]
Returns the elements of
as a list.
union(Set1, Set2) > Set3
Set1 = Set2 = Set3 = gb_set()
Returns the merged (union) gb_set of
and
.
union(SetList) > Set
SetList = [gb_set(), ...]
Set = gb_set()
Returns the merged (union) gb_set of the list of gb_sets.