Skip to content

Commit

Permalink
Re-imagine Baggy!LISTIFY
Browse files Browse the repository at this point in the history
Make it return a ready made Str.  This allows using native strs
internally, and thereby speed up things like Bag.perl|gist|Str.

Actually part of the nqp::list -> IterationBuffer hunt.
  • Loading branch information
lizmat committed Feb 24, 2017
1 parent 556db9d commit 9e9a4ad
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/core/Baggy.pm
Expand Up @@ -47,18 +47,24 @@ my role Baggy does QuantHash {
}
fail "Found negative values for {@toolow} in {self.^name}" if @toolow;
}
method !LISTIFY(&formatter) {
my $elems := nqp::getattr(%!elems,Map,'$!storage');
my $list := nqp::list();
nqp::setelems($list,nqp::elems($elems)); # presize
nqp::setelems($list,0);

my $iter := nqp::iterator($elems);
while $iter {
my \pair = nqp::iterval(nqp::shift($iter));
nqp::push($list,formatter(pair.key,pair.value));
}
$list
method !LISTIFY(&formatter, str $joiner) {
nqp::stmts(
(my $pairs := nqp::getattr(%!elems,Map,'$!storage')),
(my int $elems = nqp::elems($pairs)),
(my $list := nqp::setelems(nqp::list_s,$elems)),
(my $iter := nqp::iterator($pairs)),
(my int $i = -1),
nqp::while(
$iter,
nqp::bindpos_s($list,($i = nqp::add_i($i,1)),
formatter(
(my $pair := nqp::iterval(nqp::shift($iter))).key,
$pair.value
)
)
),
nqp::hllize(nqp::join($joiner,$list))
)
}

#--- interface methods
Expand Down Expand Up @@ -320,18 +326,18 @@ my role Baggy does QuantHash {
method default(Baggy:D:) { 0 }

multi method Str(Baggy:D: --> Str) {
join(' ', self!LISTIFY(-> \k,\v {v==1 ?? k.gist !! "{k.gist}({v})"}))
self!LISTIFY(-> \k,\v {v==1 ?? k.gist !! "{k.gist}({v})"}, ' ')
}
multi method gist(Baggy:D: --> Str) {
my str $name = nqp::unbox_s(self.^name);
( nqp::chars($name) == 3 ?? nqp::lc($name) !! "$name.new" )
~ '('
~ join(', ',self!LISTIFY(-> \k,\v {v==1 ?? k.gist !! "{k.gist}({v})"}))
~ self!LISTIFY(-> \k,\v {v==1 ?? k.gist !! "{k.gist}({v})"}, ', ')
~ ')'
}
multi method perl(Baggy:D: --> Str) {
'('
~ join(',', self!LISTIFY( -> \k,\v {"{k.perl}=>{v}"} ))
~ self!LISTIFY( -> \k,\v {"{k.perl}=>{v}"}, ',')
~ ").{self.^name}"
}

Expand Down

0 comments on commit 9e9a4ad

Please sign in to comment.