aboutsummaryrefslogblamecommitdiffstats
path: root/lib/stdlib/doc/src/gb_sets.xml
blob: accec623b9e2991cdc3562268fe7e663b3bc11f3 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487






































































































































































































































































































































































































































































































                                                                                 
<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>2001</year><year>2009</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      The contents of this file are subject to the Erlang Public License,
      Version 1.1, (the "License"); you may not use this file except in
      compliance with the License. You should have received a copy of the
      Erlang Public License along with this software. If not, it can be
      retrieved online at http://www.erlang.org/.
    
      Software distributed under the License is distributed on an "AS IS"
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
      the License for the specific language governing rights and limitations
      under the License.
    
    </legalnotice>

    <title>gb_sets</title>
    <prepared></prepared>
    <docno></docno>
    <date></date>
    <rev></rev>
  </header>
  <module>gb_sets</module>
  <modulesummary>General Balanced Trees</modulesummary>
  <description>
    <p>An implementation of ordered sets using Prof. Arne Andersson's
      General Balanced Trees. This can be much more efficient than
      using ordered lists, for larger sets, but depends on the
      application.</p>
  </description>

  <section>
    <title>Complexity note</title>
    <p>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 between 10
      and 100 times. This implementation is particularly suited for
      accumulating elements a few at a time, building up a large set
      (more than 100-200 elements), and repeatedly testing for
      membership in the current set.</p>
    <p>As with normal tree structures, lookup (membership testing),
      insertion and deletion have logarithmic complexity.</p>
  </section>

  <section>
    <title>Compatibility</title>
    <p>All of the following functions in this module also exist
      and do the same thing in the <c>sets</c> and <c>ordsets</c>
      modules. That is, by only changing the module name for each call,
      you can try out different set representations.</p>
    <p></p>
    <list type="bulleted">
      <item>
        <p><c>add_element/2</c></p>
      </item>
      <item>
        <p><c>del_element/2</c></p>
      </item>
      <item>
        <p><c>filter/2</c></p>
      </item>
      <item>
        <p><c>fold/3</c></p>
      </item>
      <item>
        <p><c>from_list/1</c></p>
      </item>
      <item>
        <p><c>intersection/1</c></p>
      </item>
      <item>
        <p><c>intersection/2</c></p>
      </item>
      <item>
        <p><c>is_element/2</c></p>
      </item>
      <item>
        <p><c>is_set/1</c></p>
      </item>
      <item>
        <p><c>is_subset/2</c></p>
      </item>
      <item>
        <p><c>new/0</c></p>
      </item>
      <item>
        <p><c>size/1</c></p>
      </item>
      <item>
        <p><c>subtract/2</c></p>
      </item>
      <item>
        <p><c>to_list/1</c></p>
      </item>
      <item>
        <p><c>union/1</c></p>
      </item>
      <item>
        <p><c>union/2</c></p>
      </item>
    </list>
  </section>

  <section>
    <title>DATA TYPES</title>
    <code type="none">
gb_set() = a GB set</code>
  </section>
  <funcs>
    <func>
      <name>add(Element, Set1) -> Set2</name>
      <name>add_element(Element, Set1) -> Set2</name>
      <fsummary>Add a (possibly existing) element to a gb_set</fsummary>
      <type>
        <v>Element = term()</v>
        <v>Set1 = Set2 = gb_set()</v>
      </type>
      <desc>
        <p>Returns a new gb_set formed from <c>Set1</c> with
          <c>Element</c> inserted. If <c>Element</c> is already an
          element in <c>Set1</c>, nothing is changed.</p>
      </desc>
    </func>
    <func>
      <name>balance(Set1) -> Set2</name>
      <fsummary>Rebalance tree representation of a gb_set</fsummary>
      <type>
        <v>Set1 = Set2 = gb_set()</v>
      </type>
      <desc>
        <p>Rebalances the tree representation of <c>Set1</c>. Note that
          this is rarely necessary, but may be motivated when a large
          number of elements have been deleted from the tree without
          further insertions. Rebalancing could then be forced in order
          to minimise lookup times, since deletion only does not
          rebalance the tree.</p>
      </desc>
    </func>
    <func>
      <name>delete(Element, Set1) -> Set2</name>
      <fsummary>Remove an element from a gb_set</fsummary>
      <type>
        <v>Element = term()</v>
        <v>Set1 = Set2 = gb_set()</v>
      </type>
      <desc>
        <p>Returns a new gb_set formed from <c>Set1</c> with
          <c>Element</c> removed. Assumes that <c>Element</c> is present
          in <c>Set1</c>.</p>
      </desc>
    </func>
    <func>
      <name>delete_any(Element, Set1) -> Set2</name>
      <name>del_element(Element, Set1) -> Set2</name>
      <fsummary>Remove a (possibly non-existing) element from a gb_set</fsummary>
      <type>
        <v>Element = term()</v>
        <v>Set1 = Set2 = gb_set()</v>
      </type>
      <desc>
        <p>Returns a new gb_set formed from <c>Set1</c> with
          <c>Element</c> removed. If <c>Element</c> is not an element
          in <c>Set1</c>, nothing is changed.</p>
      </desc>
    </func>
    <func>
      <name>difference(Set1, Set2) -> Set3</name>
      <name>subtract(Set1, Set2) -> Set3</name>
      <fsummary>Return the difference of two gb_sets</fsummary>
      <type>
        <v>Set1 = Set2 = Set3 = gb_set()</v>
      </type>
      <desc>
        <p>Returns only the elements of <c>Set1</c> which are not also
          elements of <c>Set2</c>.</p>
      </desc>
    </func>
    <func>
      <name>empty() -> Set</name>
      <name>new() -> Set</name>
      <fsummary>Return an empty gb_set</fsummary>
      <type>
        <v>Set = gb_set()</v>
      </type>
      <desc>
        <p>Returns a new empty gb_set.</p>
      </desc>
    </func>
    <func>
      <name>filter(Pred, Set1) -> Set2</name>
      <fsummary>Filter gb_set elements</fsummary>
      <type>
        <v>Pred = fun (E) -> bool()</v>
        <v>&nbsp;E = term()</v>
        <v>Set1 = Set2 = gb_set()</v>
      </type>
      <desc>
        <p>Filters elements in <c>Set1</c> using predicate function
          <c>Pred</c>.</p>
      </desc>
    </func>
    <func>
      <name>fold(Function, Acc0, Set) -> Acc1</name>
      <fsummary>Fold over gb_set elements</fsummary>
      <type>
        <v>Function = fun (E, AccIn) -> AccOut</v>
        <v>Acc0 = Acc1 = AccIn = AccOut = term()</v>
        <v>&nbsp;E = term()</v>
        <v>Set = gb_set()</v>
      </type>
      <desc>
        <p>Folds <c>Function</c> over every element in <c>Set</c>
          returning the final value of the accumulator.</p>
      </desc>
    </func>
    <func>
      <name>from_list(List) -> Set</name>
      <fsummary>Convert a list into a gb_set</fsummary>
      <type>
        <v>List = [term()]</v>
        <v>Set = gb_set()</v>
      </type>
      <desc>
        <p>Returns a gb_set of the elements in <c>List</c>, where
          <c>List</c> may be unordered and contain duplicates.</p>
      </desc>
    </func>
    <func>
      <name>from_ordset(List) -> Set</name>
      <fsummary>Make a gb_set from an ordset list</fsummary>
      <type>
        <v>List = [term()]</v>
        <v>Set = gb_set()</v>
      </type>
      <desc>
        <p>Turns an ordered-set list <c>List</c> into a gb_set. The list
          must not contain duplicates.</p>
      </desc>
    </func>
    <func>
      <name>insert(Element, Set1) -> Set2</name>
      <fsummary>Add a new element to a gb_set</fsummary>
      <type>
        <v>Element = term()</v>
        <v>Set1 = Set2 = gb_set()</v>
      </type>
      <desc>
        <p>Returns a new gb_set formed from <c>Set1</c> with
          <c>Element</c> inserted. Assumes that <c>Element</c> is not
          present in <c>Set1</c>.</p>
      </desc>
    </func>
    <func>
      <name>intersection(Set1, Set2) -> Set3</name>
      <fsummary>Return the intersection of two gb_sets</fsummary>
      <type>
        <v>Set1 = Set2 = Set3 = gb_set()</v>
      </type>
      <desc>
        <p>Returns the intersection of <c>Set1</c> and <c>Set2</c>.</p>
      </desc>
    </func>
    <func>
      <name>intersection(SetList) -> Set</name>
      <fsummary>Return the intersection of a list of gb_sets</fsummary>
      <type>
        <v>SetList = [gb_set()]</v>
        <v>Set = gb_set()</v>
      </type>
      <desc>
        <p>Returns the intersection of the non-empty list of gb_sets.</p>
      </desc>
    </func>
    <func>
      <name>is_disjoint(Set1, Set2) -> bool()</name>
      <fsummary>Check whether two gb_sets are disjoint</fsummary>
      <type>
        <v>Set1 = Set2 = gb_set()</v>
      </type>
      <desc>
        <p>Returns <c>true</c> if <c>Set1</c> and
          <c>Set2</c> are disjoint (have no elements in common),
	  and <c>false</c> otherwise.</p>
      </desc>
    </func>
    <func>
      <name>is_empty(Set) -> bool()</name>
      <fsummary>Test for empty gb_set</fsummary>
      <type>
        <v>Set = gb_set()</v>
      </type>
      <desc>
        <p>Returns <c>true</c> if <c>Set</c> is an empty set, and
          <c>false</c> otherwise.</p>
      </desc>
    </func>
    <func>
      <name>is_member(Element, Set) -> bool()</name>
      <name>is_element(Element, Set) -> bool()</name>
      <fsummary>Test for membership of a gb_set</fsummary>
      <type>
        <v>Element = term()</v>
        <v>Set = gb_set()</v>
      </type>
      <desc>
        <p>Returns <c>true</c> if <c>Element</c> is an element of
          <c>Set</c>, otherwise <c>false</c>.</p>
      </desc>
    </func>
    <func>
      <name>is_set(Term) -> bool()</name>
      <fsummary>Test for a gb_set</fsummary>
      <type>
        <v>Term = term()</v>
      </type>
      <desc>
        <p>Returns <c>true</c> if <c>Set</c> appears to be a gb_set,
          otherwise <c>false</c>.</p>
      </desc>
    </func>
    <func>
      <name>is_subset(Set1, Set2) -> bool()</name>
      <fsummary>Test for subset</fsummary>
      <type>
        <v>Set1 = Set2 = gb_set()</v>
      </type>
      <desc>
        <p>Returns <c>true</c> when every element of <c>Set1</c> is
          also a member of <c>Set2</c>, otherwise <c>false</c>.</p>
      </desc>
    </func>
    <func>
      <name>iterator(Set) -> Iter</name>
      <fsummary>Return an iterator for a gb_set</fsummary>
      <type>
        <v>Set = gb_set()</v>
        <v>Iter = term()</v>
      </type>
      <desc>
        <p>Returns an iterator that can be used for traversing the
          entries of <c>Set</c>; see <c>next/1</c>. The implementation
          of this is very efficient; traversing the whole set using
          <c>next/1</c> is only slightly slower than getting the list
          of all elements using <c>to_list/1</c> 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.</p>
      </desc>
    </func>
    <func>
      <name>largest(Set) -> term()</name>
      <fsummary>Return largest element</fsummary>
      <type>
        <v>Set = gb_set()</v>
      </type>
      <desc>
        <p>Returns the largest element in <c>Set</c>. Assumes that
          <c>Set</c> is nonempty.</p>
      </desc>
    </func>
    <func>
      <name>next(Iter1) -> {Element, Iter2} | none</name>
      <fsummary>Traverse a gb_set with an iterator</fsummary>
      <type>
        <v>Iter1 = Iter2 = Element = term()</v>
      </type>
      <desc>
        <p>Returns <c>{Element, Iter2}</c> where <c>Element</c> is the
          smallest element referred to by the iterator <c>Iter1</c>,
          and <c>Iter2</c> is the new iterator to be used for
          traversing the remaining elements, or the atom <c>none</c> if
          no elements remain.</p>
      </desc>
    </func>
    <func>
      <name>singleton(Element) -> gb_set()</name>
      <fsummary>Return a gb_set with one element</fsummary>
      <type>
        <v>Element = term()</v>
      </type>
      <desc>
        <p>Returns a gb_set containing only the element <c>Element</c>.</p>
      </desc>
    </func>
    <func>
      <name>size(Set) -> int()</name>
      <fsummary>Return the number of elements in a gb_set</fsummary>
      <type>
        <v>Set = gb_set()</v>
      </type>
      <desc>
        <p>Returns the number of elements in <c>Set</c>.</p>
      </desc>
    </func>
    <func>
      <name>smallest(Set) -> term()</name>
      <fsummary>Return smallest element</fsummary>
      <type>
        <v>Set = gb_set()</v>
      </type>
      <desc>
        <p>Returns the smallest element in <c>Set</c>. Assumes that
          <c>Set</c> is nonempty.</p>
      </desc>
    </func>
    <func>
      <name>take_largest(Set1) -> {Element, Set2}</name>
      <fsummary>Extract largest element</fsummary>
      <type>
        <v>Set1 = Set2 = gb_set()</v>
        <v>Element = term()</v>
      </type>
      <desc>
        <p>Returns <c>{Element, Set2}</c>, where <c>Element</c> is the
          largest element in <c>Set1</c>, and <c>Set2</c> is this set
          with <c>Element</c> deleted. Assumes that <c>Set1</c> is
          nonempty.</p>
      </desc>
    </func>
    <func>
      <name>take_smallest(Set1) -> {Element, Set2}</name>
      <fsummary>Extract smallest element</fsummary>
      <type>
        <v>Set1 = Set2 = gb_set()</v>
        <v>Element = term()</v>
      </type>
      <desc>
        <p>Returns <c>{Element, Set2}</c>, where <c>Element</c> is the
          smallest element in <c>Set1</c>, and <c>Set2</c> is this set
          with <c>Element</c> deleted. Assumes that <c>Set1</c> is
          nonempty.</p>
      </desc>
    </func>
    <func>
      <name>to_list(Set) -> List</name>
      <fsummary>Convert a gb_set into a list</fsummary>
      <type>
        <v>Set = gb_set()</v>
        <v>List = [term()]</v>
      </type>
      <desc>
        <p>Returns the elements of <c>Set</c> as a list.</p>
      </desc>
    </func>
    <func>
      <name>union(Set1, Set2) -> Set3</name>
      <fsummary>Return the union of two gb_sets</fsummary>
      <type>
        <v>Set1 = Set2 = Set3 = gb_set()</v>
      </type>
      <desc>
        <p>Returns the merged (union) gb_set of <c>Set1</c> and
          <c>Set2</c>.</p>
      </desc>
    </func>
    <func>
      <name>union(SetList) -> Set</name>
      <fsummary>Return the union of a list of gb_sets</fsummary>
      <type>
        <v>SetList = [gb_set()]</v>
        <v>Set = gb_set()</v>
      </type>
      <desc>
        <p>Returns the merged (union) gb_set of the list of gb_sets.</p>
      </desc>
    </func>
  </funcs>

  <section>
    <title>SEE ALSO</title>
    <p><seealso marker="gb_trees">gb_trees(3)</seealso>, 
      <seealso marker="ordsets">ordsets(3)</seealso>, 
      <seealso marker="sets">sets(3)</seealso></p>
  </section>
</erlref>