summaryrefslogtreecommitdiff
path: root/utils/Automaton.h
blob: c865b762c0731b8b56d88790053d3418e764c4cd (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
/*
 *	CENA C++ Utilities
 *
 *	by Stephane Chatty
 *
 *	Copyright 1992-1995
 *	Centre d'Etudes de la Navigation Aerienne (CENA)
 *
 *	Automata.
 *
 *	$Id$
 *	$CurLog$
 */

#ifndef IvlAutomaton_H_
#define IvlAutomaton_H_

#include "cplus_bugs.h"
#include "List.h"
#include "HashTable.h"
#include "String.h"
#include "bool.h"

typedef void IvlToken;
typedef void IvlKey;
class IvlBaseState;
class IvlBaseAutomaton;

class IvlBaseLink {
protected:
	IvlBaseState*	To;
public:
		IvlBaseLink (IvlBaseState*);
virtual		~IvlBaseLink ();
virtual	void	Fire (IvlBaseState*, IvlBaseState*, IvlToken*) = 0;
inline	IvlBaseState*	GetDestination () const	{ return To;}
};

class IvlBaseState {
protected:
	IvlString	Name;
	IvlHashTableOf <IvlBaseLink>	Links;
	IvlBaseAutomaton*	TheAutomaton;

virtual	void	In () = 0;
virtual	void	Out () = 0;

public:
		IvlBaseState (const char*, IvlBaseAutomaton&);
virtual		~IvlBaseState ();
	void	Add (IvlBaseLink*);
	void	CreateLink (IvlBaseState*, IvlKey*);
	void	RemoveLink (IvlBaseLink*);
	IvlBaseState*	Next (IvlToken*, bool = false);
};

#if 0
typedef unsigned int (*HASH_F) (const IvlKey*, int);
typedef int (*HCMP_F) (const IvlKey*, const IvlKey*);
typedef IvlKey* (*HCP_F) (const IvlKey*);
typedef void (*HDEL_F) (const IvlKey*);
#endif
typedef IvlKey* (*AKEY_F) (IvlToken*);

class IvlBaseAutomaton {
friend	class	IvlBaseState;
friend	class	IvlAutomIter;
protected:
	int	Size;
	HASH_F	Hash;
	HCP_F	Copy;
	HDEL_F	Delete;
	HCMP_F	Compare;
	AKEY_F	GetKey;
	IvlBaseState*	Initial;
	IvlListOf <IvlBaseState>	AllStates;
public:
		IvlBaseAutomaton (AKEY_F = 0, HASH_F = 0, HCP_F = 0, HDEL_F = 0, HCMP_F = 0, int sz = 4);
		~IvlBaseAutomaton ();
inline	IvlBaseState*	GetInitial ()	{ return Initial; }
inline	void	SetInitial (IvlBaseState* s)	{ Initial = s; }
	IvlBaseState*	CreateState (const char* = 0);
};

class IvlState;
typedef void (IvlLinkFun) (IvlState*, IvlState*, IvlToken*);

class IvlLink : public IvlBaseLink {
protected:
	IvlLinkFun*	Fun;
public:
		IvlLink (IvlState*, IvlLinkFun*);
		~IvlLink ();
	void	Fire (IvlBaseState*, IvlBaseState*, IvlToken*);
};

typedef void (IvlStateFun) (IvlState*);
class IvlAutomaton;

class IvlState : public IvlBaseState {
protected:
	IvlStateFun*	InFun;
	IvlStateFun*	OutFun;

	void	In ();
	void	Out ();

public:
		IvlState (const char*, IvlAutomaton&, IvlStateFun* = 0, IvlStateFun* = 0);
		~IvlState ();
	void	CreateLink (IvlState*, IvlKey*, IvlLinkFun);
};

class IvlAutomaton : public IvlBaseAutomaton {
public:
inline		IvlAutomaton () : IvlBaseAutomaton ()	{}
inline		~IvlAutomaton ()	{}
	IvlState*	CreateState (const char* = 0, IvlStateFun = 0, IvlStateFun = 0);
};

class IvlAutomIter {
protected:
	IvlBaseAutomaton*	TheAutomaton;
	IvlBaseState*	CurState;
public:
		IvlAutomIter (IvlBaseAutomaton&);
		~IvlAutomIter ();
	bool	Step (IvlToken*);
};

#endif	/* IvlAutomaton_H_ */