-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstructs.cpp
154 lines (134 loc) · 2.64 KB
/
structs.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
#include <iostream>
#include <deque>
#include <map>
#include <vector>
enum
{
NHASH = 4098,
};
std::hash<std::string> hasher;
class Suffixes
{
private:
class Suffix
{
public:
std::string val;
Suffix *next;
};
Suffix *suffixes;
int _size;
public:
int size()
{
return _size;
}
std::string operator[](int index)
{
int c = 0;
for (Suffix *s = suffixes; s != NULL; s = s->next)
if (index == c++)
return s->val;
return NULL;
}
void push(std::string val)
{
_size++;
Suffix *s = new Suffix();
s->val = val;
s->next = suffixes;
suffixes = s;
}
};
class Prefix
{
public:
std::string values[2];
int _size;
int size()
{
return _size;
}
std::string at(int index)
{
return values[index];
}
void push_back(std::string val)
{
values[_size++] = val;
}
void pop_front()
{
if (_size == 2)
{
values[0] = values[1];
values[1] = std::string();
}
else if (_size == 1)
{
values[0] = std::string();
}
else if (_size == 0)
{
return;
}
_size--;
}
int hash()
{
unsigned int h = 0;
for (int i = 0; i < size(); i++)
{
h += hasher(at(i));
}
return h % NHASH;
}
bool operator==(Prefix other)
{
if (size() != other.size())
return false;
for (int i = 0; i < size(); i++)
if (at(i).compare(other.at(i)) != 0)
return false;
return true;
}
Prefix()
{
_size = 0;
values[0] = std::string();
values[1] = std::string();
}
};
class StateMap
{
private:
class StateMapEntry
{
public:
Prefix *prefix;
Suffixes *suffixes;
StateMapEntry *next;
};
public:
StateMapEntry *entries[NHASH];
Suffixes *operator[](Prefix p)
{
int hash = p.hash();
for (StateMapEntry *e = entries[hash]; e != NULL; e = e->next)
{
if (p == *e->prefix)
{
return e->suffixes;
}
}
//create
StateMapEntry *new_entry = new StateMapEntry();
new_entry->prefix = new Prefix();
for (int i = 0; i < p.size(); i++)
new_entry->prefix->push_back(p.at(i));
new_entry->suffixes = new Suffixes();
new_entry->next = entries[hash];
entries[hash] = new_entry;
return entries[hash]->suffixes;
}
};