Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:rakudo/rakudo
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed Mar 19, 2010
2 parents a0064d2 + c05da93 commit e252983
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/Makefile.in
Expand Up @@ -172,6 +172,7 @@ CORE_SOURCES = \
src/core/Any-num.pm \
src/core/Any-str.pm \
src/core/Seq.pm \
src/core/Set.pm \
src/core/Array.pm \
src/core/Int.pm \
src/core/Iterable.pm \
Expand Down
158 changes: 158 additions & 0 deletions src/core/Set.pm
@@ -0,0 +1,158 @@
class Set does Associative {
# We could use a hash here, but right now hash keys coerce to Str,
# so instead let's use an array and &uniq for the time being.
has @!elems;

multi method new(@elems) {
self.bless(self.CREATE, :elems( uniq @elems ));
}
multi method new(*@elems) {
self.bless(self.CREATE, :elems( uniq @elems ));
}
multi method new(%elems) {
self.bless(self.CREATE, :elems( %elems.keys ));
}
multi method new(Set $set) {
$set;
}

sub contains(@array, $value) {
for @array {
if $value === $_ {
return True;
}
}
return False;
}

method keys() { @!elems }
method values() { True xx +@!elems }
method elems() { +@!elems }
method exists($elem) { contains(@!elems, $elem) }

method Num() { +self.elems }
method Bool() { ?self.elems }

multi method union(@otherset) {
self.new((@!elems, @otherset));
}
multi method union(%otherset) {
self.union(%otherset.keys);
}

multi method intersection(@otherset) {
self.new(grep { contains(@otherset, $_) }, @!elems);
}
multi method intersection(%otherset) {
self.intersection(%otherset.keys);
}

multi method difference(%otherset) {
self.difference(%otherset.keys);
}
multi method difference(@otherset) {
self.new(grep { !contains(@otherset, $_) }, @!elems);
}

multi method subsetorequal(@otherset) {
?contains(@otherset, all(@!elems));
}
multi method subsetorequal(%otherset) {
self.subsetorequal(%otherset.keys);
}

multi method supersetorequal(@otherset) {
?contains(@!elems, all(@otherset));
}
multi method supersetorequal(%otherset) {
self.supersetorequal(%otherset.keys);
}

method equal($otherset) {
+self == +$otherset && self.subsetorequal($otherset);
}

method subset($otherset) {
+self < +Set.new($otherset) && self.subsetorequal($otherset);
}

method superset($otherset) {
+self > +Set.new($otherset) && self.supersetorequal($otherset);
}

method perl() {
'Set.new(' ~ join(', ', map { .perl }, @!elems) ~ ')';
}
}

multi sub infix:<>(Set $a, %b) { $a.union(%b) }
multi sub infix:<>( %a, %b) { Set.new( %a).union(%b) }
multi sub infix:<>( @a, %b) { Set.new(|@a).union(%b) }
multi sub infix:<>( @a, @b) { Set.new(|@a).union(@b) }

multi sub infix:<>(Set $a, %b) { $a.intersection(%b) }
multi sub infix:<>( %a, %b) { Set.new( %a).intersection(%b) }
multi sub infix:<>( @a, %b) { Set.new(|@a).intersection(%b) }
multi sub infix:<>( @a, @b) { Set.new(|@a).intersection(@b) }

multi sub infix:<>(Set $a, %b) { $a.difference(%b) }
multi sub infix:<>( %a, %b) { Set.new( %a).difference(%b) }
multi sub infix:<>( @a, %b) { Set.new(|@a).difference(%b) }
multi sub infix:<>( @a, @b) { Set.new(|@a).difference(@b) }

multi sub infix:<>(Set $a, %b) { $a.subsetorequal(%b) }
multi sub infix:<>( %a, %b) { Set.new( %a).subsetorequal(%b) }
multi sub infix:<>( @a, %b) { Set.new(|@a).subsetorequal(%b) }
multi sub infix:<>( @a, @b) { Set.new(|@a).subsetorequal(@b) }

multi sub infix:<>(Set $a, %b) { $a.supersetorequal(%b) }
multi sub infix:<>( %a, %b) { Set.new( %a).supersetorequal(%b) }
multi sub infix:<>( @a, %b) { Set.new(|@a).supersetorequal(%b) }
multi sub infix:<>( @a, @b) { Set.new(|@a).supersetorequal(@b) }

multi sub infix:<>(Set $a, %b) { $a.subset(%b) }
multi sub infix:<>( %a, %b) { Set.new( %a).subset(%b) }
multi sub infix:<>( @a, %b) { Set.new(|@a).subset(%b) }
multi sub infix:<>( @a, @b) { Set.new(|@a).subset(@b) }

multi sub infix:<>(Set $a, %b) { $a.superset(%b) }
multi sub infix:<>( %a, %b) { Set.new( %a).superset(%b) }
multi sub infix:<>( @a, %b) { Set.new(|@a).superset(%b) }
multi sub infix:<>( @a, @b) { Set.new(|@a).superset(@b) }

multi sub infix:<(|)>(Set $a, %b) { $a.union(%b) }
multi sub infix:<(|)>( %a, %b) { Set.new( %a).union(%b) }
multi sub infix:<(|)>( @a, %b) { Set.new(|@a).union(%b) }
multi sub infix:<(|)>( @a, @b) { Set.new(|@a).union(@b) }

multi sub infix:<(&)>(Set $a, %b) { $a.intersection(%b) }
multi sub infix:<(&)>( %a, %b) { Set.new( %a).intersection(%b) }
multi sub infix:<(&)>( @a, %b) { Set.new(|@a).intersection(%b) }
multi sub infix:<(&)>( @a, @b) { Set.new(|@a).intersection(@b) }

multi sub infix:<(-)>(Set $a, %b) { $a.difference(%b) }
multi sub infix:<(-)>( %a, %b) { Set.new( %a).difference(%b) }
multi sub infix:<(-)>( @a, %b) { Set.new(|@a).difference(%b) }
multi sub infix:<(-)>( @a, @b) { Set.new(|@a).difference(@b) }

multi sub infix:<(<=)>(Set $a, %b) { $a.subsetorequal(%b) }
multi sub infix:<(<=)>( %a, %b) { Set.new( %a).subsetorequal(%b) }
multi sub infix:<(<=)>( @a, %b) { Set.new(|@a).subsetorequal(%b) }
multi sub infix:<(<=)>( @a, @b) { Set.new(|@a).subsetorequal(@b) }

multi sub infix(>=)»(Set $a, %b) { $a.supersetorequal(%b) }
multi sub infix(>=)»( %a, %b) { Set.new( %a).supersetorequal(%b) }
multi sub infix(>=)»( @a, %b) { Set.new(|@a).supersetorequal(%b) }
multi sub infix(>=)»( @a, @b) { Set.new(|@a).supersetorequal(@b) }

multi sub infix:<(<)>(Set $a, %b) { $a.subset(%b) }
multi sub infix:<(<)>( %a, %b) { Set.new( %a).subset(%b) }
multi sub infix:<(<)>( @a, %b) { Set.new(|@a).subset(%b) }
multi sub infix:<(<)>( @a, @b) { Set.new(|@a).subset(@b) }

multi sub infix(>)»(Set $a, %b) { $a.superset(%b) }
multi sub infix(>)»( %a, %b) { Set.new( %a).superset(%b) }
multi sub infix(>)»( @a, %b) { Set.new(|@a).superset(%b) }
multi sub infix(>)»( @a, @b) { Set.new(|@a).superset(@b) }

# vim: ft=perl6

0 comments on commit e252983

Please sign in to comment.