Class: FrequencyEnumerator::Sorter::AccumulationHelper
- Inherits:
-
Object
- Object
- FrequencyEnumerator::Sorter::AccumulationHelper
- Defined in:
- lib/frequency_enumerator/sorter.rb
Instance Attribute Summary collapse
-
#bit_count ⇒ Object
readonly
Returns the value of attribute bit_count.
-
#frequencies ⇒ Object
readonly
Returns the value of attribute frequencies.
Instance Method Summary collapse
- #accumulate(key) ⇒ Object
- #accumulation ⇒ Object
- #available_keys ⇒ Object
- #consume(key) ⇒ Object
- #depleted_keys? ⇒ Boolean
-
#initialize(frequencies, bit_count = 6) ⇒ AccumulationHelper
constructor
A new instance of AccumulationHelper.
- #maximal_key ⇒ Object
- #probabilities ⇒ Object
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_count ⇒ Object (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 |
#frequencies ⇒ Object (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 |
#accumulation ⇒ Object
54 55 56 |
# File 'lib/frequency_enumerator/sorter.rb', line 54 def accumulation @accumulation ||= probabilities.dup end |
#available_keys ⇒ Object
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
35 36 37 |
# File 'lib/frequency_enumerator/sorter.rb', line 35 def depleted_keys? available_keys.empty? end |
#maximal_key ⇒ Object
39 40 41 |
# File 'lib/frequency_enumerator/sorter.rb', line 39 def maximal_key accumulation.max_by { |_, v| v }.first end |
#probabilities ⇒ Object
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 |