Skip to content
Permalink
Browse files

Add QuantHash.STORE(\keys,\values)

Another way of creating a QuantHashes internally, taking 2 iterables: one with
keys and one with values, that are supposed to generate an equal number
of values.  To be used in hyper operations.
  • Loading branch information...
lizmat committed Nov 14, 2018
1 parent cf5c8a0 commit 534d8f65769f72fe862091e4eb8c56f97b812b88
Showing with 115 additions and 0 deletions.
  1. +9 −0 src/core/Bag.pm6
  2. +9 −0 src/core/BagHash.pm6
  3. +9 −0 src/core/Mix.pm6
  4. +9 −0 src/core/MixHash.pm6
  5. +55 −0 src/core/Rakudo/QuantHash.pm6
  6. +9 −0 src/core/Set.pm6
  7. +15 −0 src/core/SetHash.pm6
@@ -30,6 +30,15 @@ my class Bag does Baggy {
!! self.SET-SELF(Rakudo::QuantHash.ADD-PAIRS-TO-BAG(
nqp::create(Rakudo::Internals::IterationSet), $iterator))
}
multi method STORE(Bag:D: \objects, \values, :$INITIALIZE! --> Bag:D) {
self.SET-SELF(
Rakudo::QuantHash.ADD-OBJECTS-VALUES-TO-BAG(
nqp::create(Rakudo::Internals::IterationSet),
objects.iterator,
values.iterator
)
)
}

multi method DELETE-KEY(Bag:D: \k) {
X::Immutable.new(method => 'DELETE-KEY', typename => self.^name).throw;
@@ -12,6 +12,15 @@ my class BagHash does Baggy {
)
)
}
multi method STORE(BagHash:D: \objects, \values --> BagHash:D) {
self.SET-SELF(
Rakudo::QuantHash.ADD-OBJECTS-VALUES-TO-BAG(
nqp::create(Rakudo::Internals::IterationSet),
objects.iterator,
values.iterator
)
)
}
multi method AT-KEY(BagHash:D: \k) is raw {
Proxy.new(
FETCH => {
@@ -11,6 +11,15 @@ my class Mix does Mixy {
!! self.SET-SELF(Rakudo::QuantHash.ADD-PAIRS-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet), $iterator))
}
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
)
)
}

multi method DELETE-KEY(Mix:D: \k) {
X::Immutable.new(method => 'DELETE-KEY', typename => self.^name).throw;
@@ -15,6 +15,15 @@ my class MixHash does Mixy {
)
)
}
multi method STORE(MixHash:D: \objects, \values --> MixHash:D) {
self.SET-SELF(
Rakudo::QuantHash.ADD-OBJECTS-VALUES-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet),
objects.iterator,
values.iterator
)
)
}
multi method AT-KEY(MixHash:D: \k) is raw {
Proxy.new(
FETCH => {
@@ -334,6 +334,20 @@ my class Rakudo::QuantHash {
)
}

# Add to given IterationSet with setty semantics the values of the two
# given iterators where the first iterator supplies objects, and the
# second supplies values (only include if value is trueish).
method ADD-OBJECTS-VALUES-TO-SET(\elems,Mu \objects, Mu \bools) is raw {
nqp::until(
nqp::eqaddr((my \object := objects.pull-one),IterationEnd),
nqp::if(
bools.pull-one,
nqp::bindkey(elems,object.WHICH,nqp::decont(object))
)
);
self
}

# Add to given IterationSet with setty semantics the keys of given Map
method ADD-MAP-TO-SET(\elems, \map) {
nqp::stmts(
@@ -818,6 +832,20 @@ my class Rakudo::QuantHash {
)
}

# Add to given IterationSet with baggy semantics the values of the two
# given iterators where the first iterator supplies objects, and the
# second supplies values.
method ADD-OBJECTS-VALUES-TO-BAG(\elems,Mu \objects, Mu \values) is raw {
nqp::until(
nqp::eqaddr((my \object := objects.pull-one),IterationEnd),
nqp::if(
(my \value := values.pull-one.Int) > 0,
nqp::bindkey(elems,object.WHICH,Pair.new(object,value))
)
);
self
}

# Take the given IterationSet with baggy semantics, and add the other
# IterationSet with setty semantics to it. Return the given IterationSet.
method ADD-SET-TO-BAG(\elems,Mu \set) {
@@ -1242,6 +1270,33 @@ 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 {
nqp::until(
nqp::eqaddr((my \object := objects.pull-one),IterationEnd),
nqp::if(
nqp::istype((my \value := values.pull-one),Num)
&& nqp::isnanorinf(value),
X::OutOfRange.new( # NaN or -Inf or Inf, we're done
what => 'Value',
got => value,
range => '-Inf^..^Inf'
).throw,
nqp::if(
nqp::istype(nqp::bind(value,value.Real),Real),
nqp::if(
value,
nqp::bindkey(elems,object.WHICH,Pair.new(object,value))
),
value.throw
)
)
);
elems
}

# Take the given IterationSet with mixy semantics, and add the other
# IterationSet with setty semantics to it. Return the given IterationSet.
method ADD-SET-TO-MIX(\elems,Mu \set) {
@@ -93,6 +93,15 @@ my class Set does Setty {
!! self.SET-SELF(Rakudo::QuantHash.ADD-PAIRS-TO-SET(
nqp::create(Rakudo::Internals::IterationSet), $iterator))
}
multi method STORE(Set:D: \objects, \bools, :$INITIALIZE! --> Set:D) {
self.SET-SELF(
Rakudo::QuantHash.ADD-OBJECTS-VALUES-TO-SET(
nqp::create(Rakudo::Internals::IterationSet),
objects.iterator,
bools.iterator
)
)
}

multi method AT-KEY(Set:D: \k --> Bool:D) {
nqp::hllbool($!elems ?? nqp::existskey($!elems,k.WHICH) !! 0)
@@ -194,6 +194,21 @@ my class SetHash does Setty {
)
)
}
multi method STORE(SetHash:D: \objects, \bools --> SetHash:D) {
my \iterobjs := objects.iterator;
my \iterbools := bools.iterator;
nqp::bindattr(
self,SetHash,'$!elems',nqp::create(Rakudo::Internals::IterationSet)
);
nqp::until(
nqp::eqaddr((my \object := iterobjs.pull-one),IterationEnd),
nqp::if(
iterbools.pull-one,
nqp::bindkey($!elems,object.WHICH,nqp::decont(object))
)
);
self
}

multi method AT-KEY(SetHash:D: \k --> Bool:D) is raw {
Proxy.new(

0 comments on commit 534d8f6

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