Class: FrequencyEnumerator::Sorter::AccumulationHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/frequency_enumerator/sorter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frequencies, bit_count = 6) ⇒ AccumulationHelper

Returns a new instance of AccumulationHelper.



30
31
32
33
# File 'lib/frequency_enumerator/sorter.rb', line 30

def initialize(frequencies, bit_count = 6)
  @frequencies = frequencies
  @bit_count = bit_count
end

Instance Attribute Details

#bit_countObject (readonly)

Returns the value of attribute bit_count.



28
29
30
# File 'lib/frequency_enumerator/sorter.rb', line 28

def bit_count
  @bit_count
end

#frequenciesObject (readonly)

Returns the value of attribute frequencies.



28
29
30
# File 'lib/frequency_enumerator/sorter.rb', line 28

def frequencies
  @frequencies
end

Instance Method Details

#accumulate(key) ⇒ Object



43
44
45
46
# File 'lib/frequency_enumerator/sorter.rb', line 43

def accumulate(key)
  accumulation[key] *= probabilities[key]
  consume(key)
end

#accumulationObject



54
55
56
# File 'lib/frequency_enumerator/sorter.rb', line 54

def accumulation
  @accumulation ||= probabilities.dup
end

#available_keysObject



48
49
50
51
52
# File 'lib/frequency_enumerator/sorter.rb', line 48

def available_keys
  @available_keys ||= frequencies.inject({}) do |hash, (k, _)|
    hash.merge(k => bit_count)
  end
end

#consume(key) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/frequency_enumerator/sorter.rb', line 58

def consume(key)
  available_keys[key] -= 1

  if available_keys[key].zero?
    available_keys.delete(key)
    accumulation.delete(key)
  end
end

#depleted_keys?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/frequency_enumerator/sorter.rb', line 35

def depleted_keys?
  available_keys.empty?
end

#maximal_keyObject



39
40
41
# File 'lib/frequency_enumerator/sorter.rb', line 39

def maximal_key
  accumulation.max_by { |_, v| v }.first
end

#probabilitiesObject



67
68
69
70
71
72
73
# File 'lib/frequency_enumerator/sorter.rb', line 67

def probabilities
  return @probabilities if @probabilities
  total = frequencies.values.inject(:+)
  @probabilities = frequencies.inject({}) do |hash, (k, v)|
    hash.merge(k => v.to_f / total)
  end
end