Skip to content

Commit

Permalink
Some more unification in R:Q methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Jun 8, 2017
1 parent 58c01c5 commit f0c3bf7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
47 changes: 26 additions & 21 deletions src/core/Rakudo/QuantHash.pm
Expand Up @@ -449,31 +449,34 @@ my class Rakudo::QuantHash {
)
}

method ADD-SET-TO-BAG(\elems,Mu \set --> Nil) {
nqp::if(
set && nqp::elems(set),
nqp::stmts(
(my $iter := nqp::iterator(set)),
nqp::while(
$iter,
nqp::if(
nqp::existskey(elems,nqp::iterkey_s(nqp::shift($iter))),
nqp::stmts(
(my $pair := nqp::atkey(elems,nqp::iterkey_s($iter))),
nqp::bindattr($pair,Pair,'$!value',
nqp::add_i(nqp::getattr($pair,Pair,'$!value'),1)
method ADD-SET-TO-BAG(\elems,Mu \set) {
nqp::stmts(
nqp::if(
set && nqp::elems(set),
nqp::stmts(
(my $iter := nqp::iterator(set)),
nqp::while(
$iter,
nqp::if(
nqp::existskey(elems,nqp::iterkey_s(nqp::shift($iter))),
nqp::stmts(
(my $pair := nqp::atkey(elems,nqp::iterkey_s($iter))),
nqp::bindattr($pair,Pair,'$!value',
nqp::add_i(nqp::getattr($pair,Pair,'$!value'),1)
)
),
nqp::bindkey(elems,nqp::iterkey_s($iter),
Pair.new(nqp::iterval($iter),1)
)
),
nqp::bindkey(elems,nqp::iterkey_s($iter),
Pair.new(nqp::iterval($iter),1)
)
)
)
)
),
elems
)
}

method MULTIPLY-BAG-TO-BAG(\elems,Mu \bag --> Nil) {
method MULTIPLY-BAG-TO-BAG(\elems,Mu \bag) {
nqp::stmts(
(my $iter := nqp::iterator(elems)),
nqp::if(
Expand Down Expand Up @@ -502,11 +505,12 @@ my class Rakudo::QuantHash {
$iter,
nqp::deletekey(elems,nqp::iterkey_s(nqp::shift($iter)))
)
)
),
elems
)
}

method MULTIPLY-SET-TO-BAG(\elems,Mu \set --> Nil) {
method MULTIPLY-SET-TO-BAG(\elems,Mu \set) {
nqp::stmts(
(my $iter := nqp::iterator(elems)),
nqp::if(
Expand All @@ -522,7 +526,8 @@ my class Rakudo::QuantHash {
$iter,
nqp::deletekey(elems,nqp::iterkey_s(nqp::shift($iter)))
)
)
),
elems
)
}

Expand Down
19 changes: 9 additions & 10 deletions src/core/set_operators.pm
Expand Up @@ -1031,9 +1031,8 @@ multi sub infix:<(.)>(Any $a) { $a.Bag }
multi sub infix:<(.)>(Setty:D $a, Setty:D $b) {
nqp::if(
(my $elems := $a.Bag.raw_hash) && nqp::elems($elems),
nqp::stmts(
nqp::create(Bag).SET-SELF(
Rakudo::QuantHash.MULTIPLY-SET-TO-BAG($elems,$b.raw_hash),
nqp::create(Bag).SET-SELF($elems)
),
bag()
)
Expand All @@ -1057,9 +1056,8 @@ multi sub infix:<(.)>(Baggy:D $a, Baggy:D $b) {
nqp::if(
(my $elems := Rakudo::QuantHash.BAGGY-CLONE-RAW($a.raw_hash))
&& nqp::elems($elems),
nqp::stmts(
nqp::create(Bag).SET-SELF(
Rakudo::QuantHash.MULTIPLY-BAG-TO-BAG($elems,$b.raw_hash),
nqp::create(Bag).SET-SELF($elems)
),
bag()
)
Expand Down Expand Up @@ -1107,13 +1105,14 @@ multi sub infix:<(+)>(MixHash:D $a) { $a.Mix }
multi sub infix:<(+)>(Any $a) { $a.Bag }

multi sub infix:<(+)>(Setty:D $a, Setty:D $b) {
nqp::stmts(
nqp::create(Bag).SET-SELF(
Rakudo::QuantHash.ADD-SET-TO-BAG(
(my $elems := nqp::create(Rakudo::Internals::IterationSet)),
$a.raw_hash
),
Rakudo::QuantHash.ADD-SET-TO-BAG($elems,$b.raw_hash),
nqp::create(Bag).SET-SELF($elems)
Rakudo::QuantHash.ADD-SET-TO-BAG(
nqp::create(Rakudo::Internals::IterationSet),
$a.raw_hash
),
$b.raw_hash
)
)
}

Expand Down

0 comments on commit f0c3bf7

Please sign in to comment.