summaryrefslogtreecommitdiff
path: root/dnn/Automaton.cc
blob: 558a0baf93d91dee41562dd3ac5885478b97e83b (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
/*
 *	DNN - Data News Network
 *
 *	by Stephane Chatty
 *
 *	Copyright 1993-1995
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	Automata.
 *
 *	$Id$
 *	$CurLog$
 */

#include "Automaton.h"

DnnState :: DnnState (const char* name, int sz, DnnStateFun* in, DnnStateFun* out)
: Name (name),
  InFun (in),
  OutFun (out),
  Links (sz)
{
}

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

void
DnnState :: CreateLink (DnnState* to, void* k, DnnLinkFun fn)
{
	CcuHashCellOf <DnnLink>* c = Links.Add (k);
	c->SetInfo (new DnnLink (to, fn));
}

DnnState*
DnnState :: Next (void* k, bool peek)
{
	CcuHashCellOf <DnnLink>* c = Links.Get (k);
	if (!c)
		return 0;
	DnnLink* l = c->GetInfo ();
	DnnState* to = l->To;
	if (!peek) {
		if (OutFun)
			(*OutFun) (this);
		if (l->Fun)
			l->Fun (this, to, k);
		if (to->InFun)
			(*to->InFun) (to);
	}
	return to;
}

DnnAutomaton :: DnnAutomaton (int sz)
: Initial (0),
  Size (sz)
{
}

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

DnnState*
DnnAutomaton :: CreateState (const char* n, DnnStateFun in, DnnStateFun out)
{
	DnnState* s = new DnnState (n, Size, in, out);
	AllStates.Append (s);
	return s;
}

DnnAutomIter :: DnnAutomIter (DnnAutomaton& a)
: TheAutomaton (&a),
  CurState (a.GetInitial ())
{
}

DnnAutomIter :: ~DnnAutomIter ()
{
}

bool
DnnAutomIter :: Step (void* k)
{
	if (!CurState && !(CurState = TheAutomaton->GetInitial ()))
		return false;
	DnnState* n = CurState->Next (k);
	if (!n)
		return false;
	CurState = n;
	return true;
}