-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAllStates.cpp
1656 lines (1455 loc) · 52.7 KB
/
AllStates.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////
//Title: AllStates.cpp
//Author: Kristina Klinkner
//Date: July 23, 2003
//Description: Class which contains and controls array of states
// for program '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 "AllStates.h"
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::PrintOut
// Purpose: prints out all states in array to a file
// In Params: the name of the input file used for program, the alphabet
// Out Params: none
// In/Out Params: none
// Pre- Cond: all states have been created up to maximum length of strings
// Post-Cond: output file exists with states listed inside
//////////////////////////////////////////////////////////////////////////
void AllStates::PrintOut(char input[], char alpha[])
{
bool success = true;
char* output = new char[strlen(input) + 8 + END_STRING];
strcpy(output,input);
strcat(output, "_results");
//create file streams
ofstream outData(output, ios::out);
//open output file, if unsuccessful set boolean return value
if(!outData)
{
cerr << " the results output file cannot be opened " << endl;
success = false;
}
//otherwise output data
else
{
for(int i = 0; i < m_arraySize; i++)
{
outData << "State number: "<<m_StateArray[i]->getNumber() <<endl;
m_StateArray[i]->PrintStringList(&outData, alpha);
}
}
outData.close();
delete[] output;
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::Grow
// Purpose: enlarges array of states by 'INCREMENT'
// In Params: size of new array
// Out Params: new array
// In/Out Params: old array
// Pre- Cond: Array of states is full, m_arraySize = m_maxArraySize -1
// Post-Cond: Array is now able to insert new states
//////////////////////////////////////////////////////////////////////////
void AllStates::Grow(int newsize)
{
//check if index is valid
State** newBuffer = NULL;
int i;
newBuffer = new State*[newsize];
if (newBuffer == NULL)
{
cerr << "Out of memory." << endl;
exit(1);
}
//set each pointer to NULL
for(i=0;i<newsize;i++)
newBuffer[i] = NULL;
//copy buffer contents into newBuffer
for (i=0;i <= m_arraySize;i++)
{
newBuffer[i] = m_StateArray[i];
}
delete [] m_StateArray;
m_StateArray = NULL;
//point the array buffer at the newly created buffer
m_StateArray = newBuffer;
m_maxArraySize = newsize;
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::Insert
// Purpose: adds a new array element to the specified state
// In Params: string for new state, state in which to insert the string
// Out Params: new state
// In/Out Params: hash table of pointers to strings and states
// Pre- Cond: Array of states has been initialized, new distribution has
// been checked for uniqueness
// Post-Cond: string has been added to new state, new state added to array
// array size increased by one
///////////////////////////////////////////////////////////////////////////
void AllStates::Insert(ArrayElem* elem, State* state)
{
state->Insert(elem, m_table);
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::Insert
// Purpose: adds a new state to the array of states, or attaches a string
// to existing state
// In Params: array element for new state, index of insertion for elem
// Out Params: new state
// In/Out Params: hash table of pointers to strings and states
// Pre- Cond: Array of states has been initialized, new distribution has
// been checked for uniqueness
// Post-Cond: string has been added to new state, new state added to array
// array size increased by one
///////////////////////////////////////////////////////////////////////////
void AllStates::Insert(ArrayElem* elem, int index)
{
//to add a new state
if(index == m_arraySize)
{
if(Is_Full())
{
Grow(m_maxArraySize + INCREMENT );
}
State* temp = new State(m_distSize, m_arraySize);
m_StateArray[m_arraySize] = temp;
m_StateArray[m_arraySize]->Insert(elem, m_table);
m_arraySize++;
}
//otherwise add a string to an existing state
else
{
m_StateArray[index]->Insert(elem, m_table);
}
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::Insert
// Purpose: adds a new state to the array of states, or attaches a string
// to existing state
// In Params: string element for new state, index of insertion for elem
// Out Params: new state
// In/Out Params: hash table of pointers to strings and states
// Pre- Cond: Array of states has been initialized, new distribution has
// been checked for uniqueness
// Post-Cond: string has been added to new state, new state added to array
// array size increased by one
///////////////////////////////////////////////////////////////////////////
void AllStates::Insert(StringElem* elem, int index)
{
//to add a new state
if(index == m_arraySize)
{
if(Is_Full())
{
Grow(m_maxArraySize + INCREMENT );
}
State* temp = new State(m_distSize, m_arraySize);
m_StateArray[m_arraySize] = temp;
m_StateArray[m_arraySize]->Insert(elem, m_table);
m_arraySize++;
}
//otherwise add a string to an existing state
else
{
m_StateArray[index]->Insert(elem, m_table);
}
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::CalcNewDist
// Purpose: calculates the frequency of occurrence of each element given
// new string - P(element|new string)and adds states as need be
// In Params: Parse tree of all strings, current length to examine
// Out Params: any new states created
// In/Out Params: old states which have strings added to them
// Pre- Cond: Parse tree is set, initial states have been created
// Post-Cond: State has array of probabilities of occurence for each
// element in the alphabet given new string
//////////////////////////////////////////////////////////////////////////
void AllStates::CalcNewDist(int length, ParseTree& parsetree)
{
G_Array g_array;
double* newDist = new double[m_distSize];
int stringCount;
bool match;
State* removalState;
char* removalString;
ArrayElem** list;
int listSize;
//get list containing all strings of proper length
parsetree.FindStrings(length, &g_array);
list = g_array.getList();
listSize = g_array.getSize();
//for all of the strings
for(int i =0; i < listSize; i++)
{
match = false;
stringCount = 0;
//calculate new distribution
for(int k =0; k < m_distSize; k++)
{
newDist[k] = (double)((list[i]->getCounts())[k]);
stringCount+=(list[i]->getCounts())[k];
}
//divide by total string occurences
for(int j =0; j < m_distSize; j++)
newDist[j] = newDist[j]/(double)stringCount;
//check whether new distribution matches that of parent state
match = CompareToParent(removalString, removalState, list[i],
newDist, length, match, stringCount);
//check whether new distribution matches that of another existing state
match = RePartition(match, removalString, removalState, list[i], newDist,
stringCount);
//if there were no matches make new state
if(match == false)
{
//insert new string
Insert(list[i], m_arraySize);
//calculate probability distribution
m_StateArray[m_arraySize - 1]->CalcCurrentDist();
//remove ancestor-strings when progeny
//creates new state
RemoveAncestors(removalString, removalState);
}
}
delete[] newDist;
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::RemoveAncestors
// Purpose: removes the histories which have spawned new histories with
// distinct distributions
// In Params: none
// Out Params: none
// In/Out Params: the history which has spawned a new distribution and
// the parent state of that history
// Pre- Cond: Parse tree is set, initial states have been created,
// distribution for new history and for parent state are known
// Post-Cond: ancestors of string are removed from state; if state is left
// empty it is then deleted.
//////////////////////////////////////////////////////////////////////////
void AllStates::RemoveAncestors(char*& removalString, State*& removalState)
{
int removalLength;
if(removalState!= NULL)
{
if(removalString)
removalLength = strlen(removalString);
else
removalLength = 0;
while(removalState!=NULL && removalLength > 0)
{
//remove ancestor-string
m_table->RemoveString(removalString);
//if state is empty remove it
if(!removalState->getStringList())
RemoveState(removalState->getNumber());
else
//re-caculate ancestor-state's distribution
removalState->CalcCurrentDist();
removalString++;
removalState = m_table->WhichState(removalString);
removalLength--;
}
}
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::CompareToParent
// Purpose: checks the distribution of the new history against that of its
// parent state
// In Params: array element containing the appropriate history, distribution of
// that history, count of that history, length of that history
// Out Params: boolean signifying whether the history matched its parent
// state's
// In/Out Params: the history which has spawned a new distribution and
// the parent state of that history
// Pre- Cond: Parse tree is set, initial states have been created,
// distribution for new history and for parent state are known
// Post-Cond: history has been added to parent state if it matched, boolean
// has been set
//////////////////////////////////////////////////////////////////////////
bool AllStates::CompareToParent(char*& removalString, State*& removalState,
ArrayElem* element, double* newDist,
int length, bool match, int stringCount)
{
double sigLevel;
//Compare new distributions to parent State first
removalString = element->getString();
if(length > 1)
(removalString)++;
else
(removalString) = "NULL";
(removalState) = m_table->WhichState(removalString);
if(removalState != NULL)
{
sigLevel = Compare(removalState,newDist,stringCount);
//if not significantly different from parent state
if(sigLevel >= m_sigLevel)
{
match = true;
//add new string to state
Insert(element, removalState);
//re-calculate probability distribution
(removalState)->CalcCurrentDist();
}
}
return match;
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::RePartition
// Purpose: checks the distribution of the new history against that of all
// non-parent states
// In Params: array element containing the appropriate history, distribution of
// that history, count of that history
// Out Params: boolean signifying whether the history matched its parent
// state's
// In/Out Params: the history which has spawned a new distribution and
// the parent state of that history
// Pre- Cond: Parse tree is set, initial states have been created,
// distribution for new history and for non-parent states are known
// Post-Cond: history has been added to non-parent state if it matched, boolean
// has been set
//////////////////////////////////////////////////////////////////////////
bool AllStates::RePartition(bool match, char* removalString,
State* removalState, ArrayElem* element,
double* newDist, int stringCount)
{
double sigLevel;
//compare new distribution to all other states
for(int q =0; q < m_arraySize && match == false;q++)
{
//test distribution against that of current state
sigLevel = Compare(q,newDist,stringCount);
//if not significantly different from state,
//add to that state
if(sigLevel >= m_sigLevel)
{
match = true;
Insert(element, q);
//re-calculate probability distribution
m_StateArray[q]->CalcCurrentDist();
}
}
return match;
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::Determinize
// Purpose: splits states which have strings with differing futures
// for the same symbol
// In Params:parse tree which contains alphabet of symbols and max length
// Out Params: any new states created
// In/Out Params: old states which have been changed
// Pre- Cond: states have been created based on pasts of maximum length
// Post-Cond: States all have consistent transitions for a given symbol
///////////////////////////////////////////////////////////////////////////
void AllStates::Determinize(ParseTree& parsetree)
{
StringElem** stringArray = NULL;
int** stateArray = new int*[m_distSize];
int arraySize;
bool isDeterministic; //variable to check whether states are not created
int maxLength = parsetree.getMaxLength();
bool firstPass = true;
bool isStatesRemoved;
for(int q = 0; q < m_distSize; q++)
stateArray[q] = NULL;
do {
//no states created yet
isDeterministic = true;
//no states deleted due to only long strings yet
isStatesRemoved = false;
//for each state
for(int i = 0; i < m_arraySize; i++)
{
//make 2-D array of state values and 1-D array of
//string values
arraySize = m_StateArray[i]->getListSize();
if(arraySize > 0)
{
//allocate and initialize arrays
AllocateArray(stateArray, arraySize);
stringArray = new StringElem*[arraySize];
for(int k = 0; k < arraySize; k++)
stringArray[k] = NULL;
//fill arrays with transition information
CreateChildStateArray
(parsetree,arraySize,stateArray,stringArray,i);
//check for non-determinism
//and create new states to make deterministic
isDeterministic = MakeNewDetStates(i, stringArray,
stateArray, arraySize,
isDeterministic, parsetree);
//deallocate arrays
for(int j = 0; j < arraySize; j++)
{
delete stringArray[j];
stringArray[j] = NULL;
}
delete[] stringArray;
stringArray = NULL;
DeAllocateArray(stateArray);
}
}
} while (isDeterministic == false || isStatesRemoved == true);
delete[] stateArray;
stateArray = NULL;
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::MakeNewDetStates
// Purpose: creates new states by determinism
// In Params: index of state to determinize, 2-D array of histories in the
// state and the children of those histories, 2-D index of states
// the child histories lead to, the size of the state, the parsetree
// Out Params: boolean signifying whether the state is already deterministic
// In/Out Params: none
// Pre- Cond: States have been set based on distribution and connected
// components
// Post-Cond: State has been passed through at least once for determinization
//////////////////////////////////////////////////////////////////////////
bool AllStates::MakeNewDetStates(int stateIndex, StringElem** stringArray,
int** stateArray, int stringMax,
bool isDeterministic, ParseTree& parsetree)
{
bool isFirstPass = true;
StringElem* temp = NULL;
int childState = 0;
int tempSize = 0;
bool isIncreased = false; //variable to check whether
//more states have been created
bool isNewTransitions = false;
//leave those with first non-NULL state value
//in present state, then
//make all other necessary states
//now that arrays are complete,
//determine common transition states
for(int alphaIndex = 0; alphaIndex < m_distSize; alphaIndex++)
{
isFirstPass = true;
tempSize = m_arraySize;
isIncreased = false;
isNewTransitions = false;
do{
isNewTransitions = false;
isFirstPass = true;
for(int q = 0; (q < stringMax)&& isNewTransitions == false; q++)
{
//for a state which has a transition,
if(stateArray[alphaIndex][q] != NULL_STATE)
{
childState = stateArray[alphaIndex][q];
stateArray[alphaIndex][q] = NULL_STATE;
//after first time through, create new
//states for those strings with differing transitions
if(isFirstPass == false)
{
isIncreased = true;
isDeterministic = false;
temp = new StringElem(*stringArray[q]);
m_table->RemoveString(stringArray[q]->m_string);
Insert(temp, tempSize);
delete temp;
}
FindSimilarTransitions(stringArray, stateArray, childState,
tempSize, alphaIndex, q, stringMax,
isFirstPass);
//if another state has been created,
// recalculate distributions
if(isIncreased == true)
{
//fill arrays
isNewTransitions = true;
stringMax = m_StateArray[stateIndex]->getListSize();
CreateChildStateArray(parsetree,stringMax,stateArray,
stringArray,stateIndex);
m_StateArray[tempSize]->CalcCurrentDist();
m_StateArray[stateIndex]->CalcCurrentDist();
tempSize++;
isIncreased = false;
}
if(isFirstPass)
isFirstPass = false;
}
}
} while (isNewTransitions == true);
}
return isDeterministic;
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::FindSimilarTransitions
// Purpose: finds histories which transition to the same state upon the same
// symbol
// In Params: index of state which initial history has transitioned to, 2-D
// array of histories in the state and the children of those
// histories, 2-D index of states the child histories lead to, the
// current index of the state, the size of the state, the index of
// the symbol being examined, a boolean to show whether or not this
// is the first check of these values
// Out Params: none
// In/Out Params: none
// Pre- Cond: States have been set based on distribution and connected
// components
// Post-Cond: Given hisoty's transition has been matched to all similar
// transitions
//////////////////////////////////////////////////////////////////////////
void AllStates::FindSimilarTransitions(StringElem** stringArray,
int** stateArray, int childState,
int tempSize, int alphaIndex,
int stringIndex, int stringMax,
bool isFirstPass)
{
StringElem* temp = NULL;
for( int k = stringIndex+1; k < stringMax; k++)
{
if(stateArray[alphaIndex][k] == childState)
{
stateArray[alphaIndex][k] = NULL_STATE;
if(!isFirstPass)
{
temp = new StringElem(*stringArray[k]);
m_table->RemoveString(stringArray[k]->m_string);
Insert(temp, tempSize);
delete temp;
}
}
}
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::DestroyLongHists
// Purpose: remove all histories of max length from the states
// In Params: the max length, the parsetree of histories
// Out Params: boolean signifying whether a state has been removed
// In/Out Params: none
// Pre- Cond: States are set
// Post-Cond: no histories of max length remain
///////////////////////////////////////////////////////////////////////////
bool AllStates::DestroyLongHists(int maxLength, ParseTree& parsetree)
{
StringElem* longHist;
StringElem* deadHist;
bool isStatesRemoved = false;
//Check whether the shortest shistory in the state is
//one less than the max, and set transitions here if so
for(int i = 0; i < m_arraySize; i++)
{
longHist = m_StateArray[i]->getStringList();
if(strlen(longHist->m_string) == maxLength - 1)
FindNSetTransitions(i, maxLength, parsetree.getAlpha());
}
//for all states in array of states
for(int j = 0; j < m_arraySize; j++)
{
longHist = m_StateArray[j]->getStringList();
while((longHist) && (strlen(longHist->m_string) < maxLength))
{
longHist = longHist->m_nextPtr;
}
while(longHist)
{
deadHist = longHist;
longHist = longHist->m_nextPtr;
m_table->RemoveString(deadHist->m_string);
}
}
//Remove any states which are now empty
//due to max length history removal
for(int k = 0; k < m_arraySize; k++)
{
if(!m_StateArray[k]->getStringList())
{
RemoveState(k);
isStatesRemoved = true;
}
}
return isStatesRemoved;
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::DestroyShortHists
// Purpose: remove all histories of less than max length - 1 from the states
// In Params: the max length, the parsetree of histories
// Out Params: boolean signifying whether a state has been removed
// In/Out Params: none
// Pre- Cond: States are set
// Post-Cond: no histories of less than max length - 1 remain
///////////////////////////////////////////////////////////////////////////
bool AllStates::DestroyShortHists(int maxLength, ParseTree& parsetree)
{
StringElem* shortHist;
StringElem* deadHist;
bool isStatesRemoved = false;
//for all states in array of states
for(int j = 0; j < m_arraySize; j++)
{
//check for 'NULL' string
shortHist = m_StateArray[j]->getStringList();
if(strcmp(shortHist->m_string, "NULL") == 0)
{
deadHist = shortHist;
shortHist = shortHist->m_nextPtr;
m_table->RemoveString(deadHist->m_string);
}
//check for histories which are too short
while((shortHist) && (strlen(shortHist->m_string) < maxLength - 1))
{
deadHist = shortHist;
shortHist = shortHist->m_nextPtr;
m_table->RemoveString(deadHist->m_string);
}
}
//Remove any states which are now empty
//due to history removal
for(int k = 0; k < m_arraySize; k++)
{
if(!m_StateArray[k]->getStringList())
{
RemoveState(k);
isStatesRemoved = true;
}
}
return isStatesRemoved;
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::StoreTransitions
// Purpose: stores symbol and corresponding transition for each state
// In Params:max length of strings, alphabet
// Out Params: none
// In/Out Params: none
// Pre- Cond: memory for state transitions have been allocated
// Post-Cond: state transition values have been set
///////////////////////////////////////////////////////////////////////////
void AllStates::StoreTransitions(int maxLength, char* alpha)
{
//for each state
for(int i = 0; i < m_arraySize; i++)
FindNSetTransitions(i, maxLength, alpha);
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::FindNSetTransitions
// Purpose: sets the transitions of a state per given symbol
// In Params: the state to set, max history length in state, alphabet
// Out Params: none
// In/Out Params: none
// Pre- Cond: States have been set and determinized
// Post-Cond: State has a transition for each symbol
//////////////////////////////////////////////////////////////////////////
void AllStates::FindNSetTransitions(int state, int maxLength, char* alpha)
{
int shortestLength;
StringElem* temp;
int length;
char* childString;
char* symbol = new char[2];
bool isNull;
int childState;
bool isTooLong;
symbol[1] = '\0';
temp = m_StateArray[state]->getStringList();
shortestLength = strlen(temp->m_string) + 2;
for(int k = 0; k < m_distSize; k++)
{
isTooLong = false;
isNull = true;
temp = m_StateArray[state]->getStringList();
while( isNull == true && temp && isTooLong == false)
{
length = strlen(temp->m_string) + 2;
childString = new char[length];
strcpy(childString, temp->m_string);
//if string exceeds max length allowed
if((length == maxLength + 2) &&
(shortestLength < (maxLength + 2)))
{
childState = NULL_STATE;
isTooLong = true;
}
if(length > maxLength + 1)
childString++;
symbol[0] = alpha[k];
//create child string
strcat(childString, symbol);
//determine state of child string
childState = m_table->WhichStateNumber(childString);
if(childState != NULL_STATE)
{
m_StateArray[state]->setTransitions
(k, childState);
isNull = false;
}
if(isNull == true && temp)
temp = temp->m_nextPtr;
if(length > maxLength + 1)
childString--;
delete[] childString;
}
//if no valid transitions were found, set to NULL_STATE
if(isNull == true || isTooLong == true)
{
m_StateArray[state]->setTransitions(k, childState);
}
}
delete[] symbol;
}
/////////////////////////////////////////////////////////////////////////
//Function: AllStates::DeAllocateArray
// Purpose: frees memory used for multidimensional array
// In Params: none
// Out Params: none
// In/Out Params: pointer to multidimensional array
// Pre- Cond: memory is allocated for array
// Post-Cond: memory is free
////////////////////////////////////////////////////////////////////////
void AllStates::DeAllocateArray(int** stateArray)
{
for(int i = 0; i < m_distSize; i++)
{
delete[] stateArray[i];
stateArray[i] = NULL;
}
}
/////////////////////////////////////////////////////////////////////////
//Function: AllStates::AllocateArray
// Purpose: allocates memory used for multidimensional array
// In Params: size of array to allocate
// Out Params: none
// In/Out Params: pointer to multidimensional array
// Pre- Cond: memory is allocated for main pointer (1-D array)
// Post-Cond: memory is allocated for each pointer (2-D array)
////////////////////////////////////////////////////////////////////////
void AllStates::AllocateArray(int** stateArray, int arraySize)
{
for(int i = 0; i < m_distSize; i++)
{
stateArray[i] = NULL;
stateArray[i] = new int[arraySize];
if(stateArray[i] == NULL)
{
cerr << "Out of memory\n";
exit(1);
}
for(int j = 0; j < arraySize;j++)
{
stateArray[i][j] = NULL_STATE;
}
}
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::CheckConnComponents
// Purpose: searches the machine for transient states and removes them
// In Params: the parse tree of history strings
// Out Params: none
// In/Out Params: none
// Pre- Cond: States have been set
// Post-Cond: All states are recurrent
//////////////////////////////////////////////////////////////////////////
void AllStates::CheckConnComponents(ParseTree& parsetree)
{
char* alpha = parsetree.getAlpha();
int stateCounter = 0;
int maxLength = parsetree.getMaxLength();
bool done = false;
int* stateArray = new int[m_arraySize];
TransTable* transTable = NULL;
//initialize array
for(int i=0; i<m_arraySize;i++)
stateArray[i] = NULL_STATE;
//keep doing until number of states stabilizes
while (done == false)
{
transTable = new TransTable(m_arraySize);
done = true;
if(m_arraySize > 1)
{
//for all states
for(int index = 0; index < m_arraySize; index++)
FillRecurrStateArray(alpha, maxLength, index, stateArray,
stateCounter, transTable);
done = RemoveTransientStates(stateArray, done, stateCounter,
transTable, alpha);
}
delete transTable;
}
delete[] stateArray;
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::FillRecurrStateArray
// Purpose: fills an array with the states which are recurrent from
// particular state which is examined
// In Params: alphabet, maximum length of history, index of specific
// state to be examined
// Out Params: none
// In/Out Params: array of recurrent states, number of states which are
// recurrent, table of transitons from max length strings
// Pre- Cond: Memory has been allocated for the array of recurrent states
// Post-Cond: The array of recurrent state indices has been set for the
// specific state (array of 'childstates' of given state)
//////////////////////////////////////////////////////////////////////////
void AllStates::FillRecurrStateArray(char* alpha, int maxLength, int index,
int*& stateArray, int& stateCounter,
TransTable*& transTable)
{
int length;
char* childString = NULL;
int childState;
char* symbol = new char[2];
bool match = false;
StringElem* temp;
//use the list of strings for appropriate state
temp = m_StateArray[index]->getStringList();
while(temp!=NULL)
{
//get string which is in list
length = strlen(temp->m_string);
//look at transitions of child strings
for(int k =0; k < m_distSize; k++)
{
childString = new char[length + 2*SYMB_SIZE + END_STRING];
strcpy(childString, temp->m_string);
//wrap string if it exceeds max length
if(length == maxLength)
childString++;
symbol[0] = alpha[k];
symbol[1] = '\0';
//create child string
strcat(childString, symbol);
//determine state of child string
childState = m_table->WhichStateNumber(childString);
//fill array with states to which there are transitions
if(childState != index && ((length <= maxLength - 1)
|| temp == m_StateArray[index]->getStringList()))
{
//if state is already in array, ignore
for(int q = 0; q < stateCounter;q++)
{
if(stateArray[q] == childState)
match = true;
}
//if null state, ignore
if(childState == NULL_STATE)
match = true;
//if not, add that state to the array
if(match == false)
{
stateArray[stateCounter] = childState;
stateCounter++;
}
match = false;
}
if(length == maxLength)
{
//record transitions from longest strings
transTable->setTrans(childState, temp, index);
childString--;
}
delete[] childString;
childString = NULL;
}
temp = temp->m_nextPtr;
}
delete[] symbol;
}
///////////////////////////////////////////////////////////////////////////
// Function: AllStates::RemoveTransientStates
// Purpose: removes any transient states
// In Params: array of recurrent states, boolean to be set when none are
// deleted, number of transient states
// Out Params: none
// In/Out Params: none
// Pre- Cond: Array of recurrent states has been set
// Post-Cond: Transient states have been deleted (though more may have been
// created)
//////////////////////////////////////////////////////////////////////////
bool AllStates::RemoveTransientStates(int* stateArray, bool done,
int stateCounter, TransTable* transTable,
char* alpha)
{