Skip to content

Commit

Permalink
Add QuantHash.STORE(\keys,\values)
Browse files Browse the repository at this point in the history
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 534d8f6
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/core/Bag.pm6
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/core/BagHash.pm6
Expand Up @@ -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 => {
Expand Down
9 changes: 9 additions & 0 deletions src/core/Mix.pm6
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/core/MixHash.pm6
Expand Up @@ -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 => {
Expand Down
55 changes: 55 additions & 0 deletions src/core/Rakudo/QuantHash.pm6
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions src/core/Set.pm6
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions src/core/SetHash.pm6
Expand Up @@ -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(
Expand Down

0 comments on commit 534d8f6

Please sign in to comment.