File: Fsm.h

package info (click to toggle)
grcompiler 4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,076 kB
  • ctags: 5,163
  • sloc: cpp: 45,565; sh: 4,451; ansic: 4,377; makefile: 185; xml: 175; perl: 127
file content (327 lines) | stat: -rw-r--r-- 7,363 bytes parent folder | download | duplicates (4)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.

Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.

File: Fsm.h
Responsibility: Sharon Correll
Last reviewed: Not yet.

Description:
    Defines the class that are used to generate the finite state machines.
-------------------------------------------------------------------------------*//*:End Ignore*/
#ifdef _MSC_VER
#pragma once
#endif
#ifndef FSM_INCLUDED
#define FSM_INCLUDED


typedef std::set<GdlGlyphClassDefn *> SourceClassSet;	// hungarian: scs
typedef std::vector<FsmMachineClass *> * MachineClassList;	// hungarian: mcl

void OutputNumber(std::ostream& strmOut, int nValue, int nSpaces);

/*----------------------------------------------------------------------------------------------
Class: FsmMachineClass
Description: A machine class corresponds to one column in the FSM matrix.
Hungarian: fsmc
----------------------------------------------------------------------------------------------*/
class FsmMachineClass
{
	friend class FsmTable;
	friend class GdlPass;
	friend class FsmcLess;

public:
	FsmMachineClass(int nPass)
	{
		m_nPass = nPass;
	}

	int Column()
	{
		return m_ifsmcColumn;
	}

	void SetColumn(int ifsmc)
	{
		m_ifsmcColumn = ifsmc;
	}

	void SetSourceClasses(SourceClassSet * pscs)
	{
		for (SourceClassSet::iterator itscs = pscs->begin();
			itscs != pscs->end();
			++itscs)
		{
			m_scs.insert(*itscs);
		}
	}

	void AddGlyph(utf16 w);

	int Key(int ipass);

	bool MatchesSources(SourceClassSet *);
	bool MatchesOneSource(GdlGlyphClassDefn *);

	//	Output:
	int NumberOfRanges();
	utf16 OutputRange(utf16 wGlyphID, GrcBinaryStream * pbstrm);

protected:
	int m_nPass;

	SourceClassSet m_scs;	// source-class-set corresponding to this machine-class
	std::vector<utf16> m_wGlyphs;
	int m_ifsmcColumn;	// column this machine class is assigned to
	std::string staDebug;

	// Comparison function for set manipulation:
	bool operator<(const FsmMachineClass & fsmc) const
	{
		if (this == &fsmc)
			return false; // equal, not less than
		if (this->m_wGlyphs.size() != fsmc.m_wGlyphs.size())
			return (this->m_wGlyphs.size() < fsmc.m_wGlyphs.size());
		for (size_t iw = 0; iw < m_wGlyphs.size(); iw++)
		{
			if (this->m_wGlyphs[iw] != fsmc.m_wGlyphs[iw])
				return (this->m_wGlyphs[iw] < fsmc.m_wGlyphs[iw]);
		}
		return false; // equal, not less than
	}
};


// Functor class for set manipulation
class FsmcLess
{
	friend class FsmMachineClass;
public:
	bool operator()(const FsmMachineClass * pfsmc1, const FsmMachineClass * pfsmc2) const
	{
		return (*pfsmc1 < *pfsmc2);
	}
};

typedef std::set<FsmMachineClass *, FsmcLess> FsmMachineClassSet;
//typedef std::set<FsmMachineClass *> FsmMachineClassSet;

/*----------------------------------------------------------------------------------------------
Class: FsmState
Description: A row in the FSM table.
Hungarian: fstate
----------------------------------------------------------------------------------------------*/
class FsmState
{
	friend class FsmTable;
	friend class GdlPass;

public:
	FsmState(int ccol, int critSlots, int ifsIndex)
	{
		Init(ccol, critSlots, ifsIndex);
	}

	FsmState()
	{
		Assert(false);
		m_cfsmc = 0;
		m_prgiCells = NULL;
		m_pfstateMerged = NULL;
	}

	void Init(int cfsmc, int critSlots, int ifsIndex)
	{
		m_cfsmc = cfsmc;
		m_prgiCells = new int[cfsmc];
		memset(m_prgiCells, 0, cfsmc * sizeof(int));

		m_critSlotsMatched = critSlots;
		m_ifsWorkIndex = ifsIndex;
		m_ifsFinalIndex = -1;
		m_pfstateMerged = NULL;
	}

	~FsmState()
	{
		if (m_prgiCells)
			delete[] m_prgiCells;
	}

	int SlotsMatched()
	{
		return m_critSlotsMatched;
	}

	int WorkIndex()
	{
		return m_ifsWorkIndex;
	}

	int CellValue(int ifsmc)
	{
		Assert(m_prgiCells);
		return m_prgiCells[ifsmc];
	}

	void SetCellValue(int ifsmc, int ifsValue)
	{
		Assert(m_prgiCells);
		m_prgiCells[ifsmc] = ifsValue;
	}

	int NumberOfRulesMatched()
	{
		return m_setiruleMatched.size();
	}

	bool RuleMatched(int irule)
	{
		return (m_setiruleMatched.find(irule) != m_setiruleMatched.end()); // is a member
	}

	void AddRuleToMatchedList(int irule)
	{
		if (m_setiruleMatched.find(irule) == m_setiruleMatched.end()) // not a member
			m_setiruleMatched.insert(irule);
	}

	int NumberOfRulesSucceeded()
	{
		return m_setiruleSuccess.size();
	}

	bool RuleSucceeded(int irule)
	{
		return (m_setiruleSuccess.find(irule) != m_setiruleSuccess.end()); // is a member
	}

	void AddRuleToSuccessList(int irule)
	{
		if (m_setiruleSuccess.find(irule) == m_setiruleSuccess.end())  // not a member
			m_setiruleSuccess.insert(irule);
	}

	bool StatesMatch(FsmState * pfState);

	bool HasBeenMerged()
	{
		return (m_pfstateMerged != NULL);
	}

	FsmState * MergedState()
	{
		return m_pfstateMerged;
	}

	void SetMergedState(FsmState * pfsmc)
	{
		m_pfstateMerged = pfsmc;
	}

	void SetFinalIndex(int n)
	{
		m_ifsFinalIndex = n;
	}

	bool AllCellsEmpty()
	{
		for (int ifsmc = 0; ifsmc < m_cfsmc; ifsmc++)
		{
			if (m_prgiCells[ifsmc] != 0)
				return false;
		}
		return true;
	}

protected:
	int m_critSlotsMatched;	// number of slots matched at this state
	int m_cfsmc;			// number of machine classes, ie columns
	int m_ifsWorkIndex;		// working index of this state, as the FSM was originally
							// generated (currently just used for debugging)
	int m_ifsFinalIndex;	// adjusted index for final output form of FSM (-1 for merged
							// states)

	std::set<int> m_setiruleMatched;	// indices of rules matched by this state
	std::set<int> m_setiruleSuccess;	// indices of rules for which this is a success state

	int * m_prgiCells;	// the cells of the state, holding the index of the state
						// to transition to


	FsmState * m_pfstateMerged;		// pointer to an earlier state that is identical to this
									// one (if any), so this state is considered merged with
									// that one


public:
	//	Debuggers:
	void DebugFsmState(std::ostream & strmOut, int ifs);
};



/*----------------------------------------------------------------------------------------------
Class: FsmTable
Description: A single finite state machine.
Hungarian: fsm
----------------------------------------------------------------------------------------------*/
class FsmTable
{
	friend class GdlPass;

public:
	FsmTable(int nPass, int cfsmc)
	{
		m_nPass = nPass;
		m_cfsmc = cfsmc;
	}

	~FsmTable()
	{
		for (size_t ipfstate = 0; ipfstate < m_vpfstate.size(); ipfstate++)
			delete m_vpfstate[ipfstate];
	}

	void AddState(int critSlotsMatched)
	{
		FsmState * pfstateNew = new FsmState(m_cfsmc, critSlotsMatched, m_vpfstate.size());
		m_vpfstate.push_back(pfstateNew);
	}

	FsmState * StateAt(int ifs)
	{
		FsmState * pfstateRet = RawStateAt(ifs);
		if (pfstateRet->HasBeenMerged())
			return pfstateRet->MergedState();

		return pfstateRet;
	}

	FsmState * RawStateAt(int ifs)
	{
		return m_vpfstate[ifs];
	}

	int RawNumberOfStates()
	{
		return signed(m_vpfstate.size());
	}

	int NumberOfColumns()
	{
		return m_cfsmc;
	}

protected:
	int m_nPass;	// the pass this table pertains to
	int m_cfsmc;	// the number of machine classes, ie columns, in the table
	std::vector<FsmState *> m_vpfstate;	// the rows
};


#endif // !FSM_INCLUDED