Created
February 3, 2015 18:17
-
-
Save ibnesayeed/13cc58bb1971e7a081de to your computer and use it in GitHub Desktop.
Aruuz StateMachine Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Aruuz | |
| class StateMachine | |
| TRANSITIONS = { | |
| "hindi": { | |
| "-": [2, 4, -1, 0, 5, -1, 7, -1], | |
| "=": [1, 0, 3, -1, 6, 1, -1, 0] | |
| }, | |
| "zamzama": { | |
| "-": [1, 2, -1, -1], | |
| "=": [3, -1, 0, 0] | |
| }, | |
| "orig_hindi": { | |
| "-": [-1, 2, 3, -1], | |
| "=": [1, 0, -1, 1] | |
| } | |
| } | |
| def self.next_state meter, input, cur_state | |
| return -1 if cur_state < 0 | |
| TRANSITIONS[meter][input][cur_state] || -1 | |
| end | |
| end | |
| end | |
| # This is how it will be accessed | |
| Aruuz::StateMachine.next_state "hindi", "-", 1 | |
| # => 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Aruuz | |
| class StateMachine | |
| attr_reader :state | |
| @@transitions = { | |
| "hindi": { | |
| "-": [2, 4, -1, 0, 5, -1, 7, -1], | |
| "=": [1, 0, 3, -1, 6, 1, -1, 0] | |
| }, | |
| "zamzama": { | |
| "-": [1, 2, -1, -1], | |
| "=": [3, -1, 0, 0] | |
| }, | |
| "orig_hindi": { | |
| "-": [-1, 2, 3, -1], | |
| "=": [1, 0, -1, 1] | |
| } | |
| } | |
| def initialize meter | |
| @trans = @@transitions[meter] | |
| @state = 0 | |
| end | |
| def next_state input | |
| @state = @trans[input][@state] || -1 unless @state < 0 | |
| end | |
| end | |
| end | |
| # This is how it will be accessed | |
| sm = Aruuz::StateMachine.new "hindi" | |
| # => 0 | |
| sm.state | |
| # => 0 | |
| sm.next_state "-" | |
| # => 2 | |
| sm.state | |
| # => 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment