Skip to content
Permalink
Browse files

Allow parameterization of Mix/MixHash

  • Loading branch information...
lizmat committed Jan 14, 2019
1 parent 1c101a0 commit bcc8054a4d79952809e073169d5116aba5469dd2
Showing with 52 additions and 26 deletions.
  1. +4 −4 src/core/Iterable.pm6
  2. +9 −3 src/core/Mix.pm6
  3. +8 −3 src/core/MixHash.pm6
  4. +2 −2 src/core/Mixy.pm6
  5. +29 −14 src/core/Rakudo/QuantHash.pm6
@@ -137,13 +137,13 @@ my role Iterable {

sub MIXIFY(\iterable, \type) {
nqp::if(
(my $iterator := iterable.flat.iterator).is-lazy,
(my \iterator := iterable.flat.iterator).is-lazy,
Failure.new(X::Cannot::Lazy.new(:action<coerce>,:what(type.^name))),
nqp::if(
nqp::elems(my $elems := Rakudo::QuantHash.ADD-PAIRS-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet),$iterator
nqp::elems(my \elems := Rakudo::QuantHash.ADD-PAIRS-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet),iterator,Mu
)),
nqp::create(type).SET-SELF($elems),
nqp::create(type).SET-SELF(elems),
nqp::if(
nqp::eqaddr(type,Mix),
mix(),
@@ -3,20 +3,26 @@ my class Mix does Mixy {
has Real $!total;
has Real $!total-positive;

method ^parameterize(Mu \base, Mu \type) {
Rakudo::Internals.PARAMETERIZE-KEYOF(base,type)
}

#--- interface methods
multi method STORE(Mix:D: *@pairs, :$INITIALIZE! --> Mix:D) {
(my $iterator := @pairs.iterator).is-lazy
(my \iterator := @pairs.iterator).is-lazy
?? Failure.new(
X::Cannot::Lazy.new(:action<initialize>,:what(self.^name)))
!! self.SET-SELF(Rakudo::QuantHash.ADD-PAIRS-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet), $iterator))
nqp::create(Rakudo::Internals::IterationSet),iterator,self.keyof
))
}
multi method STORE(Mix:D: \objects, \values, :$INITIALIZE! --> Mix:D) {
self.SET-SELF(
Rakudo::QuantHash.ADD-OBJECTS-VALUES-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet),
objects.iterator,
values.iterator
values.iterator,
self.keyof
)
)
}
@@ -1,16 +1,20 @@
my class MixHash does Mixy {

method ^parameterize(Mu \base, Mu \type) {
Rakudo::Internals.PARAMETERIZE-KEYOF(base,type)
}

#--- interface methods
method total() { Rakudo::QuantHash.MIX-TOTAL($!elems) }
method !total-positive() { Rakudo::QuantHash.MIX-TOTAL-POSITIVE($!elems) }

multi method STORE(MixHash:D: *@pairs --> MixHash:D) {
nqp::if(
(my $iterator := @pairs.iterator).is-lazy,
(my \iterator := @pairs.iterator).is-lazy,
Failure.new(X::Cannot::Lazy.new(:action<initialize>,:what(self.^name))),
self.SET-SELF(
Rakudo::QuantHash.ADD-PAIRS-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet), $iterator
nqp::create(Rakudo::Internals::IterationSet),iterator,self.keyof
)
)
)
@@ -20,7 +24,8 @@ my class MixHash does Mixy {
Rakudo::QuantHash.ADD-OBJECTS-VALUES-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet),
objects.iterator,
values.iterator
values.iterator,
self.keyof
)
)
}
@@ -74,11 +74,11 @@ my role Mixy does Baggy {
#--- object creation methods
method new-from-pairs(Mixy:_: *@pairs --> Mixy:D) {
nqp::if(
(my $iterator := @pairs.iterator).is-lazy,
(my \iterator := @pairs.iterator).is-lazy,
Failure.new(X::Cannot::Lazy.new(:action<coerce>,:what(self.^name))),
nqp::create(self).SET-SELF(
Rakudo::QuantHash.ADD-PAIRS-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet),$iterator
nqp::create(Rakudo::Internals::IterationSet),iterator,self.keyof
)
)
)
@@ -1221,9 +1221,24 @@ my class Rakudo::QuantHash {
)
}

# bind the given which/object/value to the given IterationSet,
# check object for given type
method BIND-TO-TYPED-MIX(
\elems, Mu \which, Mu \object, Real:D \value, Mu \type
--> Nil) {
nqp::if(
nqp::istype(object,type),
nqp::bindkey(elems,which,Pair.new(object,value)),
X::TypeCheck::Binding.new(
got => object.WHAT,
expected => type
).throw
)
}

# Add to given IterationSet with mixy semantics the values of the given
# iterator while checking for Pairs with numeric values.
method ADD-PAIRS-TO-MIX(\elems,Mu \iterator) is raw {
method ADD-PAIRS-TO-MIX(\elems, Mu \iterator, Mu \type) is raw {
nqp::stmts(
nqp::until(
nqp::eqaddr(
@@ -1261,15 +1276,10 @@ my class Rakudo::QuantHash {
nqp::bindattr($pair,Pair,'$!value',$value), # non-zero
nqp::deletekey(elems,$which) # zero
),
nqp::bindkey( # new, create new Pair
elems,
$which,
nqp::p6bindattrinvres(
nqp::clone($pulled),
Pair,
'$!value',
$value
)
self.BIND-TO-TYPED-MIX( # new, create new Pair
elems, $which,
nqp::getattr($pulled,Pair,'$!key'),
$value,type
)
)
)
@@ -1289,8 +1299,9 @@ my class Rakudo::QuantHash {
nqp::getattr($pair,Pair,'$!value') + 1
)
),
nqp::bindkey( # new, create new Pair
elems,$which,Pair.new($pulled,1))
self.BIND-TO-TYPED-MIX( # new, create new Pair
elems, $which, $pulled, 1, type
)
)
)
),
@@ -1301,7 +1312,9 @@ my class Rakudo::QuantHash {
# Add to given IterationSet with mixy semantics the values of the two
# given iterators where the first iterator supplies objects, and the
# second supplies values.
method ADD-OBJECTS-VALUES-TO-MIX(\elems,Mu \objects, Mu \values) is raw {
method ADD-OBJECTS-VALUES-TO-MIX(
\elems, Mu \objects, Mu \values, Mu \type
) is raw {
nqp::until(
nqp::eqaddr((my \object := objects.pull-one),IterationEnd),
nqp::if(
@@ -1316,7 +1329,9 @@ my class Rakudo::QuantHash {
nqp::istype(nqp::bind(value,value.Real),Real),
nqp::if(
value,
nqp::bindkey(elems,object.WHICH,Pair.new(object,value))
self.BIND-TO-TYPED-MIX(
elems, object.WHICH, object, value, type
)
),
value.throw
)

0 comments on commit bcc8054

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