summaryrefslogtreecommitdiff
path: root/lib/stdlib/doc/src/sets.xml
blob: 4b70fe4add21ff59956f2d54630c7067882bae14 (plain)
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
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>2000</year><year>2023</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    
    </legalnotice>

    <title>sets</title>
    <prepared>Robert Virding</prepared>
    <responsible>Bjarne D&auml;cker</responsible>
    <docno>1</docno>
    <approved>Bjarne D&auml;cker</approved>
    <checked></checked>
    <date>1999-07-27</date>
    <rev>A</rev>
    <file>sets.xml</file>
  </header>
  <module since="">sets</module>
  <modulesummary>Functions for set manipulation.</modulesummary>
  <description>
    <p>Sets are collections of elements with no duplicate
    elements.</p>

    <p>The data representing a set as used by this module is to be
    regarded as opaque by other modules.  In abstract terms, the
    representation is a composite type of existing Erlang terms. See
    note on <seeguide
    marker="system/reference_manual:data_types#no_user_types">data
    types</seeguide>. Any code assuming knowledge of the format is
    running on thin ice.</p>


    <p>This module provides the same interface as the
      <seeerl marker="ordsets"><c>ordsets(3)</c></seeerl> module
      but with an undefined representation. One difference is
      that while this module considers two elements as different if they
      do not match (<c>=:=</c>), <c>ordsets</c> considers two elements as
      different if and only if they do not compare equal (<c>==</c>).</p>

    <p>Erlang/OTP 24.0 introduced a new internal representation for sets
      which is more performant. Developers can use this new representation
      by passing the <c>{version, 2}</c> flag to <seemfa marker="#new/1"><c>new/1</c></seemfa>
      and <seemfa marker="#from_list/2"><c>from_list/2</c></seemfa>, such
      as <c>sets:new([{version, 2}])</c>. This new representation will
      become the default in future Erlang/OTP versions. Functions that
      work on two sets, such as <seemfa marker="#union/2"><c>union/2</c></seemfa>
      and similar, will work with sets of different versions. In such cases,
      there is no guarantee about the version of the returned set. Explicit
      conversion from the old version to the new one can be done with
      <c>sets:from_list(sets:to_list(Old), [{version,2}])</c>.
    </p>

  </description>

  <section>
    <title>Compatibility</title>
    <p>The following functions in this module also exist and provide
      the same functionality in the
      <seeerl marker="gb_sets"><c>gb_sets(3)</c></seeerl> and
      <seeerl marker="ordsets"><c>ordsets(3)</c></seeerl>
      modules. That is, by only changing the module name for each call,
      you can try out different set representations.</p>
    <list type="bulleted">
      <item><seemfa marker="#add_element/2"><c>add_element/2</c></seemfa>
      </item>
      <item><seemfa marker="#del_element/2"><c>del_element/2</c></seemfa>
      </item>
      <item><seemfa marker="#filter/2"><c>filter/2</c></seemfa>
      </item>
      <item><seemfa marker="#fold/3"><c>fold/3</c></seemfa>
      </item>
      <item><seemfa marker="#from_list/1"><c>from_list/1</c></seemfa>
      </item>
      <item><seemfa marker="#intersection/1"><c>intersection/1</c></seemfa>
      </item>
      <item><seemfa marker="#intersection/2"><c>intersection/2</c></seemfa>
      </item>
      <item><seemfa marker="#is_element/2"><c>is_element/2</c></seemfa>
      </item>
      <item><seemfa marker="#is_empty/1"><c>is_empty/1</c></seemfa>
      </item>
      <item><seemfa marker="#is_set/1"><c>is_set/1</c></seemfa>
      </item>
      <item><seemfa marker="#is_subset/2"><c>is_subset/2</c></seemfa>
      </item>
      <item><seemfa marker="#new/0"><c>new/0</c></seemfa>
      </item>
      <item><seemfa marker="#size/1"><c>size/1</c></seemfa>
      </item>
      <item><seemfa marker="#subtract/2"><c>subtract/2</c></seemfa>
      </item>
      <item><seemfa marker="#to_list/1"><c>to_list/1</c></seemfa>
      </item>
      <item><seemfa marker="#union/1"><c>union/1</c></seemfa>
      </item>
      <item><seemfa marker="#union/2"><c>union/2</c></seemfa>
      </item>
    </list>
    <note>
      <p>
        While the three set implementations offer the same <em>functionality</em>
        with respect to the aforementioned functions, their overall <em>behavior</em>
        may differ. As mentioned, this module considers elements as different if
        and only if they do not match (<c>=:=</c>), while both
        <seeerl marker="ordsets"><c>ordsets</c></seeerl> and
        <seeerl marker="gb_sets"><c>gb_sets</c></seeerl> consider elements as
        different if and only if they do not compare equal (<c>==</c>).
      </p>
      <p><em>Example:</em></p>
      <pre>
1> <input>sets:is_element(1.0, sets:from_list([1])).</input>
false
2> <input>ordsets:is_element(1.0, ordsets:from_list([1])).</input>
true
2> <input>gb_sets:is_element(1.0, gb_sets:from_list([1])).</input>
true</pre>
    </note>
  </section>

  <datatypes>
    <datatype>
      <name name="set" n_vars="1"/>
      <desc><p>As returned by
        <seemfa marker="#new/0"><c>new/0</c></seemfa>.</p></desc>
    </datatype>
    <datatype>
      <name name="set" n_vars="0"/>
    </datatype>
  </datatypes>

  <funcs>
    <func>
      <name name="add_element" arity="2" since=""/>
      <fsummary>Add an element to a <c>Set</c>.</fsummary>
      <desc>
        <p>Returns a new set formed from <c><anno>Set1</anno></c> with
          <c><anno>Element</anno></c> inserted.</p>
      </desc>
    </func>

    <func>
      <name name="del_element" arity="2" since=""/>
      <fsummary>Remove an element from a <c>Set</c>.</fsummary>
      <desc>
        <p>Returns <c><anno>Set1</anno></c>, but with
          <c><anno>Element</anno></c> removed.</p>
      </desc>
    </func>

    <func>
      <name name="filter" arity="2" since=""/>
      <fsummary>Filter set elements.</fsummary>
      <desc>
        <p>Filters elements in <c><anno>Set1</anno></c> with boolean function
          <c><anno>Pred</anno></c>.</p>
      </desc>
    </func>

    <func>
      <name name="fold" arity="3" since=""/>
      <fsummary>Fold over set elements.</fsummary>
      <desc>
        <p>Folds <c><anno>Function</anno></c> over every element in
          <c><anno>Set</anno></c> and returns the final value of the
          accumulator. The evaluation order is undefined.</p>
      </desc>
    </func>

    <func>
      <name name="from_list" arity="1" since=""/>
      <fsummary>Convert a list into a <c>Set</c>.</fsummary>
      <desc>
        <p>Returns a set of the elements in <c><anno>List</anno></c>.</p>
      </desc>
    </func>

    <func>
      <name name="from_list" arity="2" since="OTP 24.0"/>
      <fsummary>Convert a list into a <c>Set</c> at the given version.</fsummary>
      <desc>
        <p>Returns a set of the elements in <c><anno>List</anno></c> at the given version.</p>
      </desc>
    </func>

    <func>
      <name name="intersection" arity="1" since=""/>
      <fsummary>Return the intersection of a list of <c>Sets</c>.</fsummary>
      <desc>
        <p>Returns the intersection of the non-empty list of sets.</p>
      </desc>
    </func>

    <func>
      <name name="intersection" arity="2" since=""/>
      <fsummary>Return the intersection of two <c>Sets</c>.</fsummary>
      <desc>
        <p>Returns the intersection of <c><anno>Set1</anno></c> and
          <c><anno>Set2</anno></c>.</p>
      </desc>
    </func>

    <func>
      <name name="is_disjoint" arity="2" since=""/>
      <fsummary>Check whether two <c>Sets</c> are disjoint.</fsummary>
      <desc>
        <p>Returns <c>true</c> if <c><anno>Set1</anno></c> and
          <c><anno>Set2</anno></c> are disjoint (have no elements in common),
	  otherwise <c>false</c>.</p>
      </desc>
    </func>

    <func>
      <name name="is_element" arity="2" since=""/>
      <fsummary>Test for membership of a <c>Set</c>.</fsummary>
      <desc>
        <p>Returns <c>true</c> if <c><anno>Element</anno></c> is an element of
          <c><anno>Set</anno></c>, otherwise <c>false</c>.</p>
      </desc>
    </func>

    <func>
      <name name="is_empty" arity="1" since="OTP 21.0"/>
      <fsummary>Test for empty set.</fsummary>
      <desc>
        <p>Returns <c>true</c> if <c><anno>Set</anno></c> is an empty set,
          otherwise <c>false</c>.</p>
      </desc>
    </func>

    <func>
      <name name="is_set" arity="1" since=""/>
      <fsummary>Test for a <c>Set</c>.</fsummary>
      <desc>
        <p>Returns <c>true</c> if <c><anno>Set</anno></c> appears to
        be a set of elements, otherwise <c>false</c>.  Note that the
        test is shallow and will return <c>true</c> for any term that
        coincides with the possible representations of a set.  See
        also note on <seeguide
        marker="system/reference_manual:data_types#no_user_types">data
        types</seeguide>.</p>
      </desc>
    </func>

    <func>
      <name name="is_subset" arity="2" since=""/>
      <fsummary>Test for subset.</fsummary>
      <desc>
        <p>Returns <c>true</c> when every element of <c><anno>Set1</anno></c> is
          also a member of <c><anno>Set2</anno></c>, otherwise <c>false</c>.</p>
      </desc>
    </func>

    <func>
      <name name="new" arity="0" since=""/>
      <fsummary>Return an empty set.</fsummary>
      <desc>
        <p>Returns a new empty set.</p>
      </desc>
    </func>

    <func>
      <name name="new" arity="1" since="OTP 24.0"/>
      <fsummary>Return an empty set at the given version.</fsummary>
      <desc>
        <p>Returns a new empty set at the given version.</p>
      </desc>
    </func>

    <func>
      <name name="size" arity="1" since=""/>
      <fsummary>Return the number of elements in a set.</fsummary>
      <desc>
        <p>Returns the number of elements in <c><anno>Set</anno></c>.</p>
      </desc>
    </func>

    <func>
      <name name="subtract" arity="2" since=""/>
      <fsummary>Return the difference of two <c>Sets</c>.</fsummary>
      <desc>
        <p>Returns only the elements of <c><anno>Set1</anno></c> that are not
          also elements of <c><anno>Set2</anno></c>.</p>
      </desc>
    </func>

    <func>
      <name name="to_list" arity="1" since=""/>
      <fsummary>Convert a <c>Set</c>into a list.</fsummary>
      <desc>
        <p>Returns the elements of <c><anno>Set</anno></c> as a list.
          The order of the returned elements is undefined.</p>
      </desc>
    </func>

    <func>
      <name name="union" arity="1" since=""/>
      <fsummary>Return the union of a list of <c>Sets</c>.</fsummary>
      <desc>
        <p>Returns the merged (union) set of the list of sets.</p>
      </desc>
    </func>

    <func>
      <name name="union" arity="2" since=""/>
      <fsummary>Return the union of two <c>Sets</c>.</fsummary>
      <desc>
        <p>Returns the merged (union) set of <c><anno>Set1</anno></c> and
          <c><anno>Set2</anno></c>.</p>
      </desc>
    </func>
  </funcs>

  <section>
    <title>See Also</title>
    <p><seeerl marker="gb_sets"><c>gb_sets(3)</c></seeerl>,
      <seeerl marker="ordsets"><c>ordsets(3)</c></seeerl></p>
  </section>
</erlref>