Skip to content

Commit

Permalink
Simplify Setty/Baggy.ACCEPTS
Browse files Browse the repository at this point in the history
Now that we have a set equality op, we can use that.

Also fix up the Setty/Baggy infix:<eqv> check on object identity:
the should always decont before checking.
  • Loading branch information
lizmat committed May 30, 2020
1 parent 701f4cf commit dbd69ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 72 deletions.
43 changes: 5 additions & 38 deletions src/core.c/Baggy.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,10 @@ my role Baggy does QuantHash {
other.^does(self)
}
multi method ACCEPTS(Baggy:D: Baggy:D \other --> Bool:D) {
nqp::hllbool(
nqp::unless(
nqp::eqaddr(self,other),
nqp::if( # not same object
(my \araw := $!elems) && nqp::elems(araw),
nqp::if( # something on left
nqp::isconcrete(my \braw := other.RAW-HASH)
&& nqp::elems(braw),
nqp::if( # something on both sides
nqp::iseq_i(nqp::elems(araw),nqp::elems(braw)),
nqp::stmts( # same size
(my \iter := nqp::iterator(araw)),
nqp::while(
iter,
nqp::unless(
nqp::getattr(
nqp::ifnull(
nqp::atkey(braw,nqp::iterkey_s(nqp::shift(iter))),
BEGIN nqp::p6bindattrinvres( # virtual Pair with 0
nqp::create(Pair),Pair,'$!value',0)
),Pair,'$!value')
== nqp::getattr(nqp::iterval(iter),Pair,'$!value'),
return False # missing/different: we're done
)
),
True # all keys identical/same value
)
)
),
# true -> both empty
nqp::isfalse(
(my \raw := other.RAW-HASH) && nqp::elems(raw)
)
)
)
)
self (==) other
}
multi method ACCEPTS(Baggy:D: \other --> Bool:D) {
self.ACCEPTS(other.Bag)
self (==) other.Bag
}

multi method AT-KEY(Baggy:D: \k) { # exception: ro version for Bag/Mix
Expand Down Expand Up @@ -738,7 +703,9 @@ my role Baggy does QuantHash {

multi sub infix:<eqv>(Baggy:D \a, Baggy:D \b --> Bool:D) {
nqp::hllbool(
nqp::eqaddr(a,b) || (nqp::eqaddr(a.WHAT,b.WHAT) && a.ACCEPTS(b))
nqp::eqaddr(nqp::decont(a),nqp::decont(b))
|| (nqp::eqaddr(a.WHAT,b.WHAT) && a.ACCEPTS(b))
)
}

# vim: ft=perl6 expandtab sw=4
44 changes: 10 additions & 34 deletions src/core.c/Setty.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -105,40 +105,15 @@ my role Setty does QuantHash {
multi method hash(Setty:D: --> Hash:D) { self!HASHIFY(Bool) }
multi method Hash(Setty:D: --> Hash:D) { self!HASHIFY(Any) }

multi method ACCEPTS(Setty:U: \other) { other.^does(self) }
multi method ACCEPTS(Setty:D: Setty:D \other) {
nqp::hllbool(
nqp::unless(
nqp::eqaddr(self,other),
nqp::if( # not same object
$!elems && nqp::elems($!elems),
nqp::if( # something on left
nqp::isconcrete(my $oraw := other.RAW-HASH)
&& nqp::elems($oraw),
nqp::if( # something on both sides
nqp::iseq_i(nqp::elems($!elems),nqp::elems($oraw)),
nqp::stmts( # same size
(my $iter := nqp::iterator($!elems)),
nqp::while(
$iter,
nqp::unless(
nqp::existskey($oraw,nqp::iterkey_s(nqp::shift($iter))),
return False # missing key, we're done
)
),
True # all keys identical
)
)
),
# true -> both empty
nqp::isfalse(
($oraw := other.RAW-HASH) && nqp::elems($oraw)
)
)
)
)
multi method ACCEPTS(Setty:U: \other --> Bool:D) {
other.^does(self)
}
multi method ACCEPTS(Setty:D: Setty:D \other --> Bool:D) {
self (==) other
}
multi method ACCEPTS(Setty:D: \other --> Bool:D) {
self (==) other.Set
}
multi method ACCEPTS(Setty:D: \other) { self.ACCEPTS(other.Set) }

multi method Str(Setty:D $ : --> Str:D) {
nqp::join(" ",Rakudo::QuantHash.RAW-VALUES-MAP(self, *.Str))
Expand Down Expand Up @@ -301,7 +276,8 @@ my role Setty does QuantHash {

multi sub infix:<eqv>(Setty:D \a, Setty:D \b --> Bool:D) {
nqp::hllbool(
nqp::eqaddr(a,b) || (nqp::eqaddr(a.WHAT,b.WHAT) && a.ACCEPTS(b))
nqp::eqaddr(nqp::decont(a),nqp::decont(b))
|| (nqp::eqaddr(a.WHAT,b.WHAT) && a.ACCEPTS(b))
)
}

Expand Down

0 comments on commit dbd69ba

Please sign in to comment.