1gb_sets(3)                 Erlang Module Definition                 gb_sets(3)
2
3
4

NAME

6       gb_sets - General balanced trees.
7

DESCRIPTION

9       This  module provides ordered sets using Prof. Arne Andersson's General
10       Balanced Trees. Ordered sets can be  much  more  efficient  than  using
11       ordered lists, for larger sets, but depends on the application.
12
13       This  module considers two elements as different if and only if they do
14       not compare equal (==).
15

COMPLEXITY NOTE

17       The complexity on set operations is bounded by either O(|S|) or O(|T| *
18       log(|S|)),  where  S  is  the  largest given set, depending on which is
19       fastest for any particular function call.  For  operating  on  sets  of
20       almost  equal  size,  this  implementation is about 3 times slower than
21       using ordered-list sets directly. For sets  of  very  different  sizes,
22       however,  this  solution  can  be arbitrarily much faster; in practical
23       cases, often 10-100 times. This implementation is  particularly  suited
24       for  accumulating  elements a few at a time, building up a large set (>
25       100-200 elements), and repeatedly testing for membership in the current
26       set.
27
28       As with normal tree structures, lookup (membership testing), insertion,
29       and deletion have logarithmic complexity.
30

COMPATIBILITY

32       The following functions in this module also exist and provides the same
33       functionality  in  the sets(3) and ordsets(3) modules. That is, by only
34       changing the module name for each call, you can try out  different  set
35       representations.
36
37         * add_element/2
38
39         * del_element/2
40
41         * filter/2
42
43         * fold/3
44
45         * from_list/1
46
47         * intersection/1
48
49         * intersection/2
50
51         * is_element/2
52
53         * is_set/1
54
55         * is_subset/2
56
57         * new/0
58
59         * size/1
60
61         * subtract/2
62
63         * to_list/1
64
65         * union/1
66
67         * union/2
68

DATA TYPES

70       set(Element)
71
72              A general balanced set.
73
74       set() = set(term())
75
76       iter(Element)
77
78              A general balanced set iterator.
79
80       iter() = iter(term())
81

EXPORTS

83       add(Element, Set1) -> Set2
84
85       add_element(Element, Set1) -> Set2
86
87              Types:
88
89                 Set1 = Set2 = set(Element)
90
91              Returns  a  new  set  formed from Set1 with Element inserted. If
92              Element is already an element in Set1, nothing is changed.
93
94       balance(Set1) -> Set2
95
96              Types:
97
98                 Set1 = Set2 = set(Element)
99
100              Rebalances the tree representation of Set1. Notice that this  is
101              rarely  necessary,  but  can be motivated when a large number of
102              elements have been deleted from the tree without further  inser‐
103              tions.  Rebalancing can then be forced to minimise lookup times,
104              as deletion does not rebalance the tree.
105
106       del_element(Element, Set1) -> Set2
107
108              Types:
109
110                 Set1 = Set2 = set(Element)
111
112              Returns a new set formed from Set1 with Element removed. If Ele‐
113              ment is not an element in Set1, nothing is changed.
114
115       delete(Element, Set1) -> Set2
116
117              Types:
118
119                 Set1 = Set2 = set(Element)
120
121              Returns a new set formed from Set1 with Element removed. Assumes
122              that Element is present in Set1.
123
124       delete_any(Element, Set1) -> Set2
125
126              Types:
127
128                 Set1 = Set2 = set(Element)
129
130              Returns a new set formed from Set1 with Element removed. If Ele‐
131              ment is not an element in Set1, nothing is changed.
132
133       difference(Set1, Set2) -> Set3
134
135              Types:
136
137                 Set1 = Set2 = Set3 = set(Element)
138
139              Returns  only the elements of Set1 that are not also elements of
140              Set2.
141
142       empty() -> Set
143
144              Types:
145
146                 Set = set()
147
148              Returns a new empty set.
149
150       filter(Pred, Set1) -> Set2
151
152              Types:
153
154                 Pred = fun((Element) -> boolean())
155                 Set1 = Set2 = set(Element)
156
157              Filters elements in Set1 using predicate function Pred.
158
159       fold(Function, Acc0, Set) -> Acc1
160
161              Types:
162
163                 Function = fun((Element, AccIn) -> AccOut)
164                 Acc0 = Acc1 = AccIn = AccOut = Acc
165                 Set = set(Element)
166
167              Folds Function over every element in  Set  returning  the  final
168              value of the accumulator.
169
170       from_list(List) -> Set
171
172              Types:
173
174                 List = [Element]
175                 Set = set(Element)
176
177              Returns  a  set  of  the  elements  in  List,  where List can be
178              unordered and contain duplicates.
179
180       from_ordset(List) -> Set
181
182              Types:
183
184                 List = [Element]
185                 Set = set(Element)
186
187              Turns an ordered-set list List into a set.  The  list  must  not
188              contain duplicates.
189
190       insert(Element, Set1) -> Set2
191
192              Types:
193
194                 Set1 = Set2 = set(Element)
195
196              Returns  a  new  set  formed  from  Set1  with Element inserted.
197              Assumes that Element is not present in Set1.
198
199       intersection(SetList) -> Set
200
201              Types:
202
203                 SetList = [set(Element), ...]
204                 Set = set(Element)
205
206              Returns the intersection of the non-empty list of sets.
207
208       intersection(Set1, Set2) -> Set3
209
210              Types:
211
212                 Set1 = Set2 = Set3 = set(Element)
213
214              Returns the intersection of Set1 and Set2.
215
216       is_disjoint(Set1, Set2) -> boolean()
217
218              Types:
219
220                 Set1 = Set2 = set(Element)
221
222              Returns true if Set1 and Set2 are disjoint (have no elements  in
223              common), otherwise false.
224
225       is_element(Element, Set) -> boolean()
226
227              Types:
228
229                 Set = set(Element)
230
231              Returns true if Element is an element of Set, otherwise false.
232
233       is_empty(Set) -> boolean()
234
235              Types:
236
237                 Set = set()
238
239              Returns true if Set is an empty set, otherwise false.
240
241       is_member(Element, Set) -> boolean()
242
243              Types:
244
245                 Set = set(Element)
246
247              Returns true if Element is an element of Set, otherwise false.
248
249       is_set(Term) -> boolean()
250
251              Types:
252
253                 Term = term()
254
255              Returns true if Term appears to be a set, otherwise false.
256
257       is_subset(Set1, Set2) -> boolean()
258
259              Types:
260
261                 Set1 = Set2 = set(Element)
262
263              Returns  true  when  every  element  of Set1 is also a member of
264              Set2, otherwise false.
265
266       iterator(Set) -> Iter
267
268              Types:
269
270                 Set = set(Element)
271                 Iter = iter(Element)
272
273              Returns an iterator that can be used for traversing the  entries
274              of  Set;  see  next/1.  The implementation of this is very effi‐
275              cient; traversing the whole set using next/1  is  only  slightly
276              slower than getting the list of all elements using to_list/1 and
277              traversing that. The main advantage of the iterator approach  is
278              that it does not require the complete list of all elements to be
279              built in memory at one time.
280
281       iterator_from(Element, Set) -> Iter
282
283              Types:
284
285                 Set = set(Element)
286                 Iter = iter(Element)
287
288              Returns an iterator that can be used for traversing the  entries
289              of  Set;  see next/1. The difference as compared to the iterator
290              returned by iterator/1 is that the first element greater than or
291              equal to Element is returned.
292
293       largest(Set) -> Element
294
295              Types:
296
297                 Set = set(Element)
298
299              Returns  the  largest  element  in  Set. Assumes that Set is not
300              empty.
301
302       new() -> Set
303
304              Types:
305
306                 Set = set()
307
308              Returns a new empty set.
309
310       next(Iter1) -> {Element, Iter2} | none
311
312              Types:
313
314                 Iter1 = Iter2 = iter(Element)
315
316              Returns {Element, Iter2}, where Element is the smallest  element
317              referred  to by iterator Iter1, and Iter2 is the new iterator to
318              be used for traversing the remaining elements, or the atom  none
319              if no elements remain.
320
321       singleton(Element) -> set(Element)
322
323              Returns a set containing only element Element.
324
325       size(Set) -> integer() >= 0
326
327              Types:
328
329                 Set = set()
330
331              Returns the number of elements in Set.
332
333       smallest(Set) -> Element
334
335              Types:
336
337                 Set = set(Element)
338
339              Returns  the  smallest  element  in Set. Assumes that Set is not
340              empty.
341
342       subtract(Set1, Set2) -> Set3
343
344              Types:
345
346                 Set1 = Set2 = Set3 = set(Element)
347
348              Returns only the elements of Set1 that are not also elements  of
349              Set2.
350
351       take_largest(Set1) -> {Element, Set2}
352
353              Types:
354
355                 Set1 = Set2 = set(Element)
356
357              Returns {Element, Set2}, where Element is the largest element in
358              Set1, and Set2 is this set with Element  deleted.  Assumes  that
359              Set1 is not empty.
360
361       take_smallest(Set1) -> {Element, Set2}
362
363              Types:
364
365                 Set1 = Set2 = set(Element)
366
367              Returns  {Element,  Set2}, where Element is the smallest element
368              in Set1, and Set2 is this set with Element deleted. Assumes that
369              Set1 is not empty.
370
371       to_list(Set) -> List
372
373              Types:
374
375                 Set = set(Element)
376                 List = [Element]
377
378              Returns the elements of Set as a list.
379
380       union(SetList) -> Set
381
382              Types:
383
384                 SetList = [set(Element), ...]
385                 Set = set(Element)
386
387              Returns the merged (union) set of the list of sets.
388
389       union(Set1, Set2) -> Set3
390
391              Types:
392
393                 Set1 = Set2 = Set3 = set(Element)
394
395              Returns the merged (union) set of Set1 and Set2.
396

SEE ALSO

398       gb_trees(3), ordsets(3), sets(3)
399
400
401
402Ericsson AB                     stdlib 3.4.5.1                      gb_sets(3)
Impressum