# FreeBSD Manual Pages

gb_sets(3) Erlang Module Definition gb_sets(3)NAMEgb_sets - General balanced trees.DESCRIPTIONThis module provides ordered sets using Prof. Arne Andersson's General Balanced Trees. Ordered sets can be much more efficient than using or- dered 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 NOTEThe complexity on set operations is bounded by eitherO(|S|)orO(|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 al- most 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 accumulat- ing elements a few at a time, building up a large set (> 100-200 ele- ments), and repeatedly testing for membership in the current set. As with normal tree structures, lookup (membership testing), insertion, and deletion have logarithmic complexity.COMPATIBILITYThe following functions in this module also exist and provides the same functionality in thesets(3)andordsets(3)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_empty/1*is_set/1*is_subset/2*new/0*size/1*subtract/2*to_list/1*union/1*union/2DATA TYPESset(Element)A general balanced set.set()=set(term())iter(Element)A general balanced set iterator.iter()=iter(term())EXPORTSadd(Element,Set1)->Set2add_element(Element,Set1)->Set2Types: Set1 = Set2 =set(Element) Returns a new set formed fromSet1withElementinserted. IfEl-ementis already an element inSet1, nothing is changed.balance(Set1)->Set2Types: Set1 = Set2 =set(Element) Rebalances the tree representation ofSet1. Notice that this is rarely necessary, but can be motivated when a large number of elements have been deleted from the tree without further inser- tions. Rebalancing can then be forced to minimise lookup times, as deletion does not rebalance the tree.del_element(Element,Set1)->Set2Types: Set1 = Set2 =set(Element) Returns a new set formed fromSet1withElementremoved. IfEle-mentis not an element inSet1, nothing is changed.delete(Element,Set1)->Set2Types: Set1 = Set2 =set(Element) Returns a new set formed fromSet1withElementremoved. Assumes thatElementis present inSet1.delete_any(Element,Set1)->Set2Types: Set1 = Set2 =set(Element) Returns a new set formed fromSet1withElementremoved. IfEle-mentis not an element inSet1, nothing is changed.difference(Set1,Set2)->Set3Types: Set1 = Set2 = Set3 =set(Element) Returns only the elements ofSet1that are not also elements ofSet2.empty()->SetTypes: Set =set()Returns a new empty set.filter(Pred,Set1)->Set2Types: Pred = fun((Element) -> boolean()) Set1 = Set2 =set(Element) Filters elements inSet1using predicate functionPred.fold(Function,Acc0,Set)->Acc1Types: Function = fun((Element, AccIn) -> AccOut) Acc0 = Acc1 = AccIn = AccOut = Acc Set =set(Element) FoldsFunctionover every element inSetreturning the final value of the accumulator.from_list(List)->SetTypes: List = [Element] Set =set(Element) Returns a set of the elements inList, whereListcan be un- ordered and contain duplicates.from_ordset(List)->SetTypes: List = [Element] Set =set(Element) Turns an ordered-set listListinto a set. The list must not contain duplicates.insert(Element,Set1)->Set2Types: Set1 = Set2 =set(Element) Returns a new set formed fromSet1withElementinserted. As- sumes thatElementis not present inSet1.intersection(SetList)->SetTypes: SetList = [set(Element), ...] Set =set(Element) Returns the intersection of the non-empty list of sets.intersection(Set1,Set2)->Set3Types: Set1 = Set2 = Set3 =set(Element) Returns the intersection ofSet1andSet2.is_disjoint(Set1,Set2)->boolean()Types: Set1 = Set2 =set(Element) ReturnstrueifSet1andSet2are disjoint (have no elements in common), otherwisefalse.is_element(Element,Set)->boolean()Types: Set =set(Element) ReturnstrueifElementis an element ofSet, otherwisefalse.is_empty(Set)->boolean()Types: Set =set()ReturnstrueifSetis an empty set, otherwisefalse.is_member(Element,Set)->boolean()Types: Set =set(Element) ReturnstrueifElementis an element ofSet, otherwisefalse.is_set(Term)->boolean()Types: Term = term() ReturnstrueifTermappears to be a set, otherwisefalse.is_subset(Set1,Set2)->boolean()Types: Set1 = Set2 =set(Element) Returnstruewhen every element ofSet1is also a member ofSet2, otherwisefalse.iterator(Set)->IterTypes: Set =set(Element) Iter =iter(Element) Returns an iterator that can be used for traversing the entries ofSet; seenext/1. The implementation of this is very effi- cient; traversing the whole set usingnext/1is only slightly slower than getting the list of all elements usingto_list/1and 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)->IterTypes: Set =set(Element) Iter =iter(Element) Returns an iterator that can be used for traversing the entries ofSet; seenext/1. The difference as compared to the iterator returned byiterator/1is that the first element greater than or equal toElementis returned.largest(Set)->ElementTypes: Set =set(Element) Returns the largest element inSet. Assumes thatSetis not empty.new()->SetTypes: Set =set()Returns a new empty set.next(Iter1)->{Element,Iter2}|noneTypes: Iter1 = Iter2 =iter(Element) Returns{Element,Iter2}, whereElementis the smallest element referred to by iteratorIter1, andIter2is the new iterator to be used for traversing the remaining elements, or the atomnoneif no elements remain.singleton(Element)->set(Element)Returns a set containing only elementElement.size(Set)->integer()>=0Types: Set =set()Returns the number of elements inSet.smallest(Set)->ElementTypes: Set =set(Element) Returns the smallest element inSet. Assumes thatSetis not empty.subtract(Set1,Set2)->Set3Types: Set1 = Set2 = Set3 =set(Element) Returns only the elements ofSet1that are not also elements ofSet2.take_largest(Set1)->{Element,Set2}Types: Set1 = Set2 =set(Element) Returns{Element,Set2}, whereElementis the largest element inSet1, andSet2is this set withElementdeleted. Assumes thatSet1is not empty.take_smallest(Set1)->{Element,Set2}Types: Set1 = Set2 =set(Element) Returns{Element,Set2}, whereElementis the smallest element inSet1, andSet2is this set withElementdeleted. Assumes thatSet1is not empty.to_list(Set)->ListTypes: Set =set(Element) List = [Element] Returns the elements ofSetas a list.union(SetList)->SetTypes: SetList = [set(Element), ...] Set =set(Element) Returns the merged (union) set of the list of sets.union(Set1,Set2)->Set3Types: Set1 = Set2 = Set3 =set(Element) Returns the merged (union) set ofSet1andSet2.SEE ALSOgb_trees(3),ordsets(3),sets(3)Ericsson AB stdlib 3.8 gb_sets(3)

NAME | DESCRIPTION | COMPLEXITY NOTE | COMPATIBILITY | DATA TYPES | EXPORTS | SEE ALSO

Want to link to this manual page? Use this URL:

<https://www.freebsd.org/cgi/man.cgi?query=gb_sets&sektion=3&manpath=FreeBSD+13.0-RELEASE+and+Ports>