summaryrefslogtreecommitdiff
path: root/utils/Automaton.cc
blob: 9643bdb785921e10da8601bd5f7c723b2125b07f (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1993-1995
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	Automata.
 *
 *	$Id$
 *	$CurLog$
 */

#include "Automaton.h"

CcuBaseLink :: CcuBaseLink (CcuBaseState* s)
: To (s)
{
}

CcuBaseLink :: ~CcuBaseLink ()
{
}

CcuBaseState :: CcuBaseState (const char* name, CcuBaseAutomaton& a)
: Name (name),
  Links (a.Size, a.Hash, a.Copy, a.Delete, a.Compare),
  TheAutomaton (&a)
{
}

CcuBaseState :: ~CcuBaseState ()
{
	CcuHashIterOf <CcuBaseLink> l = Links;
	while (++l)
		delete *l;
}

#if 0
void
CcuBaseState :: CreateLink (CcuBaseState* to, CcuKey* k)
{
	CcuHashCellOf <CcuBaseLink>* c = Links.Add (k);
	c->SetInfo (new CcuBaseLink (to));
}
#endif

#if 0
void
CcuBaseState :: Add (CcuKey* key, CcuBaseLink* l)
{
	CcuHashCellOf <CcuLink>* c = Links.Add (key);
	c->SetInfo (l);
}
#endif

CcuBaseState*
CcuBaseState :: Next (CcuToken* tk, bool peek)
{
	CcuKey* k = TheAutomaton->GetKey ? (TheAutomaton->GetKey) (tk) : tk;
	CcuHashCellOf <CcuBaseLink>* c = Links.Get (k);
	if (!c)
		return 0;
	CcuBaseLink* l = c->GetInfo ();
	CcuBaseState* to = l->GetDestination ();

	if (!peek) {
		Out ();
		l->Fire (this, to, tk);
		to->In ();
	}
	return to;
}

CcuBaseAutomaton :: CcuBaseAutomaton (AKEY_F gk, HASH_F hash, HCP_F cp, HDEL_F del, HCMP_F cmp, int sz)
: Size (sz),
  Hash (hash),
  Copy (cp),
  Delete (del),
  Compare (cmp),
  GetKey (gk),
  Initial (0),
  AllStates ()
{
}

CcuBaseAutomaton :: ~CcuBaseAutomaton ()
{
	CcuListIterOf <CcuBaseState> s = AllStates;
	while (++s)
		delete *s;
}

#if 0
CcuBaseState*
CcuBaseAutomaton :: CreateState (const char* n);
{
	CcuBaseState* s = new CcuBaseState (n, *this);
	AllStates.Append (s);
	return s;
}
#endif


CcuLink :: CcuLink (CcuState* s, CcuLinkFun fn)
: CcuBaseLink (s),
  Fun (fn)
{
}

CcuLink :: ~CcuLink ()
{
}

void
CcuLink :: Fire (CcuBaseState* from, CcuBaseState* to, CcuToken* data)
{
	if (Fun)
		(*Fun) ((CcuState*) from, (CcuState*) to, data);
}

CcuState :: CcuState (const char* name, CcuAutomaton& a, CcuStateFun* in, CcuStateFun* out)
: CcuBaseState (name, a),
  InFun (in),
  OutFun (out)
{
}

CcuState :: ~CcuState ()
{
}

void
CcuState :: In ()
{
	if (InFun) (*InFun) (this);
}

void
CcuState :: Out ()
{
	if (OutFun) (*OutFun) (this);
}

void
CcuState :: CreateLink (CcuState* to, CcuKey* k, CcuLinkFun fn)
{
	CcuHashCellOf <CcuBaseLink>* c = Links.Add (k);
	c->SetInfo (new CcuLink (to, fn));
}

CcuState*
CcuAutomaton :: CreateState (const char* n, CcuStateFun in, CcuStateFun out)
{
	CcuState* s = new CcuState (n, *this, in, out);
	AllStates.Append (s);
	return s;
}

CcuAutomIter :: CcuAutomIter (CcuBaseAutomaton& a)
: TheAutomaton (&a),
  CurState (a.GetInitial ())
{
}

CcuAutomIter :: ~CcuAutomIter ()
{
}

bool
CcuAutomIter :: Step (CcuToken* tk)
{
	if (!CurState && !(CurState = TheAutomaton->GetInitial ()))
		return false;
	CcuBaseState* n = CurState->Next (tk);
	if (!n)
		return false;
	CurState = n;
	return true;
}