Skip to content

Commit d670a01

Browse files
jhischwern
authored andcommitted
import Set-Scalar 1.12 from CPAN
git-cpan-module: Set-Scalar git-cpan-version: 1.12 git-cpan-authorid: JHI git-cpan-file: authors/id/J/JH/JHI/Set-Scalar-1.12.tar.gz
1 parent dff7bc6 commit d670a01

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Sat Oct 13 17:40:28 2001 Jarkko Hietaniemi <jhi@alpha.hut.fi>
2+
3+
* Add each() as a lighter weight way of iterating
4+
over sets, as suggested by Dave Lewis.
5+
6+
* Released as 1.12.
7+
18
Wed Oct 10 17:31:12 2001 Jarkko Hietaniemi <jhi@alpha.hut.fi>
29

310
* In boolean contexts the string representation of sets

MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ t/basic_overload.t
1515
t/boolean.t
1616
t/compare.t
1717
t/difference.t
18+
t/each.t
1819
t/intersection.t
1920
t/laws.t
2021
t/misc.t

lib/Set/Scalar.pm

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local $^W = 1;
55

66
use vars qw($VERSION @ISA);
77

8-
$VERSION = '1.11';
8+
$VERSION = '1.12';
99

1010
@ISA = qw(Set::Scalar::Real Set::Scalar::Null Set::Scalar::Base);
1111

@@ -168,13 +168,66 @@ These methods have operator overloads:
168168
169169
$cmp = $s <=> $t;
170170
171+
=head2 Boolean contexts
172+
171173
In Boolean contexts such as
172174
173175
if ($set) { ... }
176+
while ($set1 && $set2) { ... }
174177
175178
the size of the C<$set> is tested, so empty sets test as false,
176179
and non-empty sets as true.
177180
181+
=head2 Iterating
182+
183+
while (defined(my $e = $s->each)) { ... }
184+
185+
This is more memory-friendly than
186+
187+
for my $e ($s->elements) { ... }
188+
189+
which would first construct the full list of elements and then
190+
walk through it: the C<$s->each> handles one element at a time.
191+
192+
Analogously to using normal C<each(%hash)> in scalar context,
193+
using C<$s->each> has the following caveats:
194+
195+
=over 4
196+
197+
=item *
198+
199+
The elements are returned in (apparently) random order.
200+
So don't expect any particular order.
201+
202+
=item *
203+
204+
When no more elements remain C<undef> is returned. Since you may one
205+
day have elements named C<"0"> don't test just like this
206+
207+
while (my $e = $s->each) { ... } # WRONG
208+
209+
but instead like this
210+
211+
while (defined(my $e = $s->each)) { ... } # right
212+
213+
=item *
214+
215+
There is one iterator per one set which is shared by many
216+
element-accessing interfaces-- using the following will reset the
217+
iterator: elements(), insert(), members(), size(), unique(). insert()
218+
causes the iterator of the set being inserted (not the set being the
219+
target of insertion) becoming reset. unique() causes the iterators of
220+
all the participant sets becoming reset. B<The iterator getting reset
221+
most probably causes an endless loop.> So avoid doing that.
222+
223+
=item *
224+
225+
Modifying the set during the iteration may cause elements to be missed
226+
or duplicated, or in the worst case, an endless loop; so don't do
227+
that, either.
228+
229+
=back
230+
178231
=head1 AUTHOR
179232
180233
Jarkko Hietaniemi <jhi@iki.fi>

lib/Set/Scalar/Base.pm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ sub element {
144144
*has = \&element;
145145
*contains = \&element;
146146

147+
sub each {
148+
my $self = shift;
149+
150+
my ($k, $e) = each %{ $self->{'elements'} };
151+
152+
return $e;
153+
}
154+
147155
sub _clone {
148156
my $self = shift;
149157
my $original = shift;

t/each.t

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use Set::Scalar;
2+
3+
print "1..2\n";
4+
5+
my @a = ("a".."e",0);
6+
my $a = Set::Scalar->new(@a);
7+
8+
my $e;
9+
my %e;
10+
11+
while (defined($e = $a->each)) {
12+
print "# e = $e\n";
13+
$e{$e}++;
14+
}
15+
16+
print "not " if defined $e;
17+
print "ok 1\n";
18+
19+
my $n;
20+
21+
for my $e (@a) {
22+
$n++ if exists $e{$e} && $e{$e} == 1;
23+
}
24+
25+
print "not " unless $n == @a && keys %e == @a;
26+
print "ok 2\n";
27+
28+
29+
30+

0 commit comments

Comments
 (0)