-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatesUtil.h
66 lines (55 loc) · 1.69 KB
/
StatesUtil.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once
#include <string>
#include <unordered_map>
#include<set>
#include <utility>
#include <tuple>
#include <stdexcept>
#include <ostream>
#include "Tape.h"
using std::string;
using std::unordered_map;
using std::set;
using std::pair;
using std::tuple;
using std::hash;
using std::out_of_range;
using std::get;
using std::ostream;
class StatesUtil;
struct State;
enum class TapeMovement { LEFT, RIGHT, STAY };
enum class StateType { ACCEPTING, REJECTING, NORMAL };
typedef pair<string, char> state_pair;
typedef tuple<string, char, TapeMovement> state_tuple;
typedef unordered_map<string, StateType> state_map;
typedef unordered_map<state_pair, state_tuple> transitions_map;
namespace std {
template<>
struct hash<state_pair> {
size_t operator()(state_pair const& v) const {
size_t s1 = hash<string>{}(v.first);
size_t s2 = hash<char>{}(v.second);
return s1 ^ (s2 << 1);
}
};
}
class StatesUtil {
friend class MachineBuilder;
friend class Machine;
public:
StatesUtil(state_map states = state_map(), transitions_map transitions = transitions_map(), string current = "", string starting = "") : states(states), transitions(transitions), current(current), starting(starting) {};
StateType transition(Tape& tape);
bool addTrasition(string from, string to, char old_tape, char new_tape, TapeMovement movement);
bool addState(string name, StateType type);
void printStates(ostream& os, bool yellow_starting) const;
void printTransitions(ostream& os) const;
StatesUtil& compose(const StatesUtil& other) const;
private:
string starting;
string current;
state_map states;
transitions_map transitions;
void moveTape(Tape& tape, TapeMovement movement);
void setCurrent(string newCurrent);
};