GuavaRing.xs

filter: Statementstmt 89.7% 35 / 39 Branchbran 65.3% 17 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.0% 52 / 65 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Hash/GuavaRing.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7  
8 /////////////////////////// guava-hash code ///////////////////////////////
9 int guava(long long state, unsigned int buckets);
10  
11 static const long long
12 K = 2862933555777941757L;
13  
14 static const double
15 D = 0x1.0p31;
16  
17 42 int guava(long long state, unsigned int buckets) {
18 42 int candidate = 0;
19 int next;
20 while (1) {
21 143 state = K * state + 1;
22 143 next = (int) ( (double) (candidate + 1) / ( (double)( (int)( (long long unsigned) state >> 33 ) + 1 ) / D ) );
23 // printf("\nstate=%lld next=%d\n",stat,next);
24 143 ▶ if ( ( next >= 0 ) && ( next < buckets )) {
Branch: 2 branches
BranchTrueFalseTotal
if ( ( next >= 0 ) && ( next < buckets )) { 143 0 143
if ( ( next >= 0 ) && ( next < buckets )) { 101 42 143
25 101 candidate = next;
26 } else {
27 42 return candidate;
28 }
29 }
30 }
31  
32  
33 /////////////////////// END guava-hash code ///////////////////////////////
34  
35 #ifndef likely
36 #define likely(x) __builtin_expect(!!(x), 1)
37 #define unlikely(x) __builtin_expect(!!(x), 0)
38 #endif
39  
40 typedef struct {
41 SV * self;
42 HV * stash;
43 AV * nodes;
44 } GuavaRing;
45  
46 #define svstrcmp(a,b) strcmp(SvPV_nolen(a),b)
47  
48 MODULE = Hash::GuavaRing PACKAGE = Hash::GuavaRing
49  
50 void new(...)
51 PPCODE:
52 2 ▶ if (items < 1) croak("Usage: %s->new(...)",SvPV_nolen(ST(0)));
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: %s->new(...)",SvPV_nolen(ST(0))); 0 2 2
53 2 GuavaRing * self = (GuavaRing *) safemalloc( sizeof(GuavaRing) );
54 2 ▶ if (unlikely(!self)) croak("Failed to allocate memory");
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!self)) croak("Failed to allocate memory"); 0 2 2
55 2 memset(self,0,sizeof(GuavaRing));
56 4 self->stash = gv_stashpv(SvPV_nolen(ST(0)), TRUE);
57 {
58 2 SV *iv = newSViv(PTR2IV( self ));
59 2 self->self = sv_bless(newRV_noinc (iv), self->stash);
60 2 ST(0) = sv_2mortal(self->self);
61 }
62 int i;
63 SV **key;
64 2 AV *nodes = 0;
65 4 ▶ for ( i=1; i < items; i=i+2) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i=1; i < items; i=i+2) { 2 2 4
66 4 ▶ if ( !svstrcmp(ST(i),"nodes") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( !svstrcmp(ST(i),"nodes") ) { 2 0 2
67 2 ▶ if (SvROK(ST(i+1)) && SvTYPE(SvRV(ST(i+1))) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(ST(i+1)) && SvTYPE(SvRV(ST(i+1))) == SVt_PVAV) { 2 0 2
if (SvROK(ST(i+1)) && SvTYPE(SvRV(ST(i+1))) == SVt_PVAV) { 2 0 2
68 2 nodes = (AV *) SvRV( ST(i+1) );
69 } else {
70 0 croak("nodes must be arrayref, but got %s", SvPV_nolen(ST(i+1)));
71 }
72 }
73 else {
74 0 croak("Uknown option '%s'", SvPV_nolen(ST(i)));
75 }
76 }
77 2 self->nodes = newAV();
78 18 ▶ for ( i = 0; i <= av_len(nodes); i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; i <= av_len(nodes); i++ ) { 16 2 18
79 16 key = av_fetch( nodes, i, 0 );
80 16 av_store(self->nodes, i, SvREFCNT_inc(*key));
81 }
82  
83 2 XSRETURN(1);
84  
85 void DESTROY(SV *)
86 PPCODE:
87 2 register GuavaRing *self = ( GuavaRing * ) SvUV( SvRV( ST(0) ) );
88 2 ▶ if (self->nodes) SvREFCNT_dec(self->nodes);
Branch: 1 branch
BranchTrueFalseTotal
if (self->nodes) SvREFCNT_dec(self->nodes); 2 0 2
89 2 ▶ if (self->self && SvOK(self->self) && SvOK( SvRV(self->self) )) {
Branch: 3 branches
BranchTrueFalseTotal
if (self->self && SvOK(self->self) && SvOK( SvRV(self->self) )) { 2 0 2
if (self->self && SvOK(self->self) && SvOK( SvRV(self->self) )) { 1 1 2
if (self->self && SvOK(self->self) && SvOK( SvRV(self->self) )) { 0 1 1
90 0 SvREFCNT_inc(SvRV(self->self));
91 0 SvREFCNT_dec(self->self);
92 }
93 2 safefree(self);
94 2 XSRETURN_UNDEF;
95  
96 void get (SV *, SV * key)
97 PPCODE:
98 42 register GuavaRing *self = ( GuavaRing * ) SvUV( SvRV( ST(0) ) );
99 // fprintf(stderr,"\nav_len(self->nodes) = %i\n",av_len(self->nodes));
100 42 int idx = guava( SvIV(key), av_len(self->nodes)+1 );
101  
102 42 SV **node = av_fetch(self->nodes, idx, 0);
103  
104 42 ST(0) = *node;
105 42 XSRETURN(1);
↑ Summary blib/lib/Hash/GuavaRing.pm »