gb_sets
(stdlib)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.
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
The following functions in this module also exist and provides
the same functionality in the
sets(3) and
ordsets(3)
modules. That is, by only changing the module name for each call,
you can try out different set representations.
Types
set(Element)
A general balanced set.
set() = set(term())
iter(Element)
A general balanced set iterator.
iter() = iter(term())
Functions
add(Element, Set1) -> Set2
Set1 = Set2 = set(Element)
add_element(Element, Set1) -> Set2
Set1 = Set2 = set(Element)
Returns a new set formed from with
inserted. If
is already an
element in , nothing is changed.
balance(Set1) -> Set2
Set1 = Set2 = set(Element)
Rebalances the tree representation of .
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.
del_element(Element, Set1) -> Set2
Set1 = Set2 = set(Element)
Returns a new set formed from with
removed. If
is not an element
in , nothing is changed.
delete(Element, Set1) -> Set2
Set1 = Set2 = set(Element)
Returns a new set formed from with
removed. Assumes that
is present
in .
delete_any(Element, Set1) -> Set2
Set1 = Set2 = set(Element)
Returns a new set formed from with
removed. If
is not an element
in , nothing is changed.
difference(Set1, Set2) -> Set3
Set1 = Set2 = Set3 = set(Element)
Returns only the elements of that are not
also elements of .
filter(Pred, Set1) -> Set2
Pred = fun((Element) -> boolean())Set1 = Set2 = set(Element)
Filters elements in using predicate function
.
fold(Function, Acc0, Set) -> Acc1
Function = fun((Element, AccIn) -> AccOut)Acc0 = Acc1 = AccIn = AccOut = AccSet = set(Element)
Folds over every element in
returning the final value of the accumulator.
from_list(List) -> Set
List = [Element]Set = set(Element)
Returns a set of the elements in , where
can be unordered and contain duplicates.
from_ordset(List) -> Set
List = [Element]Set = set(Element)
Turns an ordered-set list into a set.
The list must not contain duplicates.
insert(Element, Set1) -> Set2
Set1 = Set2 = set(Element)
Returns a new set formed from with
inserted. Assumes that
is not
present in .
intersection(SetList) -> Set
Returns the intersection of the non-empty list of sets.
intersection(Set1, Set2) -> Set3
Set1 = Set2 = Set3 = set(Element)
Returns the intersection of and
.
is_disjoint(Set1, Set2) -> boolean()
Set1 = Set2 = set(Element)
Returns true if and
are disjoint (have no elements in common),
otherwise false.
is_element(Element, Set) -> boolean()
Set = set(Element)
Returns true if is an element of
, otherwise false.
is_member(Element, Set) -> boolean()
Set = set(Element)
Returns true if is an element of
, otherwise false.
is_set(Term) -> boolean()
Term = term()
Returns true if appears to be a set,
otherwise false.
is_subset(Set1, Set2) -> boolean()
Set1 = Set2 = set(Element)
Returns true when every element of is
also a member of , otherwise false.
iterator(Set) -> 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.
iterator_from(Element, Set) -> Iter
Returns an iterator that can be used for traversing the
entries of ; see
next/1.
The difference as compared to the iterator returned by
iterator/1
is that the first element greater than
or equal to is returned.
largest(Set) -> Element
Set = set(Element)
Returns the largest element in . Assumes that
is not empty.
next(Iter1) -> {Element, Iter2} | none
Iter1 = Iter2 = iter(Element)
Returns {, where
is the smallest element referred to by
iterator ,
and is the new iterator to be used for
traversing the remaining elements, or the atom none if
no elements remain.
singleton(Element) -> set(Element)
Returns a set containing only element .
smallest(Set) -> Element
Set = set(Element)
Returns the smallest element in . Assumes that
is not empty.
subtract(Set1, Set2) -> Set3
Set1 = Set2 = Set3 = set(Element)
Returns only the elements of that are not
also elements of .
take_largest(Set1) -> {Element, Set2}
Set1 = Set2 = set(Element)
Returns {, where
is the largest element in
, and is this set
with deleted. Assumes that
is not empty.
take_smallest(Set1) -> {Element, Set2}
Set1 = Set2 = set(Element)
Returns {, where
is the smallest element in
, and is this set
with deleted. Assumes that
is not empty.
union(SetList) -> Set
Returns the merged (union) set of the list of sets.
union(Set1, Set2) -> Set3
Set1 = Set2 = Set3 = set(Element)
Returns the merged (union) set of and
.