Class: Automaton::Graph
Class Method Summary collapse
-
.from_hash(hash) ⇒ Object
Create from hash.
- .merge_transitions(t1, t2) ⇒ Object
Instance Method Summary collapse
- #[](state) ⇒ Object
-
#key_value_map ⇒ Object
Invokes block once for each element of self, each time yielding key and value.
- #merge(other) ⇒ Object
-
#product(other) ⇒ Object
Creates a new hash where the new keys are the cartesian product of the keys of the old hashes and the new values the pair of values created by self.values_at(new_key.first), other.values_at(new_key.last) => 1, :b => 2.product(:c => 3, :d => 4) #=> :d] => [1, 4], [:a, :c] => [1, 3], [:b, :c] => [2, 3], [:b, :d] => [2, 4].
- #prune(reachable_states) ⇒ Object
- #to_hash ⇒ Object
Methods inherited from Hash
Class Method Details
.from_hash(hash) ⇒ Object
Create from hash
173 174 175 176 177 178 179 180 |
# File 'lib/automaton.rb', line 173 def self.from_hash(hash) # Change from storing the states as an array to a set self.new.replace(hash.value_map do |state,transitions| transitions.value_map do |symbol,states| states.to_set end end) end |
.merge_transitions(t1, t2) ⇒ Object
228 229 230 231 232 233 234 |
# File 'lib/automaton.rb', line 228 def self.merge_transitions(t1,t2) new = {} t1.each do |symbol,states| new[symbol] = (states + (t2[symbol] || Set.new)) end t2.merge(new) end |
Instance Method Details
#[](state) ⇒ Object
208 209 210 |
# File 'lib/automaton.rb', line 208 def [](state) return super || {} end |
#key_value_map ⇒ Object
Invokes block once for each element of self, each time yielding key and value. Creates a new hash from the key => value pairs returned by the block, these pairs should be an array of the form [key, value].
{:a => 1, :b => 2}.key_value_map{|key, value| [key.to_s, value * 2]}
#=> {"a" => 2, "b" => 4}
200 201 202 203 204 205 206 |
# File 'lib/automaton.rb', line 200 def key_value_map self.inject(self.class.new) do |hash, (key,value)| new_key, new_value = yield(key, value) hash[new_key] = new_value hash end end |
#merge(other) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/automaton.rb', line 216 def merge(other) raise ArgumentError, 'Merging with something that is not a TransitionFunction' unless other.is_a?(Graph) new = {} self.each do |state, transitions| new[state] = Graph.merge_transitions(other[state],transitions) end other.each do |state, transitions| new[state] = transitions unless new.has_key?(state) end Graph.from_hash(new) end |
#product(other) ⇒ Object
Creates a new hash where the new keys are the cartesian product of the keys of the old hashes and the new values the pair of values created by self.values_at(new_key.first), other.values_at(new_key.last)
{:a => 1, :b => 2}.product(:c => 3, :d => 4)
#=> {[:a, :d] => [1, 4], [:a, :c] => [1, 3], [:b, :c] => [2, 3], [:b, :d] => [2, 4]}
187 188 189 190 191 192 |
# File 'lib/automaton.rb', line 187 def product(other) self.keys.product(other.keys).inject(self.class.new) do |hash, (key1, key2)| hash[[key1,key2]] = [self[key1],other[key2]] hash end end |
#prune(reachable_states) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/automaton.rb', line 236 def prune(reachable_states) pruned = self.select do |state,_| reachable_states.include? state end.to_h Graph.from_hash(pruned) end |
#to_hash ⇒ Object
212 213 214 |
# File 'lib/automaton.rb', line 212 def to_hash Hash.new.replace(self) end |