-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringTrie.cs
425 lines (386 loc) · 8.42 KB
/
StringTrie.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
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
namespace Wraith.Collections.Generic
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Wraith.Collections;
public class StringRangeStrategy : ITrieRangeStrategy<string>
{
#region ITrieRangeStrategy<string> Members
public IRange<string> Create(string container, int start, int length)
{
return new StringRange(container, start, length, false);
}
public IRange<string> Merge(IRange<string> lhs, IRange<string> rhs)
{
if (lhs == null)
{
return rhs;
}
if (rhs == null)
{
return lhs;
}
if (lhs.Length == 0)
{
return rhs;
}
if (rhs.Length == 0)
{
return lhs;
}
IRange<string> merged = null;
if (
merged == null &&
lhs.Container.Length >=
(
lhs.Start +
lhs.Length +
rhs.Length
)
)
{
// the lhs range.Container may include rhs range
if (
Match(
lhs.Container,
lhs.Start + lhs.Length,
rhs.Length,
rhs.Container,
rhs.Start,
rhs.Length
)
)
{
merged = Create(
lhs.Container,
lhs.Start,
(lhs.Length + rhs.Length)
);
}
}
if (
merged == null &&
rhs.Start >= lhs.Length
)
{
// the rhs range.Container may include lhs range
if (
Match(
rhs.Container,
rhs.Start - lhs.Length,
lhs.Length,
lhs.Container,
lhs.Start,
lhs.Length
)
)
{
merged = Create(
rhs.Container,
rhs.Start - lhs.Length,
(lhs.Length + rhs.Length)
);
}
}
if (merged == null)
{
// the lhs range.Container is not followed by the rhs range
// and the rhs range.Container is not preceeded by the lhs
// range
// create a new Container which contains only the required portions
// from both lhs and rhs that are needed. this is bad and
// will cause fragmentation and increased memory usage.
string newItem = string.Concat(
lhs.Container.Substring(lhs.Start, lhs.Length),
rhs.Container.Substring(rhs.Start, rhs.Length)
);
merged = Create(newItem, 0, newItem.Length);
}
return merged;
}
public void Split(IRange<string> range, int length, out IRange<string> lhs, out IRange<string> rhs)
{
if (range == null)
{
throw new ArgumentException("range is null", "range");
}
if (range.Length == 0)
{
throw new ArgumentException("range is empty", "range");
}
if (length <= 0)
{
throw new ArgumentOutOfRangeException("length", length, "length must be greater than zero");
}
if (length > range.Length)
{
throw new ArgumentOutOfRangeException("length", length, "length must be less than node.Range.Length");
}
lhs = Create(
range.Container,
range.Start,
length
);
rhs = Create(
range.Container,
range.Start + length,
range.Length - length
);
}
public bool Match(string lhs, int lhsStart, int lhsLength, string rhs, int rhsStart, int rhsLength)
{
bool retval = false;
int maxMatchLength = Math.Max(lhsLength, rhsLength);
if (maxMatchLength > 0)
{
retval = (
string.Compare(
lhs, lhsStart,
rhs, rhsStart,
maxMatchLength,
false
)
== 0
);
}
return retval;
}
public int Shared(IRange<string> lhs, int lhsOffset, IRange<string> rhs, int rhsOffset)
{
int retval = 0;
int lhsLength = -1;
int rhsLength = -1;
int lhsStart = -1;
int rhsStart = -1;
if (
lhs != null &&
rhs != null &&
(lhsLength = (lhs.Length - lhsOffset)) > 0 &&
(rhsLength = (rhs.Length - rhsOffset)) > 0
)
{
rhsStart = rhs.Start + rhsOffset;
lhsStart = lhs.Start + lhsOffset;
int maxMatchLength = Math.Min(lhsLength, rhsLength);
if (maxMatchLength > 0)
{
for (
;
retval < maxMatchLength;
retval++
)
{
if (
string.Compare(
lhs.Container,
(lhsStart + retval),
rhs.Container,
(rhsStart + retval),
1,
false
) != 0
)
{
break;
}
}
}
}
return retval;
}
public int Shared(string lhs, int lhsStart, int lhsLength, string rhs, int rhsStart, int rhsLength)
{
int retval = 0;
if (
lhs != null &&
rhs != null &&
lhsLength > 0 &&
rhsLength > 0
)
{
int maxMatchLength = Math.Min(lhsLength, rhsLength);
if (maxMatchLength > 0)
{
for (; retval < maxMatchLength; retval++)
{
if (
string.Compare(
lhs,
(lhsStart + retval),
rhs,
(rhsStart + retval),
1,
false
) != 0
)
{
break;
}
}
}
}
return retval;
}
#endregion
}
public class StringTrie : Trie<string>, IEnumerable<string>
{
[DebuggerStepThrough]
public StringTrie()
: base(
new StringRangeStrategy(),
//new ArrayTrieNodeStrategy<string>(TrieOptions.None)
//new LinkedListTrieNodeStrategy<string>(TrieOptions.None)
new LinkTrieNodeStrategy<string>(TrieOptions.None)
)
{
}
public void Insert(string value)
{
if (value != null && value.Length > 0)
{
//this.Insert(this.CreateRange(container,0,container.Length));
this.Insert(this.RangeStategy.Create(value, 0, value.Length));
}
}
public void Delete(string value)
{
if (value == null)
{
throw new ArgumentNullException("value", "value is null");
}
this.Delete(value, 0, value.Length);
}
#region IEnumerable<string> Members
[DebuggerStepThrough]
public IEnumerator<string> GetEnumerator()
{
return new StringTrieEnumerator(this);
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
public class StringArrayTrie :
//ArrayTrie<string>,IEnumerable<string>
LinkTrie<string>, IEnumerable<string>
{
public StringArrayTrie()
: base(
new StringRangeStrategy(),
TrieOptions.None
)
{
}
public void Insert(string value)
{
if (value != null && value.Length > 0)
{
//this.Insert(this.CreateRange(container,0,container.Length));
this.Insert(this.RangeStategy.Create(value, 0, value.Length));
}
}
public void Delete(string value)
{
if (value == null)
{
throw new ArgumentNullException("value", "value is null");
}
this.Delete(value, 0, value.Length);
}
#region IEnumerable<string> Members
[DebuggerStepThrough]
public IEnumerator<string> GetEnumerator()
{
return new StringTrieEnumerator(this);
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
public class StringLinkTrie : LinkTrie<string>, IEnumerable<string>
{
public StringLinkTrie()
: base(
new StringRangeStrategy(),
TrieOptions.None
)
{
}
public void Insert(string value)
{
if (value != null && value.Length > 0)
{
//this.Insert(this.CreateRange(container,0,container.Length));
this.Insert(this.RangeStategy.Create(value, 0, value.Length));
}
}
public void Delete(string value)
{
if (value == null)
{
throw new ArgumentNullException("value", "value is null");
}
this.Delete(value, 0, value.Length);
}
#region IEnumerable<string> Members
[DebuggerStepThrough]
public IEnumerator<string> GetEnumerator()
{
return new StringTrieEnumerator(this);
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
public class StringTrieEnumerator : ATrieEnumerator<string>
{
private StringBuilder buffer;
public StringTrieEnumerator(Trie<string> trie)
: base(trie)
{
this.buffer = new StringBuilder(256);
}
protected override string ValueFromPath(List<ATrieEnumerator<string>.TrieEnumeratorNode> path)
{
//StringBuilder buffer = new StringBuilder(64);
IRange<string> range = null;
for (int index = 0; index < path.Count; index++)
{
range = path[index].Node.Range;
if (range != null)
{
this.buffer.Append(range.Container, range.Start, range.Length);
}
}
range = this.CurrentNode.Range;
if (range != null)
{
this.buffer.Append(range.Container, range.Start, range.Length);
}
string retval = this.buffer.ToString();
this.buffer.Remove(0, this.buffer.Length);
return retval;
}
public override void Dispose()
{
this.buffer = null;
base.Dispose();
}
}
}