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