-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.cs
383 lines (364 loc) · 15 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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace SpaceFix
{
/// <summary>
/// Prefix tree that holds values of type.
/// Needs an alphabet provided by user.
/// Thus can be ordered using some real language.
/// </summary>
/// <typeparam name="T">
/// The type of values stored in the trie.
/// </typeparam>
class Trie<T>
{
class Node
{
//Constructors
public Node(char name, int depth, T value)
{
this.name = name;
Depth = depth;
Value = value;
childern = new Node[alphabet.Count];
}
public Node(char name, int depth) :
this(name, depth, EmptyValue)
{ }
//Fields
Node[] childern;
readonly char name;
//Properties
public int Depth { get; }
public T Value { get; set; }
public bool Valuable => !Value.Equals(EmptyValue);
public Node this[char c]
{
get
{
return
alphabet.ContainsKey(c) ?
childern[alphabet[c]] : null;
}
private set
{
if (alphabet.ContainsKey(c))
childern[alphabet[c]] = value;
}
}
//Operators
public static implicit operator char(Node n) => n.name;
//Methods
public Node GetChildStrict(char c) =>
this[c] ?? (this[c] = new Node(c, Depth + 1));
public override string ToString() =>
name == '\0' ? "root" : name.ToString();
public bool HasChild(char c) => this[c] != null;
}
//Constructors
public Trie(char[] abc, string name = null, T emptyValue = default(T))
{
//abcLength = abc.Length;
alphabet = new Dictionary<char, int>(abc.Length);
EmptyValue = emptyValue;
Name = name;
for (int i = 0; i < abc.Length; i++)
alphabet[abc[i]] = i;
root = new Node('\0', 0);
}
//Fields
readonly Node root;
static Dictionary<char, int> alphabet;
string varFinalizer = " ";
ArgumentException canNotSeparate = new ArgumentException(
"The argument can not be separated as a set of keys.",
"keys");
//Properties
public string[] Keys
{
get
{
string word = string.Empty;
List<string> keys = new List<string>();
GetAllKeys(root, ref word, ref keys);
return keys.ToArray();
}
}
public T[] Values
{
get
{
List<T> values = new List<T>();
GetAllValues(root, ref values);
return values.ToArray();
}
}
/// <summary>
/// Name of property stored in values.
/// </summary>
public string Name { get; }
public static T EmptyValue { get; private set; }
//Methods
/// <summary>
/// Adds the key, if absent, and the value to the key.
/// If the key already has a value, rewrite it.
/// </summary>
/// <param name="key">The key to hold the value.</param>
/// <param name="value">The value to add.</param>
public void Add(string key, T value)
{
Node current = root;
foreach (char c in key)
current = current.GetChildStrict(c);
current.Value = value;
}
/// <summary>
/// If the key has no value or is absent, adds the value
/// (or the key and the value) and returns true.
/// If key already holds a value returns false.
/// </summary>
/// <param name="key">The key to check and to hold the value.</param>
/// <param name="value">The value to add.</param>
/// <returns>True, if operation was successful, otherwise false.</returns>
public bool TryAdd(string key, T value)
{
if (Valuable(key) ?? false) return false;
Add(key, value);
return true;
}
/// <summary>
/// The most simple way to delete the valuable key.
/// </summary>
/// <param name="key">The key to delete</param>
public void Delete(string key)
{
Node current = GetNode(key);
if (current?.Valuable ?? false) current.Value = EmptyValue;
}
public bool? Valuable(string key) => GetNode(key)?.Valuable;
public T GetValue(string key)
{
try
{ return GetNode(key).Value; }
catch (NullReferenceException e)
{
throw new KeyNotFoundException(
"There is no such a key in the dictionary", e);
}
}
Node GetNode(string key)
{
Node current = root;
foreach (char c in key)
if (current == null) return null;
else current = current[c];
return current;
}
void GetAllKeys(Node node, ref string word, ref List<string> keys)
{
foreach (char letter in alphabet.Keys)
{
if (node[letter] == null) continue;
word += node[letter].ToString();
if (node[letter].Valuable) keys.Add(word);
GetAllKeys(node[letter], ref word, ref keys);
}
if (word != string.Empty) word = word.Remove(word.Length - 1);
}
void GetAllValues(Node node, ref List<T> values)
{
foreach (char letter in alphabet.Keys)
{
if (node[letter] == null) continue;
if (node[letter].Valuable)
values.Add(node[letter].Value);
GetAllValues(node[letter], ref values);
}
}
public string[][] SeparateKeys(string keys)
{
return SeparateKeys<object>(keys);
}
public string[][] SeparateKeys<TComp>(string keys, PreprocessComparer<List<string>, TComp> pc = null)
{
if (keys == string.Empty) throw new ArgumentException(
"The concatenated keys string must be not empty.", "keys");
//Initializing locals
List<List<string>> keysVariants =
new List<List<string>>()
{ new List<string>() { string.Empty } };
Node
current = root;
int
keyNum = 0,
lastI = keys.Length - 1,
varNum = 0;
bool isError = false;
Node[]
checkPoints = new Node[keys.Length];
//Separating keys
for (int i = 0; i < keys.Length; i++)
{
//Going through the entered string
current = current[char.ToLower(keys[i])];
if (current == null)
isError = true;
else
{
keysVariants[varNum][keyNum] += keys[i];
if (current.Valuable)
{
if (i == lastI)
keysVariants[varNum].Add(varFinalizer);
else
foreach (List<string> trueVariant in keysVariants)
if (trueVariant != keysVariants[varNum] &&
SteppedIntoPattern(trueVariant, keysVariants[varNum]))
{
int subvarLength = CharsInVariant(keysVariants[varNum]);
foreach (string key in trueVariant)
if ((subvarLength -= key.Length) < 0)
keysVariants[varNum].Add(key);
keysVariants[varNum].Add(varFinalizer);
i = lastI;
break;
}
else
if (
checkPoints[i] == null &&
root.HasChild(char.ToLower(keys[i + 1])))
checkPoints[i] = current;
}
}
//Going back to a checkpoint
if (isError || i == lastI)
for (int j = i - 1; j >= 0; j--)
if (checkPoints[j] != null)
{
//Copy true keys
keysVariants.Add(new List<string>());
keyNum = -1;
for (int writtenChars = 0;
writtenChars != j + 1;)
{
if (writtenChars +
keysVariants[varNum][++keyNum].Length >
j + 1)
{
keysVariants[varNum + 1].Add(string.Empty);
for (int k = 0; writtenChars != j + 1; k++, writtenChars++)
keysVariants[varNum + 1][keyNum] +=
keysVariants[varNum][keyNum][k];
}
else
{
keysVariants[varNum + 1].Add(
keysVariants[varNum][keyNum]);
writtenChars +=
keysVariants[varNum][keyNum].Length;
}
//Not exactly an overflow, but looks like
//the most appropriate exception in the situation.
//Added to prevent an infinite cycle that is not supposed
//to emerge if there is no bugs in the method.
if (writtenChars > j + 1) throw new OverflowException(
"The number of written characters is bigger " +
"then it ment to be.");
}
if (isError ||
i == lastI &&
keysVariants[varNum].Last() != varFinalizer)
//Delete spoiled variant
{
keysVariants.RemoveAt(varNum);
isError = false;
}
else if (i == lastI)
{
keysVariants[varNum].Remove(varFinalizer);
int compResult =
(varNum == 0) ? 0 :
pc?.Compare(
keysVariants[varNum],
keysVariants[varNum - 1]) ?? 0;
switch (compResult)
{
case 1:
//Relpace old variants with a better one
keysVariants =
new List<List<string>>()
{
keysVariants[varNum],
keysVariants[varNum + 1]
};
varNum = 1;
break;
case 0:
//Increase the number of variants
varNum++;
break;
case -1:
//Remove a variant that is not good enough.
keysVariants.RemoveAt(varNum);
break;
}
}
//Go back to the checkpoint
current = root;
keysVariants[varNum].Add(string.Empty);
keyNum++;
i = j;
//Delete the checkpoint
checkPoints[j] = null;
break;
}
if (isError || i == lastI) break;
}
if (keysVariants[varNum].Last() != varFinalizer)
keysVariants.Remove(keysVariants[varNum--]);
else keysVariants[varNum].Remove(varFinalizer);
if (pc != null)
{
//Sorting
pc.IfUseAlternative = true;
keysVariants.Sort(pc);
keysVariants.Reverse();
pc.IfUseAlternative = false;
//Repeated removal of the worst variants
for (int i = 1; i < varNum + 1; i++)
if (pc.Compare(keysVariants[i], keysVariants[0]) < 0)
{
keysVariants.RemoveAt(i--);
varNum--;
}
}
//Returning
string[][] result = new string[varNum + 1][];
for (int i = 0; i < varNum + 1; i++)
result[i] = keysVariants[i].ToArray();
return result;
}
static bool SteppedIntoPattern(List<string> trueVariant, List<string> subvariant)
{
int subvarLength = CharsInVariant(subvariant);
for (int i = 0; subvarLength > 0; i++)
{
subvarLength -= trueVariant[i].Length;
if (subvarLength == 0 &&
trueVariant[i] == subvariant.Last())
return true;
}
return false;
}
static int CharsInVariant(List<string> variant)
{
int result = 0;
foreach (string key in variant)
result += key.Length;
return result;
}
}
}