Data-HashMap-Shared
view release on metacpan or search on metacpan
Data::HashMap::Shared::SI16 - string to int16
Data::HashMap::Shared::SI32 - string to int32
Data::HashMap::Shared::SI - string to int64
Data::HashMap::Shared::SS - string to string
Integer Range and Wrapping
Integer keys and values are fixed-width two's complement: 16-bit for
"I16"/"SI16"/"I16S", 32-bit for "I32"/"SI32"/"I32S", 64-bit for
"II"/"IS"/"SI". A number outside the variant's range is silently truncated
to its low bits: on an "I16" map 70000 is key 4464. Numbers beyond 64 bits
saturate first (1e20 becomes -1, NaN 0). "incr" and "decr" wrap the same
way. Pick a variant wide enough for your data.
Constructor
my $map = Data::HashMap::Shared::II->new($path, $max_entries);
my $map = Data::HashMap::Shared::II->new(undef, $max_entries); # anonymous
my $map = Data::HashMap::Shared::II->new($path, $max_entries, $max_size);
my $map = Data::HashMap::Shared::II->new($path, $max_entries, $max_size, $ttl);
my $map = Data::HashMap::Shared::II->new($path, $max_entries, $max_size, $ttl, $lru_skip);
my $map = Data::HashMap::Shared::SS->new($path, $max_entries, 0, 0, 0, $arena_cap); # explicit arena bytes
my $map = Data::HashMap::Shared::II->new($path, $max_entries, $max_size, $ttl, $lru_skip, $arena_cap, $file_mode);
/* A UV parameter would wrap -1 to "everything". */
static UV shm_count_arg(pTHX_ SV *sv, const char *what, const char *classname) {
NV nv;
SvGETMAGIC(sv);
if (SvIOK(sv)) {
if (!SvIsUV(sv) && SvIVX(sv) < 0) croak("%s: %s must not be negative", classname, what);
return SvUVX(sv);
}
nv = SvNV_nomg(sv);
if (nv < 0) croak("%s: %s must not be negative", classname, what);
if (!(nv >= 1)) return 0; /* also NaN */
return nv >= (NV)UV_MAX ? UV_MAX : (UV)nv;
}
static inline uint32_t shm_u32_arg(pTHX_ SV *sv, const char *what, const char *classname, uint32_t dflt) {
UV v;
if (!sv) return dflt;
if ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVs_GMG)) == SVf_IOK && (UV)SvIVX(sv) <= 0xFFFFFFFFu)
return (uint32_t)SvIVX(sv);
v = shm_count_arg(aTHX_ sv, what, classname);
if (v > 0xFFFFFFFFu)
lib/Data/HashMap/Shared.pm view on Meta::CPAN
=item L<Data::HashMap::Shared::SS> - string to string
=back
=head2 Integer Range and Wrapping
Integer keys and values are fixed-width two's complement: 16-bit for
C<I16>/C<SI16>/C<I16S>, 32-bit for C<I32>/C<SI32>/C<I32S>, 64-bit for
C<II>/C<IS>/C<SI>. A number outside the variant's range is B<silently
truncated> to its low bits: on an C<I16> map C<70000> is key C<4464>. Numbers
beyond 64 bits saturate first (C<1e20> becomes C<-1>, NaN C<0>). C<incr> and
C<decr> wrap the same way. Pick a variant wide enough for your data.
=head2 Constructor
my $map = Data::HashMap::Shared::II->new($path, $max_entries);
my $map = Data::HashMap::Shared::II->new(undef, $max_entries); # anonymous
my $map = Data::HashMap::Shared::II->new($path, $max_entries, $max_size);
my $map = Data::HashMap::Shared::II->new($path, $max_entries, $max_size, $ttl);
my $map = Data::HashMap::Shared::II->new($path, $max_entries, $max_size, $ttl, $lru_skip);
my $map = Data::HashMap::Shared::SS->new($path, $max_entries, 0, 0, 0, $arena_cap); # explicit arena bytes
t/77-numeric-edges.t view on Meta::CPAN
is $si16->incr('k'), -32768, "SI16 incr wraps at 32767 ($tag)";
my $i32 = Data::HashMap::Shared::I32->new(undef, 64, 0, $ttl);
$i32->put(1, 2147483647);
is $i32->incr_by(1, 2), -2147483647, "I32 incr_by wraps past 2**31-1 ($tag)";
}
{
my $m = Data::HashMap::Shared::II->new(undef, 64);
$m->put(1, 1e20);
$m->put(2, -1e20);
$m->put(3, 'NaN' + 0);
is_deeply [ map { $m->get($_) } 1 .. 3 ], [ -1, -9223372036854775807 - 1, 0 ],
'numbers beyond the 64-bit range saturate before they are stored';
my $n = Data::HashMap::Shared::I16->new(undef, 64);
$n->put(1, -1e20);
is $n->get(1), 0, ' ... and a narrow variant keeps the low bits of that';
}
{
# header offsets: max_size 24, size 136, tombstones 140, stat_recoveries 184
my $dir = tempdir(CLEANUP => 1);
( run in 1.509 second using v1.01-cache-2.11-cpan-60884a8df76 )