Class: FrequencyEnumerator::Sorter

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

Defined Under Namespace

Classes: AccumulationHelper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Sorter

Returns a new instance of Sorter.



5
6
7
# File 'lib/frequency_enumerator/sorter.rb', line 5

def initialize(params = {})
  @bit_count = params[:bit_count] || 8
end

Instance Attribute Details

#bit_countObject (readonly)

Returns the value of attribute bit_count.



3
4
5
# File 'lib/frequency_enumerator/sorter.rb', line 3

def bit_count
  @bit_count
end

Class Method Details

.sort(frequencies) ⇒ Object



9
10
11
# File 'lib/frequency_enumerator/sorter.rb', line 9

def self.sort(frequencies)
  new.sort(frequencies)
end

Instance Method Details

#sort(frequencies) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/frequency_enumerator/sorter.rb', line 13

def sort(frequencies)
  helper = AccumulationHelper.new(frequencies, bit_count)
  sorted_keys = []

  until helper.depleted_keys? do
    key = helper.maximal_key
    sorted_keys << key
    helper.accumulate(key)
  end

  sorted_keys
end