1gb_sets(3) Erlang Module Definition gb_sets(3)
2
3
4
6 gb_sets - General balanced trees.
7
9 This module provides ordered sets using Prof. Arne Andersson's General
10 Balanced Trees. Ordered sets can be much more efficient than using or‐
11 dered 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
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 al‐
20 most equal size, this implementation is about 3 times slower than using
21 ordered-list sets directly. For sets of very different sizes, however,
22 this solution can be arbitrarily much faster; in practical cases, often
23 10-100 times. This implementation is particularly suited for accumulat‐
24 ing elements a few at a time, building up a large set (> 100-200 ele‐
25 ments), and repeatedly testing for membership in the current set.
26
27 As with normal tree structures, lookup (membership testing), insertion,
28 and deletion have logarithmic complexity.
29
31 The following functions in this module also exist and provides the same
32 functionality in the sets(3) and ordsets(3) modules. That is, by only
33 changing the module name for each call, you can try out different set
34 representations.
35
36 * add_element/2
37
38 * del_element/2
39
40 * filter/2
41
42 * fold/3
43
44 * from_list/1
45
46 * intersection/1
47
48 * intersection/2
49
50 * is_element/2
51
52 * is_empty/1
53
54 * is_set/1
55
56 * is_subset/2
57
58 * new/0
59
60 * size/1
61
62 * subtract/2
63
64 * to_list/1
65
66 * union/1
67
68 * union/2
69
71 set(Element)
72
73 A general balanced set.
74
75 set() = set(term())
76
77 iter(Element)
78
79 A general balanced set iterator.
80
81 iter() = iter(term())
82
84 add(Element, Set1) -> Set2
85
86 add_element(Element, Set1) -> Set2
87
88 Types:
89
90 Set1 = Set2 = set(Element)
91
92 Returns a new set formed from Set1 with Element inserted. If El‐
93 ement is already an element in Set1, nothing is changed.
94
95 balance(Set1) -> Set2
96
97 Types:
98
99 Set1 = Set2 = set(Element)
100
101 Rebalances the tree representation of Set1. Notice that this is
102 rarely necessary, but can be motivated when a large number of
103 elements have been deleted from the tree without further inser‐
104 tions. Rebalancing can then be forced to minimise lookup times,
105 as deletion does not rebalance the tree.
106
107 del_element(Element, Set1) -> Set2
108
109 Types:
110
111 Set1 = Set2 = set(Element)
112
113 Returns a new set formed from Set1 with Element removed. If Ele‐
114 ment is not an element in Set1, nothing is changed.
115
116 delete(Element, Set1) -> Set2
117
118 Types:
119
120 Set1 = Set2 = set(Element)
121
122 Returns a new set formed from Set1 with Element removed. Assumes
123 that Element is present in Set1.
124
125 delete_any(Element, Set1) -> Set2
126
127 Types:
128
129 Set1 = Set2 = set(Element)
130
131 Returns a new set formed from Set1 with Element removed. If Ele‐
132 ment is not an element in Set1, nothing is changed.
133
134 difference(Set1, Set2) -> Set3
135
136 Types:
137
138 Set1 = Set2 = Set3 = set(Element)
139
140 Returns only the elements of Set1 that are not also elements of
141 Set2.
142
143 empty() -> Set
144
145 Types:
146
147 Set = set()
148
149 Returns a new empty set.
150
151 filter(Pred, Set1) -> Set2
152
153 Types:
154
155 Pred = fun((Element) -> boolean())
156 Set1 = Set2 = set(Element)
157
158 Filters elements in Set1 using predicate function Pred.
159
160 fold(Function, Acc0, Set) -> Acc1
161
162 Types:
163
164 Function = fun((Element, AccIn) -> AccOut)
165 Acc0 = Acc1 = AccIn = AccOut = Acc
166 Set = set(Element)
167
168 Folds Function over every element in Set returning the final
169 value of the accumulator.
170
171 from_list(List) -> Set
172
173 Types:
174
175 List = [Element]
176 Set = set(Element)
177
178 Returns a set of the elements in List, where List can be un‐
179 ordered and contain duplicates.
180
181 from_ordset(List) -> Set
182
183 Types:
184
185 List = [Element]
186 Set = set(Element)
187
188 Turns an ordered-set list List into a set. The list must not
189 contain duplicates.
190
191 insert(Element, Set1) -> Set2
192
193 Types:
194
195 Set1 = Set2 = set(Element)
196
197 Returns a new set formed from Set1 with Element inserted. As‐
198 sumes that Element is not present in Set1.
199
200 intersection(SetList) -> Set
201
202 Types:
203
204 SetList = [set(Element), ...]
205 Set = set(Element)
206
207 Returns the intersection of the non-empty list of sets.
208
209 intersection(Set1, Set2) -> Set3
210
211 Types:
212
213 Set1 = Set2 = Set3 = set(Element)
214
215 Returns the intersection of Set1 and Set2.
216
217 is_disjoint(Set1, Set2) -> boolean()
218
219 Types:
220
221 Set1 = Set2 = set(Element)
222
223 Returns true if Set1 and Set2 are disjoint (have no elements in
224 common), otherwise false.
225
226 is_element(Element, Set) -> boolean()
227
228 Types:
229
230 Set = set(Element)
231
232 Returns true if Element is an element of Set, otherwise false.
233
234 is_empty(Set) -> boolean()
235
236 Types:
237
238 Set = set()
239
240 Returns true if Set is an empty set, otherwise false.
241
242 is_member(Element, Set) -> boolean()
243
244 Types:
245
246 Set = set(Element)
247
248 Returns true if Element is an element of Set, otherwise false.
249
250 is_set(Term) -> boolean()
251
252 Types:
253
254 Term = term()
255
256 Returns true if Term appears to be a set, otherwise false.
257
258 is_subset(Set1, Set2) -> boolean()
259
260 Types:
261
262 Set1 = Set2 = set(Element)
263
264 Returns true when every element of Set1 is also a member of
265 Set2, otherwise false.
266
267 iterator(Set) -> Iter
268
269 Types:
270
271 Set = set(Element)
272 Iter = iter(Element)
273
274 Returns an iterator that can be used for traversing the entries
275 of Set; see next/1. The implementation of this is very effi‐
276 cient; traversing the whole set using next/1 is only slightly
277 slower than getting the list of all elements using to_list/1 and
278 traversing that. The main advantage of the iterator approach is
279 that it does not require the complete list of all elements to be
280 built in memory at one time.
281
282 iterator_from(Element, Set) -> Iter
283
284 Types:
285
286 Set = set(Element)
287 Iter = iter(Element)
288
289 Returns an iterator that can be used for traversing the entries
290 of Set; see next/1. The difference as compared to the iterator
291 returned by iterator/1 is that the first element greater than or
292 equal to Element is returned.
293
294 largest(Set) -> Element
295
296 Types:
297
298 Set = set(Element)
299
300 Returns the largest element in Set. Assumes that Set is not
301 empty.
302
303 new() -> Set
304
305 Types:
306
307 Set = set()
308
309 Returns a new empty set.
310
311 next(Iter1) -> {Element, Iter2} | none
312
313 Types:
314
315 Iter1 = Iter2 = iter(Element)
316
317 Returns {Element, Iter2}, where Element is the smallest element
318 referred to by iterator Iter1, and Iter2 is the new iterator to
319 be used for traversing the remaining elements, or the atom none
320 if no elements remain.
321
322 singleton(Element) -> set(Element)
323
324 Returns a set containing only element Element.
325
326 size(Set) -> integer() >= 0
327
328 Types:
329
330 Set = set()
331
332 Returns the number of elements in Set.
333
334 smallest(Set) -> Element
335
336 Types:
337
338 Set = set(Element)
339
340 Returns the smallest element in Set. Assumes that Set is not
341 empty.
342
343 subtract(Set1, Set2) -> Set3
344
345 Types:
346
347 Set1 = Set2 = Set3 = set(Element)
348
349 Returns only the elements of Set1 that are not also elements of
350 Set2.
351
352 take_largest(Set1) -> {Element, Set2}
353
354 Types:
355
356 Set1 = Set2 = set(Element)
357
358 Returns {Element, Set2}, where Element is the largest element in
359 Set1, and Set2 is this set with Element deleted. Assumes that
360 Set1 is not empty.
361
362 take_smallest(Set1) -> {Element, Set2}
363
364 Types:
365
366 Set1 = Set2 = set(Element)
367
368 Returns {Element, Set2}, where Element is the smallest element
369 in Set1, and Set2 is this set with Element deleted. Assumes that
370 Set1 is not empty.
371
372 to_list(Set) -> List
373
374 Types:
375
376 Set = set(Element)
377 List = [Element]
378
379 Returns the elements of Set as a list.
380
381 union(SetList) -> Set
382
383 Types:
384
385 SetList = [set(Element), ...]
386 Set = set(Element)
387
388 Returns the merged (union) set of the list of sets.
389
390 union(Set1, Set2) -> Set3
391
392 Types:
393
394 Set1 = Set2 = Set3 = set(Element)
395
396 Returns the merged (union) set of Set1 and Set2.
397
399 gb_trees(3), ordsets(3), sets(3)
400
401
402
403Ericsson AB stdlib 3.14.2.1 gb_sets(3)