Class: Flexr::Automaton::TransitionRows

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/flexr/automaton/dfa.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(length, &loader) ⇒ TransitionRows

Returns a new instance of TransitionRows.



10
11
12
13
14
15
# File 'lib/flexr/automaton/dfa.rb', line 10

def initialize(length, &loader)
  @length = length
  @loader = loader
  @rows = Array.new(length)
  freeze
end

Instance Attribute Details

#lengthObject (readonly) Also known as: size

Returns the value of attribute length.



8
9
10
# File 'lib/flexr/automaton/dfa.rb', line 8

def length
  @length
end

Instance Method Details

#[](state) ⇒ Object



19
20
21
22
23
# File 'lib/flexr/automaton/dfa.rb', line 19

def [](state)
  return unless state&.between?(0, length - 1)

  @rows[state] ||= @loader.call(state).freeze
end

#eachObject



31
32
33
34
35
# File 'lib/flexr/automaton/dfa.rb', line 31

def each
  return enum_for(__method__) unless block_given?

  length.times { |state| yield self[state] }
end

#each_indexObject



37
38
39
40
41
# File 'lib/flexr/automaton/dfa.rb', line 37

def each_index(&)
  return enum_for(__method__) unless block_given?

  length.times(&)
end

#fetch(state) ⇒ Object

Raises:

  • (IndexError)


25
26
27
28
29
# File 'lib/flexr/automaton/dfa.rb', line 25

def fetch(state)
  return self[state] if state.between?(0, length - 1)

  raise IndexError, "transition row #{state} is out of bounds"
end

#materialized_countObject



43
44
45
# File 'lib/flexr/automaton/dfa.rb', line 43

def materialized_count
  @rows.count { |row| !row.nil? }
end