Skip to content

Instantly share code, notes, and snippets.

@lizmat
Created April 11, 2017 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizmat/5ce4c4963e860f7adc4dba0ff932611c to your computer and use it in GitHub Desktop.
Save lizmat/5ce4c4963e860f7adc4dba0ff932611c to your computer and use it in GitHub Desktop.
Attempt at creating faster sorted keys list for QuantHashes
method raw_sorted_keys() {
nqp::if(
(my $elems := self.raw_hash) && nqp::elems($elems),
nqp::stmts(
(my $iter := nqp::iterator($elems)),
(my $list := nqp::setelems(nqp::list_s,nqp::elems($elems))),
nqp::bindpos_s(
nqp::setelems($list,1),
0,
nqp::iterkey_s(nqp::shift($iter))
),
(my $inserter := nqp::setelems(nqp::list_s,1)),
nqp::while(
$iter,
nqp::stmts(
(my int $low = 0),
(my int $high = nqp::sub_i(nqp::elems($list),1)),
nqp::bindpos_s(
$inserter,
0,
(my str $target = nqp::iterkey_s(nqp::shift($iter))),
),
nqp::until(
nqp::iseq_i($low,$high),
nqp::if(
nqp::islt_i(
nqp::cmp_s(
$target,
nqp::atpos_s(
$list,
(my int $i = nqp::div_i(nqp::add_i($low,$high),2))
)
),
0
),
($high = $i),
($low = nqp::add_i($i,1))
)
),
nqp::splice(
$list,
$inserter,
nqp::add_i(
$low,
nqp::isgt_i(
nqp::cmp_s($target,nqp::atpos_s($list,$low)),
0
)
),
0
)
)
),
$list
),
nqp::list_s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment