| 1 | package Data::SortedSet::Shared::Strings; | ||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Data::Intern::Shared (); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Data::SortedSet::Shared (); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | # ---- construction ---- | ||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | 11 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 13 | 11 | my ($class, %opt) = @_; | |||||||||||||||||||||||||||||||||||||
| 14 | 11 | my $max = $opt{max}; | |||||||||||||||||||||||||||||||||||||
| 15 | 11 | ▶ | Carp::croak("new: 'max' is required") unless defined $max; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 16 | 10 | my $set = Data::SortedSet::Shared->new($opt{set}, $max, $opt{mode}); | |||||||||||||||||||||||||||||||||||||
| 17 | # pass mode only when given: Data::Intern::Shared 0.01 predates the mode argument | ||||||||||||||||||||||||||||||||||||||
| 18 | my $keys = Data::Intern::Shared->new($opt{keys}, $opt{max_keys} // $max, $opt{arena} // 0, | ||||||||||||||||||||||||||||||||||||||
| 19 | 10 | ▶ | defined $opt{mode} ? $opt{mode} : ()); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$opt{"max_keys"} // $max
$opt{"arena"} // 0
MC/DC0%0/2
$opt{"max_keys"} // $max
$opt{"max_keys"}
$max
MC/DC100%1/1
$opt{"arena"} // 0
$opt{"arena"}
Truth table66%2/3
A // B
A= $opt{"max_keys"}B= $max
Truth table100%2/2
A
A= $opt{"arena"}
| |||||||||||||||||||||||||||||||||||||||
| 20 | 10 | return bless { set => $set, keys => $keys }, $class; | |||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | # wrap two already-constructed shared objects (e.g. memfd-backed) | ||||||||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | sub wrap { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 25 | 3 | my ($class, $set, $keys) = @_; | |||||||||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | Carp::croak("wrap: expected a Data::SortedSet::Shared") | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $set and $set->isa("Data::SortedSet::Shared")
MC/DC50%1/2
ref $set and $set->isa("Data::SortedSet::Shared")
ref $set
$set->isa("Data::SortedSet::Shared")
Truth table66%2/3
A and B
A= ref $setB= $set->isa("Data::SortedSet::Shared")
| |||||||||||||||||||||||||||||||||||||||
| 27 | unless ref $set && $set->isa('Data::SortedSet::Shared'); | ||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | Carp::croak("wrap: expected a Data::Intern::Shared") | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $keys and $keys->isa("Data::Intern::Shared")
MC/DC50%1/2
ref $keys and $keys->isa("Data::Intern::Shared")
ref $keys
$keys->isa("Data::Intern::Shared")
Truth table66%2/3
A and B
A= ref $keysB= $keys->isa("Data::Intern::Shared")
| |||||||||||||||||||||||||||||||||||||||
| 29 | unless ref $keys && $keys->isa('Data::Intern::Shared'); | ||||||||||||||||||||||||||||||||||||||
| 30 | 1 | return bless { set => $set, keys => $keys }, $class; | |||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | sub set { $_[0]{set} } # the underlying Data::SortedSet::Shared | ||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 34 | 3 | ▶ | sub key_table { $_[0]{keys} } # the underlying Data::Intern::Shared | ||||||||||||||||||||||||||||||||||||
|
Subroutine
key_table: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | # ---- mutators (intern the key -> id) ---- | ||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||
| 38 | 20233 | ▶ | sub add { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 39 | 20233 | my ($self, $str, $score) = @_; | |||||||||||||||||||||||||||||||||||||
| 40 | 20233 | ▶ | Carp::croak("add: NaN score") if $score != $score; # reject before interning (else a ghost key leaks) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 41 | 20233 | my $id = $self->{keys}->intern($str); | |||||||||||||||||||||||||||||||||||||
| 42 | 20233 | ▶ | return undef unless defined $id; # key table full | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 43 | 20233 | return $self->{set}->add($id, $score); | |||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||
| 46 | 5974 | ▶ | sub incr { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
incr: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 47 | 5974 | my ($self, $str, $delta) = @_; | |||||||||||||||||||||||||||||||||||||
| 48 | 5974 | ▶ | Carp::croak("incr: NaN delta") if $delta != $delta; # reject before interning (else a ghost key leaks) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 49 | 5974 | my $id = $self->{keys}->intern($str); | |||||||||||||||||||||||||||||||||||||
| 50 | 5974 | ▶ | Carp::croak("incr: key table full") unless defined $id; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 51 | 5973 | return $self->{set}->incr($id, $delta); | |||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||
| 54 | 7762 | ▶ | sub remove { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
remove: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 55 | 7762 | my ($self, $str) = @_; | |||||||||||||||||||||||||||||||||||||
| 56 | 7762 | my $id = $self->{keys}->id_of($str); | |||||||||||||||||||||||||||||||||||||
| 57 | 7762 | ▶ | return defined $id ? $self->{set}->remove($id) : 0; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | sub add_many { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
add_many: called CC=7 SCAR=19.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 61 | 3 | my ($self, $rows) = @_; | |||||||||||||||||||||||||||||||||||||
| 62 | 3 | ▶ | Carp::croak("add_many: expected an arrayref") unless ref $rows eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 63 | 3 | my @id_rows; | |||||||||||||||||||||||||||||||||||||
| 64 | 3 | for my $r (@$rows) { | |||||||||||||||||||||||||||||||||||||
| 65 | 10 | ▶ | next unless ref $r eq 'ARRAY' && @$r >= 2; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $r eq "ARRAY" and @$r >= 2
MC/DC50%1/2
ref $r eq "ARRAY" and @$r >= 2
ref $r eq "ARRAY"
@$r >= 2
Truth table66%2/3
A and B
A= ref $r eq "ARRAY"B= @$r >= 2
| |||||||||||||||||||||||||||||||||||||||
| 66 | 9 | ▶ | next if $r->[1] != $r->[1]; # skip a NaN score before interning (no ghost key slot) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 67 | 8 | my $id = $self->{keys}->intern($r->[0]); | |||||||||||||||||||||||||||||||||||||
| 68 | 8 | ▶ | last unless defined $id; # key table full -> stop | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 69 | 8 | push @id_rows, [ $id, $r->[1] ]; | |||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||
| 71 | 3 | return $self->{set}->add_many(\@id_rows); | |||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | 3 | ▶ | sub clear { $_[0]{set}->clear; $_[0]{keys}->clear; return } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||
| 76 | # ---- lookup (id_of the key; undef short-circuits) ---- | ||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||
| 78 | 1766 | ▶ | sub score { my $id = $_[0]{keys}->id_of($_[1]); defined $id ? $_[0]{set}->score($id) : undef } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
score: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 79 | 883 | ▶ | sub rank { my $id = $_[0]{keys}->id_of($_[1]); defined $id ? $_[0]{set}->rank($id) : undef } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
rank: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 80 | 1 | ▶ | sub rev_rank { my $id = $_[0]{keys}->id_of($_[1]); defined $id ? $_[0]{set}->rev_rank($id) : undef } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
rev_rank: called CC=2 SCAR=7.2
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 81 | 5 | ▶ | sub exists { my $id = $_[0]{keys}->id_of($_[1]); defined $id ? $_[0]{set}->exists($id) : 0 } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
exists: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 82 | 12 | ▶ | sub count { $_[0]{set}->count } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 83 | 13 | ▶ | sub count_in_score { shift->{set}->count_in_score(@_) } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
count_in_score: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | # ---- rank / range (decode ids back to strings) ---- | ||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||
| 87 | 22 | ▶ | sub _decode { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 88 | 22 | my ($self, $ws, @list) = @_; | |||||||||||||||||||||||||||||||||||||
| 89 | 22 | my $k = $self->{keys}; | |||||||||||||||||||||||||||||||||||||
| 90 | 22 | ▶ | return map { $k->string($_) } @list unless $ws; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 91 | 2 | my @out; | |||||||||||||||||||||||||||||||||||||
| 92 | 2 | ▶ | for (my $i = 0; $i < @list; $i += 2) { push @out, $k->string($list[$i]), $list[$i + 1] } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 93 | 2 | return @out; | |||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||
| 96 | 3 | ▶ | sub at_rank { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
at_rank: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 97 | 3 | my $id = $_[0]{set}->at_rank($_[1]); | |||||||||||||||||||||||||||||||||||||
| 98 | 3 | ▶ | return defined $id ? $_[0]{keys}->string($id) : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | 5 | ▶ | sub range_by_rank { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
range_by_rank: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 102 | 5 | my ($self, $start, $stop, %opt) = @_; | |||||||||||||||||||||||||||||||||||||
| 103 | 5 | $self->_decode($opt{withscores}, $self->{set}->range_by_rank($start, $stop, %opt)); | |||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||
| 105 | 2 | ▶ | sub rev_range_by_rank { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
rev_range_by_rank: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 106 | 2 | my ($self, $start, $stop, %opt) = @_; | |||||||||||||||||||||||||||||||||||||
| 107 | 2 | $self->_decode($opt{withscores}, $self->{set}->rev_range_by_rank($start, $stop, %opt)); | |||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||
| 109 | 14 | ▶ | sub range_by_score { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
range_by_score: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 110 | 14 | my ($self, $min, $max, %opt) = @_; | |||||||||||||||||||||||||||||||||||||
| 111 | 14 | $self->_decode($opt{withscores}, $self->{set}->range_by_score($min, $max, %opt)); | |||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | sub rev_range_by_score { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
rev_range_by_score: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 114 | 1 | my ($self, $max, $min, %opt) = @_; | |||||||||||||||||||||||||||||||||||||
| 115 | 1 | $self->_decode($opt{withscores}, $self->{set}->rev_range_by_score($max, $min, %opt)); | |||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||
| 118 | # ---- pop / peek (id -> string) ---- | ||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||
| 120 | 3 | for my $m (qw(pop_min pop_max peek_min peek_max)) { | |||||||||||||||||||||||||||||||||||||
| 121 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 122 | 6059 | ▶ | *$m = sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||
| 123 | 6059 | my ($id, $score) = $_[0]{set}->$m; | |||||||||||||||||||||||||||||||||||||
| 124 | 6059 | ▶ | return defined $id ? ($_[0]{keys}->string($id), $score) : (); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 125 | 12 | }; | |||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||
| 128 | # ---- iteration ---- | ||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | 5 | ▶ | sub each { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
each: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 131 | 5 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||||||||||||
| 132 | 5 | $self->{set}->_each_strings($self->{keys}, $cb); | |||||||||||||||||||||||||||||||||||||
| 133 | 3 | return; | |||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||
| 136 | # ---- lifecycle ---- | ||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | sub sync { $_[0]{set}->sync; $_[0]{keys}->sync; return } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
sync: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 139 | 1 | ▶ | sub unlink { $_[0]{set}->unlink; $_[0]{keys}->unlink; return } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
unlink: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | sub stats { { set => $_[0]{set}->stats, keys => $_[0]{keys}->stats } } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
stats: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||
| 142 | 3 | 1; | |||||||||||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||||||||||||