-
Notifications
You must be signed in to change notification settings - Fork 8
/
TransTable.cpp
221 lines (195 loc) · 5.75 KB
/
TransTable.cpp
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
///////////////////////////////////////////////////////////////////////////////
// Title : TransTable
// Date : March 20, 2002
// Author : Kristina Klinkner
// Description : creates a table of transitions from all max length strings
// for use with CSSR
///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2002 Kristina Klinkner
// This file is part of CSSR
//
// CSSR is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// CSSR is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with CSSR; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//////////////////////////////////////////////////////////////////////////////
#include "TransTable.h"
///////////////////////////////////////////////////////////////////////////////
TransTable::TransTable(int numStates)
{
m_transArray = new TransNode*[numStates];
m_parentArray = new TransNode*[numStates];
m_transCounts = new int[numStates];
for(int i =0; i< numStates;i++)
{
m_transArray[i] = NULL;
m_parentArray[i] = NULL;
m_transCounts[i] = 0;
}
m_arraySize = numStates;
}
TransTable::~TransTable()
{
TransNode* temp;
TransNode* temp2;
delete[] m_transCounts;
//delete lists
for(int i = 0; i < m_arraySize; i++)
{
if(m_transArray[i])
temp = m_transArray[i]->nextPtr;
else
temp = NULL;
while(temp)
{
temp->stringPtr = NULL;
temp2 = temp->nextPtr;
delete temp;
temp = temp2;
}
}
//delete lists
for(int k = 0; k < m_arraySize; k++)
{
if(m_parentArray[k])
temp = m_parentArray[k]->nextPtr;
else
temp = NULL;
while(temp)
{
temp->stringPtr = NULL;
temp2 = temp->nextPtr;
delete temp;
temp = temp2;
}
}
//delete all initial pointers
delete[] m_transArray;
delete[] m_parentArray;
}
void TransTable::setTrans(int transState, StringElem* string, int parentState)
{
if(transState == NULL_STATE)
return;
else
{
//create a new TransNode and put it at the front of the list
if((transState >= 0) && (transState < m_arraySize))
{
TransNode* temp1 = new TransNode;
TransNode* temp3 = new TransNode;
if(temp1 == NULL || temp3 == NULL)
{
cerr << "Out of memory." << endl;
exit(1);
}
//set array which finds by transition state
temp1->stringPtr = string;
temp1->state = parentState;
temp1->nextPtr = NULL;
TransNode* temp2;
temp2 = m_transArray[transState];
m_transArray[transState] = temp1;
temp1->nextPtr = temp2;
//set array which finds by parent state
temp2 = NULL;
temp3->stringPtr = string;
temp3->state = transState;
temp3->nextPtr = NULL;
temp2 = m_parentArray[parentState];
m_parentArray[parentState] = temp3;
temp3->nextPtr = temp2;
//set counters
m_transCounts[transState]++;
}
else
cerr << "TransTable::setTrans::Invalid state "<<endl;
}
}
void TransTable::RemoveStringsAndState(int removalState, int removeAdjust,
int lowest)
{
TransNode* removeTemp;
TransNode* removeTemp2;
TransNode* ptrTemp1;
TransNode* ptrTemp2;
int transState = NULL_STATE;
int i = 0;
//get rid of nodes which transition from bad state
//first node which is an element of a bad state
removeTemp = m_parentArray[removalState];
while(removeTemp)
{
//transition entry containing that node
transState = removeTemp->state;
if(transState > lowest)
transState = transState - removeAdjust;
ptrTemp1 = m_transArray[transState];
if(ptrTemp1)
{
//if the matching node is the first in the entry remove it
if(ptrTemp1->stringPtr == removeTemp->stringPtr)
{
m_transArray[transState] = ptrTemp1->nextPtr;
ptrTemp1->stringPtr = NULL;
delete ptrTemp1;
}
else
{
//if not, find it
while(ptrTemp1->stringPtr != removeTemp->stringPtr)
{
ptrTemp2 = ptrTemp1;
ptrTemp1 = ptrTemp1->nextPtr;
}
//remove node while keeping linked list together
ptrTemp2->nextPtr = ptrTemp1->nextPtr;
ptrTemp1->stringPtr = NULL;
delete ptrTemp1;
}
}
//move on to next node to remove all nodes
removeTemp2 = removeTemp;
removeTemp = removeTemp->nextPtr;
//get rid of entry in parent Array
delete removeTemp2;
}
//get rid of nodes which transition to bad state
removeTemp = m_transArray[removalState];
while(removeTemp)
{
removeTemp2 = removeTemp;
removeTemp = removeTemp->nextPtr;
removeTemp2->stringPtr = NULL;
delete removeTemp2;
}
//lastly, remove the bad state from both arrays
for(i = removalState; i < m_arraySize - 1; i++)
{
m_parentArray[i] = m_parentArray[i + 1];
m_transArray[i] = m_transArray[i+1];
}
m_parentArray[i] = NULL;
m_transArray[i] = NULL;
m_arraySize--;
}
int TransTable::getCounts(int state)
{
return m_transCounts[state];
}
TransNode* TransTable::WhichStrings(int state)
{
return m_transArray[state];
}