| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| #include "murmur3.h" |
| 7 |
|
| |
| 8 |
|
| #define MAGIC 1 |
| 9 |
|
| #define HLL_HASH_SEED 313 |
| 10 |
|
| |
| 11 |
|
| typedef struct HyperLogLog { |
| 12 |
|
| uint32_t m; /// register num |
| 13 |
|
| uint8_t k; /// register bit width |
| 14 |
|
| uint8_t* registers; |
| 15 |
|
| double alphaMM; /// alpha * m^2 |
| 16 |
|
| }*HLL; |
| 17 |
|
| |
| 18 |
|
| #define GET_HLLPTR(x) get_hll(aTHX_ x, "$self") |
| 19 |
|
| |
| 20 |
|
| static const double two_32 = 4294967296.0; |
| 21 |
|
| static const double neg_two_32 = -4294967296.0; |
| 22 |
|
| |
| 23 |
|
| /** |
| 24 |
|
| * Extract pointer of struct HyperLogLog from SV. |
| 25 |
|
| * |
| 26 |
|
| * @param[in] object Perl object |
| 27 |
|
| * @param[in] context |
| 28 |
|
| */ |
| 29 |
0 |
| static HLL get_hll(pTHX_ SV* object, const char* context) { |
| 30 |
|
| SV *sv; |
| 31 |
|
| IV address; |
| 32 |
|
| |
| 33 |
0 |
▶ | if (MAGIC) SvGETMAGIC(object); |
Branch: 2 branches
| Branch | True | False | Total |
| if (MAGIC) SvGETMAGIC(object); |
0 |
0 |
0 |
| if (MAGIC) SvGETMAGIC(object); |
0 |
0 |
0 |
|
| 34 |
0 |
▶ | if (!SvROK(object)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(object)) { |
0 |
0 |
0 |
|
| 35 |
0 |
▶ | if (SvOK(object)) croak("%s is not a reference", context); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(object)) croak("%s is not a reference", context); |
0 |
0 |
0 |
|
| 36 |
0 |
| croak("%s is undefined", context); |
| 37 |
|
| } |
| 38 |
0 |
| sv = SvRV(object); |
| 39 |
0 |
▶ | if (!SvOBJECT(sv)) croak("%s is not an object reference", context); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOBJECT(sv)) croak("%s is not an object reference", context); |
0 |
0 |
0 |
|
| 40 |
0 |
▶ | if(!sv_derived_from(object,"Algorithm::HyperLogLog")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!sv_derived_from(object,"Algorithm::HyperLogLog")) { |
0 |
0 |
0 |
|
| 41 |
0 |
| croak("%s is not a Algorithm::HyperLogLog", context); |
| 42 |
|
| } |
| 43 |
0 |
| address = SvIV(sv); |
| 44 |
0 |
▶ | if (!address) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!address) |
0 |
0 |
0 |
|
| 45 |
0 |
| croak("Algorithm::HyperLogLog object %s has a NULL pointer", context); |
| 46 |
0 |
| return INT2PTR(HLL, address); |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
|
| /** |
| 50 |
|
| * Initialize struct HyperLogLog |
| 51 |
|
| * |
| 52 |
|
| * @param[in] k paramater for determining register size |
| 53 |
|
| * @return pointer of the intialized HLL |
| 54 |
|
| */ |
| 55 |
0 |
| static inline HLL initialize_hll(pTHX_ uint32_t k){ |
| 56 |
0 |
| HLL hll = NULL; |
| 57 |
0 |
| double alpha = 0.0; |
| 58 |
0 |
| New(__LINE__, hll, 1, struct HyperLogLog); |
| 59 |
0 |
▶ | if( k < 4 || k > 16 ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if( k < 4 || k > 16 ) { |
0 |
0 |
0 |
| if( k < 4 || k > 16 ) { |
0 |
0 |
0 |
|
| 60 |
0 |
| croak("Number of ragisters must be in the range [4,16]"); |
| 61 |
|
| } |
| 62 |
0 |
| hll->k = k; |
| 63 |
0 |
| hll->m = 1 << hll->k; |
| 64 |
0 |
| Newxz(hll->registers, hll->m, uint8_t); |
| 65 |
0 |
| switch (hll->m) { |
| 66 |
0 |
| case 16: |
| 67 |
0 |
| alpha = 0.673; |
| 68 |
0 |
| break; |
| 69 |
0 |
| case 32: |
| 70 |
0 |
| alpha = 0.697; |
| 71 |
0 |
| break; |
| 72 |
0 |
| case 64: |
| 73 |
0 |
| alpha = 0.709; |
| 74 |
0 |
| break; |
| 75 |
0 |
| default: |
| 76 |
0 |
| alpha = 0.7213/(1.0 + (1.079/(double) hll->m)); |
| 77 |
0 |
| break; |
| 78 |
|
| } |
| 79 |
0 |
| hll->alphaMM = alpha * hll->m * hll->m; |
| 80 |
0 |
| return hll; |
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| |
| 84 |
|
| /** |
| 85 |
|
| * Returns position of the leftmost 1-bit of x. |
| 86 |
|
| * |
| 87 |
|
| * @param[in] x target of search 1-bit |
| 88 |
|
| * @param[in] b bit width |
| 89 |
|
| * @return position of the leftmost 1-bit of x |
| 90 |
|
| */ |
| 91 |
0 |
| static inline uint8_t rho(uint32_t x, uint8_t b) { |
| 92 |
0 |
| uint8_t v = 1; |
| 93 |
0 |
▶ | while (v <= b && !(x & 0x80000000)) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (v <= b && !(x & 0x80000000)) { |
0 |
0 |
0 |
| while (v <= b && !(x & 0x80000000)) { |
0 |
0 |
0 |
|
| 94 |
0 |
| v++; |
| 95 |
0 |
| x <<= 1; |
| 96 |
|
| } |
| 97 |
0 |
| return v; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
|
| MODULE = Algorithm::HyperLogLog PACKAGE = Algorithm::HyperLogLog |
| 101 |
|
| |
| 102 |
|
| PROTOTYPES: DISABLE |
| 103 |
|
| |
| 104 |
|
| # Constructor |
| 105 |
|
| HLL |
| 106 |
|
| new(const char *klass, uint32_t k) |
| 107 |
|
| CODE: |
| 108 |
|
| { |
| 109 |
0 |
| RETVAL = initialize_hll(aTHX_ k); |
| 110 |
|
| } |
| 111 |
|
| OUTPUT: |
| 112 |
|
| RETVAL |
| 113 |
|
| |
| 114 |
|
| # Constructor(From dumped data) |
| 115 |
|
| HLL |
| 116 |
|
| _new_from_dump(const char *klass, uint32_t k, AV* data) |
| 117 |
|
| PREINIT: |
| 118 |
|
| uint32_t i; |
| 119 |
0 |
| uint32_t len = 0; |
| 120 |
|
| CODE: |
| 121 |
|
| { |
| 122 |
0 |
| RETVAL = initialize_hll(aTHX_ k); |
| 123 |
0 |
| len = av_len(data); |
| 124 |
0 |
▶ | for(i = 0;i <= len;++i){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0;i <= len;++i){ |
0 |
0 |
0 |
|
| 125 |
0 |
| RETVAL->registers[i] = (uint8_t)SvUV(*av_fetch(data, i, 0)); |
| 126 |
|
| } |
| 127 |
|
| } |
| 128 |
|
| OUTPUT: |
| 129 |
|
| RETVAL |
| 130 |
|
| |
| 131 |
|
| # dump registers |
| 132 |
|
| AV* |
| 133 |
|
| _dump_register(HLL self) |
| 134 |
|
| CODE: |
| 135 |
|
| { |
| 136 |
0 |
| RETVAL = (AV*)sv_2mortal((SV*)newAV()); |
| 137 |
|
| uint32_t i; |
| 138 |
0 |
▶ | for(i = 0;i < self->m; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0;i < self->m; i++){ |
0 |
0 |
0 |
|
| 139 |
0 |
| av_push(RETVAL, newSVuv(self->registers[i])); |
| 140 |
|
| } |
| 141 |
|
| } |
| 142 |
|
| OUTPUT: |
| 143 |
|
| RETVAL |
| 144 |
|
| |
| 145 |
|
| # Return number of registers. |
| 146 |
|
| uint32_t |
| 147 |
|
| register_size(HLL self) |
| 148 |
|
| CODE: |
| 149 |
0 |
▶ | RETVAL = self->m; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = self->m; |
0 |
0 |
0 |
|
| 150 |
|
| OUTPUT: |
| 151 |
|
| RETVAL |
| 152 |
|
| |
| 153 |
|
| # Add element to the estimator |
| 154 |
|
| void |
| 155 |
|
| add(HLL self, ...) |
| 156 |
|
| PREINIT: |
| 157 |
|
| uint32_t hash; |
| 158 |
|
| uint32_t index; |
| 159 |
|
| uint8_t rank; |
| 160 |
|
| I32 arg_index; |
| 161 |
|
| STRLEN n_a; |
| 162 |
|
| CODE: |
| 163 |
|
| { |
| 164 |
0 |
▶ | if(items > 1){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(items > 1){ |
0 |
0 |
0 |
|
| 165 |
0 |
▶ | for(arg_index = 1; arg_index < items; ++arg_index){ |
Branch: 1 branch
| Branch | True | False | Total |
| for(arg_index = 1; arg_index < items; ++arg_index){ |
0 |
0 |
0 |
|
| 166 |
0 |
| char* str = SvPV(ST(arg_index), n_a); |
| 167 |
0 |
| MurmurHash3_32((void *) str, strlen(str), HLL_HASH_SEED, (void *) &hash); |
| 168 |
0 |
| index = (hash >> (32 - self->k)); |
| 169 |
0 |
| rank = rho( (hash << self->k), 32 - self->k ); |
| 170 |
0 |
▶ | if( rank > self->registers[index] ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( rank > self->registers[index] ) { |
0 |
0 |
0 |
|
| 171 |
0 |
| self->registers[index] = rank; |
| 172 |
|
| } |
| 173 |
|
| } |
| 174 |
|
| } |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
|
| |
| 178 |
|
| # Estimate cardinality |
| 179 |
|
| double |
| 180 |
|
| estimate(HLL self) |
| 181 |
|
| CODE: |
| 182 |
|
| { |
| 183 |
|
| double estimate; |
| 184 |
0 |
| uint32_t m = self->m; |
| 185 |
0 |
| uint32_t i = 0; |
| 186 |
0 |
| double sum = 0.0; |
| 187 |
|
| // Calculate hermonic mean |
| 188 |
0 |
▶ | for (i = 0; i < m; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < m; i++) { |
0 |
0 |
0 |
|
| 189 |
0 |
| sum += 1.0/pow(2.0, self->registers[i]); |
| 190 |
|
| } |
| 191 |
0 |
| estimate = self->alphaMM/sum; // E in the original paper |
| 192 |
0 |
▶ | if( estimate <= 2.5 * m ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( estimate <= 2.5 * m ) { |
0 |
0 |
0 |
|
| 193 |
0 |
| uint32_t zeros = 0;// V in the original paper |
| 194 |
0 |
| uint32_t i = 0; |
| 195 |
0 |
▶ | for (i = 0; i < m; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < m; i++) { |
0 |
0 |
0 |
|
| 196 |
0 |
▶ | if (self->registers[i] == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->registers[i] == 0) { |
0 |
0 |
0 |
|
| 197 |
0 |
| zeros++; |
| 198 |
|
| } |
| 199 |
|
| } |
| 200 |
0 |
▶ | if( zeros != 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if( zeros != 0 ) { |
0 |
0 |
0 |
|
| 201 |
0 |
| estimate = m * log((double)m/zeros); |
| 202 |
|
| } |
| 203 |
0 |
▶ | } else if (estimate > (1.0/30.0) * two_32) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (estimate > (1.0/30.0) * two_32) { |
0 |
0 |
0 |
|
| 204 |
0 |
| estimate = neg_two_32 * log(1.0 - ( estimate/two_32 ) ); |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
0 |
▶ | RETVAL = estimate; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = estimate; |
0 |
0 |
0 |
|
| 208 |
|
| } |
| 209 |
|
| OUTPUT: |
| 210 |
|
| RETVAL |
| 211 |
|
| |
| 212 |
|
| |
| 213 |
|
| # Merge two HLLs |
| 214 |
|
| double |
| 215 |
|
| merge(HLL self, HLL other) |
| 216 |
|
| CODE: |
| 217 |
|
| { |
| 218 |
0 |
| uint32_t m = self->m; |
| 219 |
0 |
| uint32_t i = 0; |
| 220 |
|
| |
| 221 |
0 |
▶ | if (m != other->m) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (m != other->m) { |
0 |
0 |
0 |
|
| 222 |
0 |
| croak("hll size mismatch: %d != %d\n", m, other->m); |
| 223 |
|
| } |
| 224 |
|
| |
| 225 |
0 |
▶ | for (i = 0; i < m; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < m; i++) { |
0 |
0 |
0 |
|
| 226 |
0 |
▶ | if (self->registers[i] < other->registers[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->registers[i] < other->registers[i]) { |
0 |
0 |
0 |
|
| 227 |
0 |
| self->registers[i] = other->registers[i]; |
| 228 |
|
| } |
| 229 |
|
| } |
| 230 |
0 |
| XSRETURN_UNDEF; |
| 231 |
|
| } |
| 232 |
|
| OUTPUT: |
| 233 |
|
| RETVAL |
| 234 |
|
| |
| 235 |
|
| # Destructor |
| 236 |
|
| void |
| 237 |
|
| DESTROY(HLL self) |
| 238 |
|
| CODE: |
| 239 |
|
| { |
| 240 |
0 |
| Safefree(self->registers); |
| 241 |
0 |
| Safefree (self); |
| 242 |
|
| } |
| 243 |
|
| |