Class: Flexr::Automaton::TransitionRows
- Inherits:
-
Object
- Object
- Flexr::Automaton::TransitionRows
- Includes:
- Enumerable
- Defined in:
- lib/flexr/automaton/dfa.rb
Instance Attribute Summary collapse
-
#length ⇒ Object
(also: #size)
readonly
Returns the value of attribute length.
Instance Method Summary collapse
- #[](state) ⇒ Object
- #each ⇒ Object
- #each_index ⇒ Object
- #fetch(state) ⇒ Object
-
#initialize(length, &loader) ⇒ TransitionRows
constructor
A new instance of TransitionRows.
- #materialized_count ⇒ Object
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
#length ⇒ Object (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 |
#each ⇒ Object
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_index ⇒ Object
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
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_count ⇒ Object
43 44 45 |
# File 'lib/flexr/automaton/dfa.rb', line 43 def materialized_count @rows.count { |row| !row.nil? } end |