Class: Hyperll::DeltaBytes

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperll/delta_bytes.rb

Class Method Summary collapse

Class Method Details

.compress(bytes) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/hyperll/delta_bytes.rb', line 5

def self.compress(bytes)
  compressed = Varint.write_unsigned_var_int(bytes.length)
  previous_value = 0

  bytes.each do |b|
    compressed.concat(Varint.write_unsigned_var_int(b - previous_value))
    previous_value = b
  end

  compressed
end

.uncompress(bytes) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hyperll/delta_bytes.rb', line 17

def self.uncompress(bytes)
  uncompressed = []
  previous_value = 0

  length = Varint.read_unsigned_var_int(bytes)
  length.times do
    next_value = Varint.read_unsigned_var_int(bytes)

    uncompressed << next_value + previous_value
    previous_value = uncompressed.last
  end

  uncompressed
end