-
Notifications
You must be signed in to change notification settings - Fork 8
/
States.cpp
359 lines (315 loc) · 11 KB
/
States.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/////////////////////////////////////////////////////////////////////////////
//Title: States.cpp
//Author: Kristina Klinkner
//Date: March 20, 2002
//Description: Class for state object. The state object represents a
// causal state, which contains all histories which
// determine the causal state. 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 "States.h"
///////////////////////////////////////////////////////////////////////////
// Function: State::Insert
// Purpose: adds a new string to the linked list in a state
// In Params: string for state
// Out Params: any new lists created
// In/Out Params: hash table of pointers to strings and states
// Pre- Cond: state has been initialized
// Post-Cond: string has been added to state
//////////////////////////////////////////////////////////////////////////
void State::Insert(char string[], HashTable* table)
{
//create temporary String Element
StringElem *temp = new StringElem(m_distributionSize);
temp->m_string = new char[strlen(string) + 1];
//copy new string into String Element
strcpy(temp->m_string,string);
//enter in hash table
table->Insert(temp, this);
//if first in list, set up head and tail pointers
if(m_listSize == 0)
{
m_StringList = m_listTail = temp;
m_listSize++;
}
//otherwise put new element last in list
else
{
m_listTail->m_nextPtr = temp;
m_listTail = temp;
temp = NULL;
m_listSize++;
}
}
///////////////////////////////////////////////////////////////////////////
// Function: State::Insert
// Purpose: adds a new string and 'counts array' to the linked list in a state
// In Params: ArrayElem holding string and counts
// Out Params: any new lists created
// In/Out Params: hash table of pointers to strings and states
// Pre- Cond: state has been initialized
// Post-Cond: string has been added to state
//////////////////////////////////////////////////////////////////////////
void State::Insert(ArrayElem* elem, HashTable* table)
{
char* string = elem->getString();
//create temporary String Element
StringElem *temp = new StringElem(m_distributionSize);
temp->m_string = new char[strlen(string) + 1];
//copy new string into String Element
strcpy(temp->m_string,string);
//copy new counts into String Element
for(int i =0; i < m_distributionSize;i++)
temp->m_counts[i] = elem->getCounts()[i];
//enter in hash table
table->Insert(temp, this);
//if first in list, set up head and tail pointers
if(m_listSize == 0)
{
m_StringList = m_listTail = temp;
m_listSize++;
}
//otherwise put new element last in list
else
{
m_listTail->m_nextPtr = temp;
m_listTail = temp;
temp = NULL;
m_listSize++;
}
}
///////////////////////////////////////////////////////////////////////////
// Function: State::Insert
// Purpose: adds a new string and 'counts array' to the linked list in a state
// In Params: String Element to copy and enter
// Out Params: any new lists created
// In/Out Params: hash table of pointers to strings and states
// Pre- Cond: state has been initialized
// Post-Cond: string has been added to state
//////////////////////////////////////////////////////////////////////////
void State::Insert(StringElem* elem, HashTable* table)
{
//create temporary String Element
StringElem *temp = new StringElem(*elem);
//enter in hash table
table->Insert(temp, this);
//if first in list, set up head and tail pointers
if(m_listSize == 0)
{
m_StringList = m_listTail = temp;
m_listSize++;
}
//otherwise put new element last in list
else
{
m_listTail->m_nextPtr = temp;
m_listTail = temp;
temp = NULL;
m_listSize++;
}
}
///////////////////////////////////////////////////////////////////////////
// Function: State::CalcCurrentDist
// Purpose: calculates the frequency of occurrence of each element given
// state - P(element|state)
// In Params: input to program (alphabet and time-series)
// Out Params: any new states created
// In/Out Params: the alphabet and data strings
// Pre- Cond: Alphabet and Data arrays are set, all strings of length one
// have been examined, initial states have been created
// Post-Cond: State has array of probabilities of occurence for each
// element in the alphabet
//////////////////////////////////////////////////////////////////////////
void State::CalcCurrentDist()
{
StringElem* stringElem; //points to current string
//initialize the occurence count for all strings in state
m_occurenceCount = 0;
//set temporary to point to first string
stringElem = m_StringList;
//initialize distribution
for(int j =0; j< m_distributionSize;j++)
m_currentDist[j] = 0;
while(stringElem!= NULL)
{
for(int i = 0; i < m_distributionSize; i++)
{
m_occurenceCount+=stringElem->m_counts[i];
m_currentDist[i]+= (double)stringElem->m_counts[i];
}
stringElem = stringElem->m_nextPtr;
}
//divide all counts by total number of strings in state
if(m_occurenceCount !=0)
{
for(int k =0; k< m_distributionSize;k++)
m_currentDist[k] =((m_currentDist[k])/((double)m_occurenceCount));
}
}
///////////////////////////////////////////////////////////////////////////
// Function: ~State
// Purpose: destructor for State Class
///////////////////////////////////////////////////////////////////////////
State::~State()
{
StringElem *temp1 = m_StringList;
StringElem *temp2;
while (temp1 !=m_listTail)
{
temp2 = temp1->m_nextPtr;
delete temp1;
temp1 = temp2;
}
if(m_listTail) delete m_listTail;
if(m_currentDist != NULL) delete [] m_currentDist;
}
///////////////////////////////////////////////////////////////////////////
//Function: PrintStringList
//Purpose: prints out linked list of strings stored in state
///////////////////////////////////////////////////////////////////////////
void State::PrintStringList(ofstream *outData, char alpha[])
{
StringElem *temp1 = m_StringList;
char null[6] = "NULL\0";
if(temp1 != NULL)
{
//print out strings
while (temp1 !=m_listTail)
{
*outData << temp1->m_string << endl;
temp1 = temp1->m_nextPtr;
}
*outData << temp1->m_string << endl;
*outData << "distribution: ";
//print out distributions and transitions
for(int i = 0; i < m_distributionSize;i++)
*outData << "P(" << alpha[i] << ") = " << m_currentDist[i] << "\t";
*outData << endl<< "transitions: ";
for(int k = 0; k < m_distributionSize;k++)
{
if(m_transitions[k] == -1)
*outData <<"T(" << alpha[k] << ") = "<< null << "\t";
else
*outData <<"T(" << alpha[k] << ") = "<< m_transitions[k] << "\t";
}
*outData << endl;
*outData << "P(state): "<< m_frequency;
}
else
*outData << "empty state" << endl;
*outData << endl << endl;
}
///////////////////////////////////////////////////////////////////////////
// Function: State
// Purpose: constructor for State Class
///////////////////////////////////////////////////////////////////////////
State::State(int distSize, int number)
{
m_distributionSize = distSize;
m_number = number;
m_listSize = 0;
m_listTail = NULL;
m_StringList = NULL;
m_occurenceCount = 0;
m_currentDist = new double[distSize];
m_transitions = new int[distSize];
}
///////////////////////////////////////////////////////////////////////////
// Function: StringElem
// Purpose: constructor for StringElem Class
///////////////////////////////////////////////////////////////////////////
StringElem::StringElem(int distSize)
{
m_size = distSize;
m_counts = new int[distSize];
m_string = NULL;
m_nextPtr = NULL;
}
///////////////////////////////////////////////////////////////////////////
// Function: State::RemoveString
// Purpose: deletes an element from the list
// In Params: element to be removed
// Out Params: none
// In/Out Params: none
// Pre- Cond: new state has been generated by an extension of string to
// be deleted, and all other extensions have been tested
// Post-Cond: element/string has been deleted from list/state
///////////////////////////////////////////////////////////////////////////
void State::RemoveString(StringElem *element)
{
StringElem *temp;
StringElem *temp2;
if(element)
{
if(element == m_StringList)
{
temp = m_StringList;
if(m_listTail == m_StringList)
m_listTail = m_StringList = NULL;
else
m_StringList = m_StringList->m_nextPtr;
delete temp;
}
else
{
for(temp = m_StringList;
temp->m_nextPtr!= NULL && temp->m_nextPtr != element;
temp = temp->m_nextPtr);
if(temp->m_nextPtr == NULL)
{
cerr << "trying to remove a string which does not exist in"
<< " state";
exit(1);
}
if(temp->m_nextPtr == m_listTail)
{
m_listTail = temp;
delete temp->m_nextPtr;
temp->m_nextPtr = NULL;
}
else
{
temp2 = temp->m_nextPtr->m_nextPtr;
delete temp->m_nextPtr;
temp->m_nextPtr = temp2;
}
}
m_listSize--;
}
}
///////////////////////////////////////////////////////////////////////////
// Function: StringElem
// Purpose: copy constructor for StringElem struct
///////////////////////////////////////////////////////////////////////////
StringElem::StringElem(const StringElem &oldElem)
{
m_nextPtr = NULL;
m_size = oldElem.m_size;
//allocate space for string and count info
m_string = new char[strlen(oldElem.m_string) + 1];
strcpy(m_string,oldElem.m_string);
m_counts = new int[m_size];
for(int i = 0; i < m_size; i++)
m_counts[i] = oldElem.m_counts[i];
return;
}