Class: FrequencyEnumerator

Inherits:
Enumerable::Enumerator
  • Object
show all
Defined in:
lib/frequency_enumerator/base.rb

Defined Under Namespace

Classes: Composer, Decomposer, Sorter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frequencies, params = {}) ⇒ FrequencyEnumerator

Returns a new instance of FrequencyEnumerator.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/frequency_enumerator/base.rb', line 5

def initialize(frequencies, params = {})
  @frequencies = frequencies

  @bit_count  = params[:bit_count]  || 6
  @from       = params[:from]       || 0
  @to         = params[:to]         || limit
  @offset     = params[:offset]     || 0

  raise_if_either_boundary_is_out_of_range

  @sorter     = params[:sorter]     || fe::Sorter
  @composer   = params[:composer]   || fe::Composer
  @decomposer = params[:decomposer] || fe::Decomposer
end

Instance Attribute Details

#bit_countObject (readonly)

Returns the value of attribute bit_count.



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

def bit_count
  @bit_count
end

#frequenciesObject (readonly)

Returns the value of attribute frequencies.



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

def frequencies
  @frequencies
end

#fromObject (readonly)

Returns the value of attribute from.



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

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



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

def to
  @to
end

Instance Method Details

#each(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/frequency_enumerator/base.rb', line 20

def each(&block)
  (from..to).each do |i|
    binary = decomposer.decompose(i)
    bitmap = fragmented_bitmap(binary)
    result = composition(bitmap)
    yield offset(result)
  end

  self
end

#limitObject



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

def limit
  @limit ||= (2 ** bit_count) ** frequencies.size - 1
end