Class: Hyperll::HyperLogLogPlus
- Inherits:
-
Object
- Object
- Hyperll::HyperLogLogPlus
- Defined in:
- lib/hyperll/hyper_log_log_plus.rb,
ext/hyperll/hyper_log_log_plus.c
Class Method Summary collapse
Instance Method Summary collapse
- #cardinality ⇒ Object
- #convert_to_normal ⇒ Object
- #format ⇒ Object
- #format=(format) ⇒ Object
- #merge(*args) ⇒ Object
- #p ⇒ Object
- #serialize ⇒ Object
- #sp ⇒ Object
Class Method Details
.new(*args) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'ext/hyperll/hyper_log_log_plus.c', line 305
static VALUE rb_hyperllp_new(int argc, VALUE *argv, VALUE klass) {
VALUE p, sp, register_set_values, sparse_set_values;
rb_scan_args(argc, argv, "13", &p, &sp, ®ister_set_values, &sparse_set_values);
if (NIL_P(sp)) sp = INT2NUM(0);
hyperllp *hllp = ALLOC(hyperllp);
hyperllp_init(hllp, NUM2INT(p), NUM2INT(sp));
VALUE hllpv = Data_Wrap_Struct(klass, 0, hyperllp_free, hllp);
if (hllp->p < 4) rb_raise(rb_eArgError, "p must be >= 4");
if (hllp->sp >= 32) rb_raise(rb_eArgError, "sp must be < 32");
if (hllp->sp != 0 && hllp->p > hllp->sp) rb_raise(rb_eArgError, "p must be <= sp");
if (!NIL_P(register_set_values)) {
Check_Type(register_set_values, T_ARRAY);
register_set *rset = hllp->register_set;
if (RARRAY_LEN(register_set_values) == rset->size) {
for (int i = 0; i < rset->size; i++) {
rset->values[i] = NUM2ULONG(rb_ary_entry(register_set_values, i));
}
} else {
rb_raise(rb_eArgError, "initial register set values is not of the correct size");
}
}
if (!NIL_P(sparse_set_values)) {
Check_Type(sparse_set_values, T_ARRAY);
sparse_set *sset = hllp->sparse_set;
if (RARRAY_LEN(sparse_set_values) <= sset->capacity) {
sset->size = RARRAY_LEN(sparse_set_values);
for (int i = 0; i < sset->size; i++) {
sset->values[i] = NUM2ULONG(rb_ary_entry(sparse_set_values, i));
}
} else {
rb_raise(rb_eArgError, "initial sparse set values have too many values");
}
}
return hllpv;
}
|
.unserialize(serialized) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/hyperll/hyper_log_log_plus.rb', line 5 def self.unserialize(serialized) unpacked = serialized.unpack("C*") version = unpacked.shift(4) # integer, we don't currently use it p = Varint.read_unsigned_var_int(unpacked) sp = Varint.read_unsigned_var_int(unpacked) case format_type = Varint.read_unsigned_var_int(unpacked) when 0 # :normal size = Varint.read_unsigned_var_int(unpacked) rs_values = unpacked.pack("C*").unpack("N*") new(p, sp, rs_values).tap { |hllp| hllp.format = :normal } when 1 # :sparse ss_length = Varint.read_unsigned_var_int(unpacked) sparse_set = DeltaBytes.uncompress(unpacked) new(p, sp, nil, sparse_set) else raise ArgumentError, "invalid format: #{format_type}" end end |
Instance Method Details
#cardinality ⇒ Object
392 393 394 395 396 397 |
# File 'ext/hyperll/hyper_log_log_plus.c', line 392 static VALUE rb_hyperllp_cardinality(VALUE self) { hyperllp *hllp; Data_Get_Struct(self, hyperllp, hllp); return INT2NUM(hyperllp_cardinality(hllp)); } |
#convert_to_normal ⇒ Object
450 451 452 453 454 455 456 |
# File 'ext/hyperll/hyper_log_log_plus.c', line 450 static VALUE rb_hyperllp_convert_to_normal(VALUE self) { hyperllp *hllp; Data_Get_Struct(self, hyperllp, hllp); hyperllp_convert_to_normal(hllp); return self; } |
#format ⇒ Object
349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'ext/hyperll/hyper_log_log_plus.c', line 349
static VALUE rb_hyperllp_format(VALUE self) {
hyperllp *hllp;
Data_Get_Struct(self, hyperllp, hllp);
switch(hllp->format) {
case FORMAT_NORMAL:
return ID2SYM(rb_intern("normal"));
case FORMAT_SPARSE:
return ID2SYM(rb_intern("sparse"));
}
return Qnil;
}
|
#format=(format) ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'ext/hyperll/hyper_log_log_plus.c', line 363
static VALUE rb_hyperllp_format_set(VALUE self, VALUE format) {
Check_Type(format, T_SYMBOL);
hyperllp *hllp;
Data_Get_Struct(self, hyperllp, hllp);
if (format == ID2SYM(rb_intern("normal"))) {
hllp->format = FORMAT_NORMAL;
} else if (format == ID2SYM(rb_intern("sparse"))) {
hllp->format = FORMAT_SPARSE;
}
return format;
}
|
#merge(*args) ⇒ Object
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'ext/hyperll/hyper_log_log_plus.c', line 399
static VALUE rb_hyperllp_merge(int argc, VALUE *argv, VALUE self) {
VALUE others;
rb_scan_args(argc, argv, "*", &others);
hyperllp *hllp;
Data_Get_Struct(self, hyperllp, hllp);
int len = RARRAY_LEN(others);
for (int i = 0; i < len; i++) {
hyperllp *ohllp;
Data_Get_Struct(rb_ary_entry(others, i), hyperllp, ohllp);
if (!ohllp || hllp->p != ohllp->p) {
rb_raise(rb_eArgError, "cannot merge estimators of different sizes");
return Qnil;
} else {
hyperllp_merge(hllp, ohllp);
}
}
return self;
}
|
#p ⇒ Object
378 379 380 381 382 383 |
# File 'ext/hyperll/hyper_log_log_plus.c', line 378
static VALUE rb_hyperllp_p(VALUE self) {
hyperllp *hllp;
Data_Get_Struct(self, hyperllp, hllp);
return INT2NUM(hllp->p);
}
|
#serialize ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/hyperll/hyper_log_log_plus.rb', line 28 def serialize str = "" str << [-2].pack("N") # -VERSION str << Varint.write_unsigned_var_int(p).pack("C*") str << Varint.write_unsigned_var_int(sp).pack("C*") case format when :normal str << Varint.write_unsigned_var_int(0).pack("C*") rs_bytes = raw_register_set str << Varint.write_unsigned_var_int(rs_bytes.length * 4).pack("C*") str << rs_bytes.pack("N*") when :sparse str << Varint.write_unsigned_var_int(1).pack("C*") ss_bytes = raw_sparse_set str << Varint.write_unsigned_var_int(ss_bytes.length).pack("C*") str << DeltaBytes.compress(ss_bytes).pack("C*") end str end |
#sp ⇒ Object
385 386 387 388 389 390 |
# File 'ext/hyperll/hyper_log_log_plus.c', line 385
static VALUE rb_hyperllp_sp(VALUE self) {
hyperllp *hllp;
Data_Get_Struct(self, hyperllp, hllp);
return INT2NUM(hllp->sp);
}
|