-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie.cs
254 lines (211 loc) · 7.17 KB
/
Trie.cs
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
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
namespace Whammy
{
public class Trie
{
[StructLayout(LayoutKind.Explicit)]
protected unsafe struct TrieNode
{
public const int SIZE_BYTES = 2 + 1 + 26 * 4; // 107
public TrieNode(char c)
{
Character = c;
WordMarker = 0;
}
[FieldOffset(0)]
private char Character;
[FieldOffset(2)]
private byte WordMarker;
[FieldOffset(3)]
private fixed int NextChars[26];
public int GetNextChar(char c)
{
int charPos = Trie.CharToAlphabetPos(c);
if (charPos < 0 || charPos >= 26)
{
throw new ArgumentOutOfRangeException(nameof(c), "Character out of range. Only ascii characters are allowed.");
}
return NextChars[charPos];
}
public void SetNextChar(char c, int index)
{
int charPos = Trie.CharToAlphabetPos(c);
if (charPos < 0 || charPos >= 26)
{
throw new ArgumentOutOfRangeException(nameof(c), "Character out of range. Only ascii characters are allowed.");
}
NextChars[charPos] = index;
}
public void SetWordMarker()
{
WordMarker = 1;
}
public bool HasChild(char c)
{
int charPos = Trie.CharToAlphabetPos(c);
if (charPos < 0 || charPos >= 26)
{
throw new ArgumentOutOfRangeException(nameof(c), "Character out of range. Only ascii characters are allowed.");
}
return NextChars[charPos] > 0;
}
public bool IsWord => WordMarker > 0;
public void Write(BinaryWriter writer)
{
writer.Write(Character);
writer.Write(WordMarker);
for (int i = 0; i < 26; i++)
{
writer.Write(NextChars[i]);
}
}
public TrieNode Read(BinaryReader reader)
{
Character = reader.ReadChar();
WordMarker = reader.ReadByte();
for (int i = 0; i < 26; i++)
{
NextChars[i] = reader.ReadInt32();
}
return this;
}
}
public static Trie FromFile(string path)
{
Trie trie = new Trie();
using (FileStream file = File.OpenRead(path))
{
using (GZipStream gzip = new GZipStream(file, CompressionMode.Decompress))
{
using (MemoryStream memStream = new MemoryStream())
{
gzip.CopyTo(memStream);
using (BinaryReader reader = new BinaryReader(memStream))
{
memStream.Position = 0;
int count = reader.ReadInt32();
trie.nodes = new List<TrieNode>();
for (int i = 0; i < count; i++)
{
trie.nodes.Add(new TrieNode());
trie.nodes[i] = trie.nodes[i].Read(reader);
}
}
}
}
}
return trie;
}
public static void ToFile(Trie trie, string path)
{
using (FileStream file = File.OpenWrite(path))
{
using (GZipStream gzip = new GZipStream(file, CompressionLevel.Optimal))
{
var bytes = trie.Serialize();
gzip.Write(bytes, 0, bytes.Length);
}
}
}
public static int CharToAlphabetPos(char c) {
return char.ToUpper(c) - 65;
}
private List<TrieNode> nodes = new List<TrieNode>();
public Trie()
{
nodes.Add(new TrieNode());
}
public int Size => nodes.Count;
public int SizeBytes => nodes.Count * TrieNode.SIZE_BYTES;
public void Insert(string word) {
int position = 0;
int currentIdx = 0;
TrieNode current = nodes[currentIdx];
foreach (char c in word)
{
if (!current.HasChild(c))
{
var newTrieNode = new TrieNode(c);
nodes.Add(newTrieNode);
current.SetNextChar(c, nodes.Count - 1);
nodes[currentIdx] = current;
currentIdx = nodes.Count - 1;
current = nodes[currentIdx];
}
else
{
int nodeIndex = current.GetNextChar(c);
if (nodeIndex == 0)
{
throw new ArithmeticException("This should never happen");
}
currentIdx = nodeIndex;
current = nodes[nodeIndex];
}
position++;
if (position == word.Length)
{
current.SetWordMarker();
nodes[currentIdx] = current;
}
}
}
public void InsertSubstrings(string word)
{
for (int i = 0; i < word.Length; i++)
{
for (int j = i + 1; j <= word.Length; j++)
{
if (j - i > 0)
{
Insert(word.Substring(i, j - i));
}
}
}
}
public bool Contains(string word)
{
if (word.Length == 0)
{
return false;
}
TrieNode current = nodes[0];
for (int i = 0; i < word.Length; i++)
{
char c = word[i];
if (!current.HasChild(c))
{
return false;
}
int nextIndex = current.GetNextChar(c);
if (nextIndex < 1)
{
return false;
}
if (i == word.Length - 1)
{
return nodes[nextIndex].IsWord;
}
current = nodes[nextIndex];
}
return false;
}
private byte[] Serialize()
{
using (MemoryStream memStream = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(memStream))
{
writer.Write(nodes.Count);
foreach (var node in nodes)
{
node.Write(writer);
}
return memStream.ToArray();
}
}
}
}