Class: Tamper::ExistencePack

Inherits:
Pack
  • Object
show all
Defined in:
lib/tamper/existence_pack.rb

Instance Attribute Summary

Attributes inherited from Pack

#attr_name, #bit_window_width, #bitset, #item_window_width, #max_choices, #max_guid, #meta, #possibilities

Instance Method Summary collapse

Methods inherited from Pack

build

Constructor Details

#initializeExistencePack

Returns a new instance of ExistencePack.



4
5
6
7
8
9
# File 'lib/tamper/existence_pack.rb', line 4

def initialize
  @output = ''
  @current_chunk = ''
  @last_guid   = 0
  @run_counter = 0
end

Instance Method Details

#encode(guid) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tamper/existence_pack.rb', line 18

def encode(guid)
  guid_diff  = guid.to_i - @last_guid
  guid_diff += 1 if @current_chunk.empty? && @output.empty? && guid.to_i > 0

  if guid_diff == 1 || guid.to_i == 0  # guid is 1 step forward
    @current_chunk << '1'
    @run_counter += 1

  elsif guid_diff <= 0  # somehow we went backwards or didn't change guid on iteration
    raise ArgumentError, "Error: data was not sorted by GUID (got #{@last_guid}, then #{guid})!"

  elsif guid_diff > 40  # big gap, encode with skip control char
    dump_keep(@current_chunk, @run_counter)

    @output += control_code(:skip, guid_diff - 1)
    @current_chunk = '1'
    @run_counter   = 1

  else # skips < 40 should just be encoded as '0'
    if @run_counter > 40    # first check if a run came before this 0; if so dump it
      dump_keep(@current_chunk, @run_counter)
      @current_chunk = ''
      @run_counter = 0
    end

    @current_chunk += ('0' * (guid_diff - 1))
    @current_chunk << '1'
    @run_counter = 1
  end

  @last_guid = guid.to_i
end

#encodingObject



14
15
16
# File 'lib/tamper/existence_pack.rb', line 14

def encoding
  :existence
end

#finalize_pack!Object



51
52
53
54
55
# File 'lib/tamper/existence_pack.rb', line 51

def finalize_pack!
  dump_keep(@current_chunk, @run_counter)
  raise "Encoding error, #{@output.length} is not an even number of bytes!" if @output.length % 8 > 0
  @bitset = Bitset.from_s(@output)
end

#initialize_pack!(max_guid, num_items) ⇒ Object



11
12
# File 'lib/tamper/existence_pack.rb', line 11

def initialize_pack!(max_guid, num_items)
end

#to_hObject



57
58
59
60
# File 'lib/tamper/existence_pack.rb', line 57

def to_h
  { encoding: encoding,
    pack: encoded_bitset }
end