Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add :view named parameter to KeySet.Set and KeyBag.Bag coercers
This is a test to see whether this makes sense or not: basically, whenever
we coerce a KeySet to a Set, or a KeyBag to a Bag, and the :view parameter
is specified, we create an object using the same underlying :elems hash.
This prevents wasteful copying e.g. inside the (&) and other operators.
  • Loading branch information
lizmat committed Sep 9, 2013
1 parent 437e799 commit 8be54da
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/core/Baggy.pm
Expand Up @@ -214,8 +214,8 @@ my role Baggy does Associative {
}

only sub infix:<(.)>(**@p) {
my $set = Set.new: @p.map(*.Set.keys);
my @bags = @p.map(*.Bag);
my $set = Set.new: @p.map(*.Set(:view).keys);
my @bags = @p.map(*.Bag(:view));
Bag.new-fp($set.map({ ; $_ => [*] @bags>>.{$_} }));
}
# U+228D MULTISET MULTIPLICATION
Expand All @@ -224,8 +224,8 @@ only sub infix:<<"\x228D">>(|p) {
}

only sub infix:<(+)>(**@p) {
my $set = Set.new: @p.map(*.Set.keys);
my @bags = @p.map(*.Bag);
my $set = Set.new: @p.map(*.Set(:view).keys);
my @bags = @p.map(*.Bag(:view));
Bag.new-fp($set.map({ ; $_ => [+] @bags>>.{$_} }));
}
# U+228E MULTISET UNION
Expand Down
11 changes: 10 additions & 1 deletion src/core/KeyBag.pm
@@ -1,5 +1,14 @@
my class KeyBag does Baggy {

method Bag { Bag.new-fp(nqp::getattr(self, KeyBag, '%!elems').values) }
method Bag (:$view) {
if $view {
my $bag := nqp::create(Bag);
$bag.BUILD( :elems(nqp::getattr(self, KeyBag, '%!elems')) );
$bag;
}
else {
Bag.new-fp(nqp::getattr(self, KeyBag, '%!elems').values);
}
}
method KeyBag { self }
}
12 changes: 11 additions & 1 deletion src/core/KeySet.pm
@@ -1,5 +1,15 @@
my class KeySet does Setty {

method Set { Set.new(self.keys) }
method Set (:$view) {
if $view {
my $set := nqp::create(Set);
$set.BUILD( :elems(nqp::getattr(self, KeySet, '%!elems')) );
$set;
}
else {
Set.new(self.keys);
}
}

method KeySet { self }
}
8 changes: 4 additions & 4 deletions src/core/Setty.pm
Expand Up @@ -148,7 +148,7 @@ only sub infix:<<"\x222A">>(|p) {
only sub infix:<(&)>(**@p) {
if @p.first(Baggy) {
my $keybag = @p ?? @p.shift.KeyBag !! KeyBag.new;
for @p.map(*.Bag) -> $bag {
for @p.map(*.Bag(:view)) -> $bag {
$bag{$_}
?? $keybag{$_} min= $bag{$_}
!! $keybag.delete($_)
Expand All @@ -158,7 +158,7 @@ only sub infix:<(&)>(**@p) {
}
else {
my $keyset = @p ?? @p.shift.KeySet !! KeySet.new;
for @p.map(*.Set) -> $set {
for @p.map(*.Set(:view)) -> $set {
$set{$_} || $keyset.delete($_) for $keyset.keys;
}
Set.new($keyset.keys);
Expand All @@ -172,12 +172,12 @@ only sub infix:<<"\x2229">>(|p) {
only sub infix:<(-)>(**@p) {
return set() unless @p;
if @p[0] ~~ Baggy {
my @bags = @p.map(*.Bag);
my @bags = @p.map(*.Bag(:view));
my $base = @bags.shift;
Bag.new-fp($base.keys.map({ ; $_ => $base{$_} - [+] @bags>>.{$_} }));
}
else {
my @sets = @p.map(*.Set);
my @sets = @p.map(*.Set(:view));
my $base = @sets.shift;
Set.new: $base.keys.grep(* !(elem) @sets.any );
}
Expand Down

0 comments on commit 8be54da

Please sign in to comment.