1Set::Scalar(3)        User Contributed Perl Documentation       Set::Scalar(3)
2
3
4

NAME

6       Set::Scalar - basic set operations
7

SYNOPSIS

9           use Set::Scalar;
10           $s = Set::Scalar->new;
11           $s->insert('a', 'b');
12           $s->delete('b');
13           $t = Set::Scalar->new('x', 'y', $z);
14

DESCRIPTION

16   Creating
17           $s = Set::Scalar->new;
18           $s = Set::Scalar->new(@members);
19
20           $t = $s->clone;
21           $t = $s->copy;         # Clone of clone.
22           $t = $s->empty_clone;  # Like clone() but with no members.
23
24   Modifying
25           $s->insert(@members);
26           $s->delete(@members);
27           $s->invert(@members);  # Insert if hasn't, delete if has.
28
29           $s->clear;  # Removes all the elements.
30
31       Note that clear() only releases the memory used by the set to be reused
32       by Perl; it will not reduce the overall memory use.
33
34   Displaying
35           print $s, "\n";
36
37       The display format of a set is the members of the set separated by
38       spaces and enclosed in parentheses (), for example:
39
40          my $s = Set::Scalar->new();
41          $s->insert("a".."e");
42          print $s, "\n";
43
44       will output
45
46          a b c d e
47
48       You can even display recursive sets.
49
50       See "Customising Display" for customising the set display.
51
52   Querying
53       Assuming a set $s:
54
55           @members  = $s->members;
56           @elements = $s->elements;  # Alias for members.
57
58           @$s  # Overloaded alias for members.
59
60           $size = $s->size;  # The number of members.
61
62           $s->has($m)        # Return true if has that member.
63           $s->contains($m)   # Alias for has().
64
65           if ($s->has($member)) { ... }
66
67           $s->member($m)     # Returns the member if has that member.
68           $s->element($m)    # Alias for member.
69
70           $s->is_null        # Returns true if the set is empty.
71           $s->is_empty       # Alias for is_null.
72
73           $s->is_universal   # Returns true if the set is universal.
74
75           $s->null           # The null set.
76           $s->empty          # Alias for null.
77           $s->universe       # The universe of the set.
78
79   Deriving
80           $u = $s->union($t);
81           $i = $s->intersection($t);
82           $d = $s->difference($t);
83           $e = $s->symmetric_difference($t);
84           $v = $s->unique($t);
85           $c = $s->complement;
86
87       These methods have operator overloads:
88
89           $u = $s + $t;  # union
90           $i = $s * $t;  # intersection
91           $d = $s - $t;  # difference
92           $e = $s % $t;  # symmetric_difference
93           $v = $s / $t;  # unique
94           $c = -$s;      # complement
95
96       Both the "symmetric_difference" and "unique" are symmetric on all their
97       arguments.  For two sets they are identical but for more than two sets
98       beware: "symmetric_difference" returns true for elements that are in an
99       odd number (1, 3, 5, ...) of sets, "unique" returns true for elements
100       that are in one set.
101
102       Some examples of the various set differences below (the _ is just used
103       to align the elements):
104
105           set or difference                   value
106
107           $a                                  (a b c d e _ _ _ _)
108           $b                                  (_ _ c d e f g _ _)
109           $c                                  (_ _ _ _ e f g h i)
110
111           $a->difference($b)                  (a b _ _ _ _ _ _ _)
112           $a->symmetric_difference($b)        (a b _ _ _ f g _ _)
113           $a->unique($b)                      (a b _ _ _ f g _ _)
114
115           $b->difference($a)                  (_ _ _ _ _ f g _ _)
116           $b->symmetric_difference($a)        (a b _ _ _ f g _ _)
117           $b->unique($a)                      (a b _ _ _ f g _ _)
118
119           $a->difference($b, $c)              (a b _ _ _ _ _ _ _)
120           $a->symmetric_difference($b, $c)    (a b _ _ e _ _ h i)
121           $a->unique($b, $c)                  (a b _ _ _ _ _ h i)
122
123   Comparing
124           $eq = $s->is_equal($t);
125           $dj = $s->is_disjoint($t);
126           $pi = $s->is_properly_intersecting($t);
127           $ps = $s->is_proper_subset($t);
128           $pS = $s->is_proper_superset($t);
129           $is = $s->is_subset($t);
130           $iS = $s->is_superset($t);
131
132           $cmp = $s->compare($t);
133
134       The "compare" method returns a string from the following list: "equal",
135       "disjoint", "proper subset", "proper superset", "proper intersect", and
136       in future (once I get around implementing it), "disjoint universes".
137
138       These methods have operator overloads:
139
140           $eq = $s == $t;  # is_equal
141           $dj = $s != $t;  # is_disjoint
142           # No operator overload for is_properly_intersecting.
143           $ps = $s < $t;   # is_proper_subset
144           $pS = $s > $t;   # is_proper_superset
145           $is = $s <= $t;  # is_subset
146           $iS = $s >= $t;  # is_superset
147
148           $cmp = $s <=> $t;
149
150   Boolean contexts
151       In Boolean contexts such as
152
153           if ($set) { ... }
154           while ($set1 && $set2) { ... }
155
156       the size of the $set is tested, so empty sets test as false, and non-
157       empty sets as true.
158
159   Iterating
160           while (defined(my $e = $s->each)) { ... }
161
162       This is more memory-friendly than
163
164           for my $e ($s->elements) { ... }
165
166       which would first construct the full list of elements and then walk
167       through it: the "$s->each" handles one element at a time.
168
169       Analogously to using normal each(%hash) in scalar context, using
170       "$s->each" has the following caveats:
171
172       •   The elements are returned in (apparently) random order.  So don't
173           expect any particular order.
174
175       •   When no more elements remain "undef" is returned.  Since you may
176           one day have elements named 0 don't test just like this
177
178               while (my $e = $s->each) { ... }           # WRONG!
179
180           but instead like this
181
182               while (defined(my $e = $s->each)) { ... }  # Right.
183
184           (An "undef" as a set element doesn't really work, you get "".)
185
186       •   There is one iterator per one set which is shared by many element-
187           accessing interfaces-- using the following will reset the iterator:
188           elements(), insert(), members(), size(), unique().  insert() causes
189           the iterator of the set being inserted (not the set being the
190           target of insertion) becoming reset.  unique() causes the iterators
191           of all the participant sets becoming reset.  The iterator getting
192           reset most probably causes an endless loop. So avoid doing that.
193
194           For delete() the story is a little bit more complex: it depends on
195           what element you are deleting and on the version of Perl.  On
196           modern Perls you can safely delete the element you just deleted.
197           But deleting random elements can affect the iterator, so beware.
198
199       •   Modifying the set during the iteration may cause elements to be
200           missed or duplicated, or in the worst case, an endless loop; so
201           don't do that, either.
202
203   Cartesian Product and Power Set
204       •   Cartesian product is a product of two or more sets.  For two sets,
205           it is the set consisting of ordered pairs of members from each set.
206           For example for the sets
207
208             (a b)
209             (c d e)
210
211           The Cartesian product of the above is the set
212
213             ([a, c] [a, d] [a, e] [b, c] [b, d] [b, e])
214
215           The [,] notation is for the ordered pairs, which sets are not.
216           This means two things: firstly, that [e, b] is not in the above
217           Cartesian product, and secondly, [b, b] is a possibility:
218
219             (a b)
220             (b c e)
221
222             ([a, b] [a, c] [a, e] [b, b] [b, c] [b, d])
223
224           For example:
225
226             my $a = Set::Scalar->new(1..2);
227             my $b = Set::Scalar->new(3..5);
228             my $c = $a->cartesian_product($b);  # As an object method.
229             my $d = Set::Scalar->cartesian_product($a, $b);  # As a class method.
230
231           The $c and $d will be of the same class as $a.  The members of $c
232           and $c in the above will be anonymous arrays (array references),
233           not sets, since sets wouldn't be able to represent the ordering or
234           that a member can be present more than once.  Also note that since
235           the members of the input sets are unordered, the ordered pairs
236           themselves are unlikely to be in any particular order.
237
238           If you don't want to construct the Cartesian product set, you can
239           construct an iterator and call it while it returns more members:
240
241              my $iter = Set::Scalar->cartesian_product_iterator($a, $b, $c);
242              while (my @m = $iter->()) {
243                process(@m);
244              }
245
246       •   Power set is the set of all the subsets of a set.  If the set has N
247           members, its power set has 2**N members.  For example for the set
248
249               (a b c)
250
251           size 3, its power set is
252
253               (() (a) (b) (c) (a b) (a c) (b c) (a b c))
254
255           size 8.  Note that since the elements of the power set are sets,
256           they are unordered, and therefore (b c) is equal to (c b).  For
257           example:
258
259               my $a = Set::Scalar->new(1..3);
260               my $b = $a->power_set;               # As an object method.
261               my $c = Set::Scalar->power_set($a);  # As a class method.
262
263           Even the empty set has a power set, of size one.
264
265           If you don't want to construct the power set, you can construct an
266           iterator and call it until it returns no more members:
267
268              my $iter = Set::Scalar->power_set_iterator($a);
269              my @m;
270              do {
271                @m = $iter->();
272                process(@m);
273              } while (@m);
274
275   Customising Display
276       If you want to customise the display routine you will have to modify
277       the "as_string" callback.  You can modify it either for all sets by
278       using as_string_callback() as a class method:
279
280           my $class_callback = sub { ... };
281
282           Set::Scalar->as_string_callback($class_callback);
283
284       or for specific sets by using as_string_callback() as an object method:
285
286           my $callback = sub  { ... };
287
288           $s1->as_string_callback($callback);
289           $s2->as_string_callback($callback);
290
291       The anonymous subroutine gets as its first (and only) argument the set
292       to display as a string.  For example to display the set $s as
293       "a-b-c-d-e" instead of "(a b c d e)"
294
295           $s->as_string_callback(sub{join("-",sort $_[0]->elements)});
296
297       If called without an argument, the current callback is returned.
298
299       If called as a class method with undef as the only argument, the
300       original callback (the one returning "(a b c d e)") for all the sets is
301       restored, or if called for a single set the callback is removed (and
302       the callback for all the sets will be used).
303

CAVEATS

305       The first priority of Set::Scalar is to be a convenient interface to
306       sets.  While not designed to be slow or big, neither has it been
307       designed to be fast or compact.
308
309       Using references (or objects) as set members has not been extensively
310       tested.  The desired semantics are not always clear: what should happen
311       when the elements behind the references change? Especially unclear is
312       what should happen when the objects start having their own
313       stringification overloads.
314

SEE ALSO

316       Set::Bag for bags (multisets, counted sets), and Bit::Vector for fast
317       set operations (you have to take care of the element name to bit number
318       and back mappings yourself), or Set::Infinite for sets of intervals,
319       and many more.  CPAN is your friend.
320

AUTHOR

322       Jarkko Hietaniemi <jhi@iki.fi> David Oswald <davido@cpan.org> is the
323       current maintainer.  The GitHub repo is at
324       <https://github.com/daoswald/Set-Scalar>
325
327       Copyright 2001,2002,2003,2004,2005,2007,2009,2013 by Jarkko Hietaniemi
328
329       This library is free software; you can redistribute it and/or modify it
330       under the same terms as Perl itself.
331
332
333
334perl v5.36.0                      2023-01-20                    Set::Scalar(3)
Impressum