Class: Tamper::IntegerPack

Inherits:
Pack
  • Object
show all
Defined in:
lib/tamper/integer_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, #finalize_pack!, #initialize, #to_h

Constructor Details

This class inherits a constructor from Tamper::Pack

Instance Method Details

#encode(idx, data) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tamper/integer_pack.rb', line 8

def encode(idx, data)
  choice_data = data[attr_name.to_sym] || data[attr_name.to_s]
  choice_data = [choice_data] unless choice_data.is_a?(Array)

  (0...max_choices).each do |choice_idx|
    choice_offset = (item_window_width * idx) + (bit_window_width * choice_idx)

    value = choice_data[choice_idx]

    # TODO: test and handle nil case
    if possibility_idx = possibilities.index(value.to_s)
      possibility_id =  possibility_idx + 1
    else
      possibility_id = 0
    end

    bit_code = possibility_id.to_i.to_s(2).split('') # converts to str binary representation
    bit_code_length_pad = bit_window_width - bit_code.length
    bit_code.each_with_index do |bit, bit_idx|
      byebug if (choice_offset + bit_code_length_pad + bit_idx) == -1
      @bitset[(choice_offset + bit_code_length_pad + bit_idx)] = bit == "1"
    end
  end

end

#encodingObject



4
5
6
# File 'lib/tamper/integer_pack.rb', line 4

def encoding
  :integer
end

#initialize_pack!(max_guid, num_items) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/tamper/integer_pack.rb', line 34

def initialize_pack!(max_guid, num_items)
  @bit_window_width = Math.log2(possibilities.length + 1).ceil  # add 1 to possiblities.length for the implicit nil possiblity.
  @bit_window_width = 1        if @bit_window_width == 0  # edge case: 1 possibility

  @item_window_width = bit_window_width * max_choices
  @bitset = Bitset.new(item_window_width * num_items)
end