Class: Hyperll::DeltaBytes

Inherits:
Object
  • Object
show all
Defined in:
ext/hyperll/delta_bytes.c

Class Method Summary collapse

Class Method Details

.compress(rvalues) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'ext/hyperll/delta_bytes.c', line 38

static VALUE rb_delta_bytes_compress(VALUE self, VALUE rvalues) {
  int rlen = RARRAY_LEN(rvalues);

  int offset = 0;
  uint8_t *dest = (uint8_t*)calloc(5 * rlen, sizeof(uint8_t));

  uint32_t previous = 0;
  for (int i = 0; i < rlen; i++) {
    uint32_t value = NUM2ULONG(rb_ary_entry(rvalues, i));
    offset += varint_write_unsigned(value - previous, dest + offset);
    previous = value;
  }

  VALUE rary = rb_ary_new2(offset);
  for (int i = 0; i < offset; i++) {
    rb_ary_push(rary, INT2NUM(dest[i]));
  }
  free(dest);

  return rary;
}

.uncompress(rcompressed) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/hyperll/delta_bytes.c', line 60

static VALUE rb_delta_bytes_uncompress(VALUE self, VALUE rcompressed) {
  // copy to a native array
  int rlen = RARRAY_LEN(rcompressed);
  uint8_t *compressed = (uint8_t*)calloc(rlen, sizeof(uint8_t));
  for (int i = 0; i < rlen; i++) {
    compressed[i] = (uint8_t)NUM2INT(rb_ary_entry(rcompressed, i));
  }

  uint32_t *values = (uint32_t*)calloc(rlen, sizeof(uint32_t));
  int vlen = delta_bytes_uncompress(compressed, rlen, values);
  if (vlen < 0) {
    rb_raise(rb_eRuntimeError, "corrupted values");
    goto error;
  }

  VALUE rary = rb_ary_new2(vlen);
  for (int i = 0; i < vlen; i++) {
    rb_ary_push(rary, ULONG2NUM(values[i]));
  }

  free(compressed);
  free(values);
  return rary;
error:
  free(compressed);
  free(values);
  return Qnil;
}