Skip to content
Permalink
Browse files

Fewer scalar containers for (^)

Also some nqp -> ternary rewrite
  • Loading branch information...
lizmat committed May 9, 2019
1 parent 3cde10a commit 6df735b2dcfe1e98e0fb7b83c6b2c49c3c419dab
Showing with 54 additions and 62 deletions.
  1. +54 −62 src/core/set_symmetric_difference.pm6
@@ -3,23 +3,23 @@
# ⊖ set symmetric difference

proto sub infix:<(^)>(|) is pure {*}
multi sub infix:<(^)>() { set() }
multi sub infix:<(^)>(QuantHash:D $a) { $a } # Set/Bag/Mix
multi sub infix:<(^)>(Any $a) { $a.Set } # also for Iterable/Map
multi sub infix:<(^)>() { set() }
multi sub infix:<(^)>(QuantHash:D \a) { a } # Set/Bag/Mix
multi sub infix:<(^)>(Any \a) { a.Set } # also for Iterable/Map

multi sub infix:<(^)>(Setty:D $a, Setty:D $b) {
multi sub infix:<(^)>(Setty:D \a, Setty:D \b) {
nqp::if(
(my \araw := $a.RAW-HASH) && nqp::elems(araw),
(my \araw := a.RAW-HASH) && nqp::elems(araw),
nqp::if(
(my \braw := $b.RAW-HASH) && nqp::elems(braw),
(my \braw := b.RAW-HASH) && nqp::elems(braw),
nqp::stmts( # both are initialized
nqp::if(
nqp::islt_i(nqp::elems(araw),nqp::elems(braw)),
nqp::stmts( # $a smallest, iterate over it
nqp::stmts( # a smallest, iterate over it
(my $iter := nqp::iterator(araw)),
(my $elems := nqp::clone(braw))
),
nqp::stmts( # $b smallest, iterate over that
nqp::stmts( # b smallest, iterate over that
($iter := nqp::iterator(braw)),
($elems := nqp::clone(araw))
)
@@ -32,31 +32,31 @@ multi sub infix:<(^)>(Setty:D $a, Setty:D $b) {
nqp::bindkey($elems,nqp::iterkey_s($iter),nqp::iterval($iter))
)
),
nqp::create($a.WHAT).SET-SELF($elems)
nqp::create(a.WHAT).SET-SELF($elems)
),
$a # $b empty, so $a
a # b empty, so a
),
nqp::if( # $a empty, so $b
nqp::istype($a,Set), $b.Set, $b.SetHash
nqp::if( # a empty, so b
nqp::istype(a,Set), b.Set, b.SetHash
)
)
}
multi sub infix:<(^)>(Setty:D $a, Mixy:D $b) { $a.Mixy (^) $b }
multi sub infix:<(^)>(Setty:D $a, Baggy:D $b) { $a.Baggy (^) $b }
multi sub infix:<(^)>(Setty:D \a, Mixy:D \b) { a.Mixy (^) b }
multi sub infix:<(^)>(Setty:D \a, Baggy:D \b) { a.Baggy (^) b }

multi sub infix:<(^)>(Mixy:D $a, Mixy:D $b) {
multi sub infix:<(^)>(Mixy:D \a, Mixy:D \b) {
nqp::if(
(my \araw := $a.RAW-HASH) && nqp::elems(araw),
(my \araw := a.RAW-HASH) && nqp::elems(araw),
nqp::if(
(my \braw := $b.RAW-HASH) && nqp::elems(braw),
(my \braw := b.RAW-HASH) && nqp::elems(braw),
nqp::stmts( # both are initialized
nqp::if(
nqp::islt_i(nqp::elems(araw),nqp::elems(braw)),
nqp::stmts( # $a smallest, iterate over it
nqp::stmts( # a smallest, iterate over it
(my $iter := nqp::iterator(my $base := araw)),
(my $elems := nqp::clone(braw))
),
nqp::stmts( # $b smallest, iterate over that
nqp::stmts( # b smallest, iterate over that
($iter := nqp::iterator($base := braw)),
($elems := nqp::clone(araw))
)
@@ -89,38 +89,38 @@ multi sub infix:<(^)>(Mixy:D $a, Mixy:D $b) {
)
)
),
nqp::create($a.WHAT).SET-SELF($elems)
nqp::create(a.WHAT).SET-SELF($elems)
),
nqp::create($a.WHAT).SET-SELF( # $b empty, so $a
nqp::create(a.WHAT).SET-SELF( # b empty, so a
Rakudo::QuantHash.MIX-CLONE-ALL-POSITIVE(araw)
)
),
nqp::if(
(my \raw := $b.RAW-HASH) && nqp::elems(raw),
nqp::create($a.WHAT).SET-SELF( # $a empty, so $b
(my \raw := b.RAW-HASH) && nqp::elems(raw),
nqp::create(a.WHAT).SET-SELF( # a empty, so b
Rakudo::QuantHash.MIX-CLONE-ALL-POSITIVE(raw)
),
$a # $a and $b empty
a # a and b empty
)
)
}
multi sub infix:<(^)>(Mixy:D $a, Baggy:D $b) { $a (^) $b.Mix }
multi sub infix:<(^)>(Mixy:D $a, Setty:D $b) { $a (^) $b.Mix }
multi sub infix:<(^)>(Mixy:D \a, Baggy:D \b) { a (^) b.Mix }
multi sub infix:<(^)>(Mixy:D \a, Setty:D \b) { a (^) b.Mix }

multi sub infix:<(^)>(Baggy:D $a, Mixy:D $b) { $a.Mixy (^) $b }
multi sub infix:<(^)>(Baggy:D $a, Baggy:D $b) {
multi sub infix:<(^)>(Baggy:D \a, Mixy:D \b) { a.Mixy (^) b }
multi sub infix:<(^)>(Baggy:D \a, Baggy:D \b) {
nqp::if(
(my \araw := $a.RAW-HASH) && nqp::elems(araw),
(my \araw := a.RAW-HASH) && nqp::elems(araw),
nqp::if(
(my \braw := $b.RAW-HASH) && nqp::elems(braw),
(my \braw := b.RAW-HASH) && nqp::elems(braw),
nqp::stmts( # both are initialized
nqp::if(
nqp::islt_i(nqp::elems(araw),nqp::elems(braw)),
nqp::stmts( # $a smallest, iterate over it
nqp::stmts( # a smallest, iterate over it
(my $iter := nqp::iterator(my $base := araw)),
(my $elems := nqp::clone(braw))
),
nqp::stmts( # $b smallest, iterate over that
nqp::stmts( # b smallest, iterate over that
($iter := nqp::iterator($base := braw)),
($elems := nqp::clone(araw))
)
@@ -153,26 +153,26 @@ multi sub infix:<(^)>(Baggy:D $a, Baggy:D $b) {
nqp::bindkey($elems,nqp::iterkey_s($iter),nqp::iterval($iter))
)
),
nqp::create($a.WHAT).SET-SELF($elems)
nqp::create(a.WHAT).SET-SELF($elems)
),
$a # $b empty, so $a
a # b empty, so a
),
nqp::if( # $a empty, so $b
nqp::istype($a,Bag), $b.Bag, $b.BagHash
nqp::if( # a empty, so b
nqp::istype(a,Bag), b.Bag, b.BagHash
)
)
}
multi sub infix:<(^)>(Baggy:D $a, Setty:D $b) { $a (^) $b.Bag }
multi sub infix:<(^)>(Baggy:D \a, Setty:D \b) { a (^) b.Bag }

multi sub infix:<(^)>(Map:D $a, Map:D $b) {
multi sub infix:<(^)>(Map:D \a, Map:D \b) {
nqp::if(
nqp::elems((my \elems := Rakudo::QuantHash.COERCE-MAP-TO-SET($a))),
nqp::elems((my \elems := Rakudo::QuantHash.COERCE-MAP-TO-SET(a))),
nqp::if( # $a has elems
(my \iter :=
nqp::iterator(nqp::getattr(nqp::decont($b),Map,'$!storage'))),
nqp::iterator(nqp::getattr(nqp::decont(b),Map,'$!storage'))),
nqp::stmts(
nqp::if( # both have elems
nqp::eqaddr($b.keyof,Str(Any)),
nqp::eqaddr(b.keyof,Str(Any)),
nqp::while( # ordinary hash
iter,
nqp::if(
@@ -207,30 +207,22 @@ multi sub infix:<(^)>(Map:D $a, Map:D $b) {
),
nqp::create(Set).SET-SELF(elems) # nothing right, so make left
),
$b.Set # nothing left, coerce right
b.Set # nothing left, coerce right
)
}

multi sub infix:<(^)>(Failure:D $a, Any $b) { $a.throw }
multi sub infix:<(^)>(Any $a, Failure:D $b) { $b.throw }
multi sub infix:<(^)>(Any $a, Any $b) {
nqp::if(
nqp::istype($a,Mixy),
infix:<(^)>($a, $b.Mix),
nqp::if(
nqp::istype($b,Mixy),
infix:<(^)>($a.Mix, $b),
nqp::if(
nqp::istype($a,Baggy),
infix:<(^)>($a, $b.Bag),
nqp::if(
nqp::istype($b,Baggy),
infix:<(^)>($a.Bag, $b),
infix:<(^)>($a.Set, $b.Set)
)
)
)
)
multi sub infix:<(^)>(Failure:D \a, Any $) { a.throw }
multi sub infix:<(^)>(Any $, Failure:D \b) { b.throw }
multi sub infix:<(^)>(Any \a, Any \b) {
nqp::istype(a,Mixy)
?? a (^) b.Mix
!! nqp::istype(b,Mixy)
?? a.Mix (^) b
!! nqp::istype(a,Baggy)
?? a (^) b.Bag
!! nqp::istype(b,Baggy)
?? a.Bag (^) b
!! a.Set (^) b.Set
}

multi sub infix:<(^)>(**@p) {

0 comments on commit 6df735b

Please sign in to comment.
You can’t perform that action at this time.