forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTernaryTree.cs
More file actions
817 lines (741 loc) · 27.5 KB
/
Copy pathTernaryTree.cs
File metadata and controls
817 lines (741 loc) · 27.5 KB
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
// Lucene version compatibility level 4.8.1
using Lucene.Net.Support;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Lucene.Net.Analysis.Compound.Hyphenation
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// <h2>Ternary Search Tree.</h2>
///
/// <para>
/// A ternary search tree is a hybrid between a binary tree and a digital search
/// tree (trie). Keys are limited to strings. A data value of type char is stored
/// in each leaf node. It can be used as an index (or pointer) to the data.
/// Branches that only contain one key are compressed to one node by storing a
/// pointer to the trailer substring of the key. This class is intended to serve
/// as base class or helper class to implement Dictionary collections or the
/// like. Ternary trees have some nice properties as the following: the tree can
/// be traversed in sorted order, partial matches (wildcard) can be implemented,
/// retrieval of all keys within a given distance from the target, etc. The
/// storage requirements are higher than a binary tree but a lot less than a
/// trie. Performance is comparable with a hash table, sometimes it outperforms a
/// hash function (most of the time can determine a miss faster than a hash).
/// </para>
///
/// <para>
/// The main purpose of this java port is to serve as a base for implementing
/// TeX's hyphenation algorithm (see The TeXBook, appendix H). Each language
/// requires from 5000 to 15000 hyphenation patterns which will be keys in this
/// tree. The strings patterns are usually small (from 2 to 5 characters), but
/// each char in the tree is stored in a node. Thus memory usage is the main
/// concern. We will sacrifice 'elegance' to keep memory requirements to the
/// minimum. Using java's char type as pointer (yes, I know pointer it is a
/// forbidden word in java) we can keep the size of the node to be just 8 bytes
/// (3 pointers and the data char). This gives room for about 65000 nodes. In my
/// tests the english patterns took 7694 nodes and the german patterns 10055
/// nodes, so I think we are safe.
/// </para>
///
/// <para>
/// All said, this is a map with strings as keys and char as value. Pretty
/// limited!. It can be extended to a general map by using the string
/// representation of an object and using the char value as an index to an array
/// that contains the object values.
/// </para>
///
/// This class has been taken from the Apache FOP project (http://xmlgraphics.apache.org/fop/). They have been slightly modified.
/// </summary>
public class TernaryTree // LUCENENET specific: Not implementing ICloneable per Microsoft's recommendation
{
// We use 4 arrays to represent a node.I guess I should have created a proper
// node class, but somehow Knuth's pascal code made me forget we now have a
// portable language with virtual memory management and automatic garbage
// collection! And now is kind of late, furthermore, if it ain't broken, don't
// fix it.
/// <summary>
/// Pointer to low branch and to rest of the key when it is stored directly in
/// this node, we don't have unions in java!
/// </summary>
protected char[] m_lo;
/// <summary>
/// Pointer to high branch.
/// </summary>
protected char[] m_hi;
/// <summary>
/// Pointer to equal branch and to data when this node is a string terminator.
/// </summary>
protected char[] m_eq;
/// <summary>
/// <para>
/// The character stored in this node: splitchar. Two special values are
/// reserved:
/// </para>
/// <list type="bullet">
/// <item><description>0x0000 as string terminator</description></item>
/// <item><description>0xFFFF to indicate that the branch starting at this node is compressed</description></item>
/// </list>
/// <para>
/// This shouldn't be a problem if we give the usual semantics to strings since
/// 0xFFFF is guaranteed not to be an Unicode character.
/// </para>
/// </summary>
protected char[] m_sc;
/// <summary>
/// This vector holds the trailing of the keys when the branch is compressed.
/// </summary>
protected CharVector m_kv;
protected char m_root;
protected char m_freenode;
protected int m_length; // number of items in tree
protected const int BLOCK_SIZE = 2048; // allocation size for arrays
internal TernaryTree()
{
Init();
}
// LUCENENET specific - S1699 - marked non-virtual because calling
// virtual members from the constructor is not a safe operation in .NET
protected void Init()
{
m_root = (char)0;
m_freenode = (char)1;
m_length = 0;
m_lo = new char[BLOCK_SIZE];
m_hi = new char[BLOCK_SIZE];
m_eq = new char[BLOCK_SIZE];
m_sc = new char[BLOCK_SIZE];
m_kv = new CharVector();
}
/// <summary>
/// Branches are initially compressed, needing one node per key plus the size
/// of the string key. They are decompressed as needed when another key with
/// same prefix is inserted. This saves a lot of space, specially for long
/// keys.
/// </summary>
public virtual void Insert(string key, char val)
{
// make sure we have enough room in the arrays
int len = key.Length + 1; // maximum number of nodes that may be generated
if (m_freenode + len > m_eq.Length)
{
RedimNodeArrays(m_eq.Length + BLOCK_SIZE);
}
char[] strkey = new char[len--];
key.CopyTo(0, strkey, 0, len - 0);
strkey[len] = (char)0;
m_root = Insert(m_root, strkey, 0, val);
}
public virtual void Insert(char[] key, int start, char val)
{
int len = StrLen(key) + 1;
if (m_freenode + len > m_eq.Length)
{
RedimNodeArrays(m_eq.Length + BLOCK_SIZE);
}
m_root = Insert(m_root, key, start, val);
}
/// <summary>
/// The actual insertion function, recursive version.
/// </summary>
private char Insert(char p, char[] key, int start, char val)
{
int len = StrLen(key, start);
if (p == 0)
{
// this means there is no branch, this node will start a new branch.
// Instead of doing that, we store the key somewhere else and create
// only one node with a pointer to the key
p = m_freenode++;
m_eq[p] = val; // holds data
m_length++;
m_hi[p] = (char)0;
if (len > 0)
{
m_sc[p] = (char)0xFFFF; // indicates branch is compressed
m_lo[p] = (char)m_kv.Alloc(len + 1); // use 'lo' to hold pointer to key
StrCpy(m_kv.Array, m_lo[p], key, start);
}
else
{
m_sc[p] = (char)0;
m_lo[p] = (char)0;
}
return p;
}
if (m_sc[p] == 0xFFFF)
{
// branch is compressed: need to decompress
// this will generate garbage in the external key array
// but we can do some garbage collection later
char pp = m_freenode++;
m_lo[pp] = m_lo[p]; // previous pointer to key
m_eq[pp] = m_eq[p]; // previous pointer to data
m_lo[p] = (char)0;
if (len > 0)
{
m_sc[p] = m_kv[m_lo[pp]];
m_eq[p] = pp;
m_lo[pp]++;
if (m_kv[m_lo[pp]] == 0)
{
// key completely decompressed leaving garbage in key array
m_lo[pp] = (char)0;
m_sc[pp] = (char)0;
m_hi[pp] = (char)0;
}
else
{
// we only got first char of key, rest is still there
m_sc[pp] = (char)0xFFFF;
}
}
else
{
// In this case we can save a node by swapping the new node
// with the compressed node
m_sc[pp] = (char)0xFFFF;
m_hi[p] = pp;
m_sc[p] = (char)0;
m_eq[p] = val;
m_length++;
return p;
}
}
char s = key[start];
if (s < m_sc[p])
{
m_lo[p] = Insert(m_lo[p], key, start, val);
}
else if (s == m_sc[p])
{
if (s != 0)
{
m_eq[p] = Insert(m_eq[p], key, start + 1, val);
}
else
{
// key already in tree, overwrite data
m_eq[p] = val;
}
}
else
{
m_hi[p] = Insert(m_hi[p], key, start, val);
}
return p;
}
/// <summary>
/// Compares 2 null terminated char arrays
/// </summary>
public static int StrCmp(char[] a, int startA, char[] b, int startB)
{
for (; a[startA] == b[startB]; startA++, startB++)
{
if (a[startA] == 0)
{
return 0;
}
}
return a[startA] - b[startB];
}
/// <summary>
/// Compares a string with null terminated char array
/// </summary>
public static int StrCmp(string str, char[] a, int start)
{
int i, d, len = str.Length;
for (i = 0; i < len; i++)
{
d = (int)str[i] - a[start + i];
if (d != 0)
{
return d;
}
if (a[start + i] == 0)
{
return d;
}
}
if (a[start + i] != 0)
{
return -a[start + i];
}
return 0;
}
public static void StrCpy(char[] dst, int di, char[] src, int si)
{
while (src[si] != 0)
{
dst[di++] = src[si++];
}
dst[di] = (char)0;
}
public static int StrLen(char[] a, int start)
{
int len = 0;
for (int i = start; i < a.Length && a[i] != 0; i++)
{
len++;
}
return len;
}
public static int StrLen(char[] a)
{
return StrLen(a, 0);
}
public virtual int Find(string key)
{
int len = key.Length;
char[] strkey = new char[len + 1];
key.CopyTo(0, strkey, 0, len - 0);
strkey[len] = (char)0;
return Find(strkey, 0);
}
public virtual int Find(char[] key, int start)
{
int d;
char p = m_root;
int i = start;
char c;
while (p != 0)
{
if (m_sc[p] == 0xFFFF)
{
if (StrCmp(key, i, m_kv.Array, m_lo[p]) == 0)
{
return m_eq[p];
}
else
{
return -1;
}
}
c = key[i];
d = c - m_sc[p];
if (d == 0)
{
if (c == 0)
{
return m_eq[p];
}
i++;
p = m_eq[p];
}
else if (d < 0)
{
p = m_lo[p];
}
else
{
p = m_hi[p];
}
}
return -1;
}
public virtual bool Knows(string key)
{
return (Find(key) >= 0);
}
// redimension the arrays
private void RedimNodeArrays(int newsize)
{
int len = newsize < m_lo.Length ? newsize : m_lo.Length;
char[] na = new char[newsize];
Arrays.Copy(m_lo, 0, na, 0, len);
m_lo = na;
na = new char[newsize];
Arrays.Copy(m_hi, 0, na, 0, len);
m_hi = na;
na = new char[newsize];
Arrays.Copy(m_eq, 0, na, 0, len);
m_eq = na;
na = new char[newsize];
Arrays.Copy(m_sc, 0, na, 0, len);
m_sc = na;
}
public virtual int Length => m_length;
public virtual object Clone()
{
TernaryTree t = new TernaryTree();
t.m_lo = (char[])this.m_lo.Clone();
t.m_hi = (char[])this.m_hi.Clone();
t.m_eq = (char[])this.m_eq.Clone();
t.m_sc = (char[])this.m_sc.Clone();
t.m_kv = (CharVector)this.m_kv.Clone();
t.m_root = this.m_root;
t.m_freenode = this.m_freenode;
t.m_length = this.m_length;
return t;
}
/// <summary>
/// Recursively insert the median first and then the median of the lower and
/// upper halves, and so on in order to get a balanced tree. The array of keys
/// is assumed to be sorted in ascending order.
/// </summary>
protected virtual void InsertBalanced(string[] k, char[] v, int offset, int n)
{
int m;
if (n < 1)
{
return;
}
m = n >> 1;
Insert(k[m + offset], v[m + offset]);
InsertBalanced(k, v, offset, m);
InsertBalanced(k, v, offset + m + 1, n - m - 1);
}
/// <summary>
/// Balance the tree for best search performance
/// </summary>
public virtual void Balance()
{
// System.out.print("Before root splitchar = ");
// System.out.println(sc[root]);
int i = 0, n = m_length;
string[] k = new string[n];
char[] v = new char[n];
using (Enumerator iter = new Enumerator(this))
{
while (iter.MoveNext())
{
v[i] = iter.Value;
k[i++] = iter.Current;
}
}
Init();
InsertBalanced(k, v, 0, n);
// With uniform letter distribution sc[root] should be around 'm'
// System.out.print("After root splitchar = ");
// System.out.println(sc[root]);
}
/// <summary>
/// Each node stores a character (splitchar) which is part of some key(s). In a
/// compressed branch (one that only contain a single string key) the trailer
/// of the key which is not already in nodes is stored externally in the kv
/// array. As items are inserted, key substrings decrease. Some substrings may
/// completely disappear when the whole branch is totally decompressed. The
/// tree is traversed to find the key substrings actually used. In addition,
/// duplicate substrings are removed using a map (implemented with a
/// TernaryTree!).
///
/// </summary>
public virtual void TrimToSize()
{
// first balance the tree for best performance
Balance();
// redimension the node arrays
RedimNodeArrays(m_freenode);
// ok, compact kv array
CharVector kx = new CharVector();
kx.Alloc(1);
TernaryTree map = new TernaryTree();
Compact(kx, map, m_root);
m_kv = kx;
m_kv.TrimToSize();
}
private void Compact(CharVector kx, TernaryTree map, char p)
{
int k;
if (p == 0)
{
return;
}
if (m_sc[p] == 0xFFFF)
{
k = map.Find(m_kv.Array, m_lo[p]);
if (k < 0)
{
k = kx.Alloc(StrLen(m_kv.Array, m_lo[p]) + 1);
StrCpy(kx.Array, k, m_kv.Array, m_lo[p]);
map.Insert(kx.Array, k, (char)k);
}
m_lo[p] = (char)k;
}
else
{
Compact(kx, map, m_lo[p]);
if (m_sc[p] != 0)
{
Compact(kx, map, m_eq[p]);
}
Compact(kx, map, m_hi[p]);
}
}
/// <summary>
/// Gets an enumerator over the keys of this <see cref="TernaryTree"/>.
/// <para/>
/// NOTE: This was keys() in Lucene.
/// </summary>
/// <returns>An enumerator over the keys of this <see cref="TernaryTree"/>.</returns>
public virtual IEnumerator<string> GetEnumerator()
{
return new Enumerator(this);
}
/// <summary>
/// Enumerator for TernaryTree
/// <para/>
/// LUCENENET NOTE: This differs a bit from its Java counterpart to adhere to
/// .NET IEnumerator semantics. In Java, when the <see cref="Enumerator"/> is
/// instantiated, it is already positioned at the first element. However,
/// to act like a .NET IEnumerator, the initial state is undefined and considered
/// to be before the first element until <see cref="MoveNext"/> is called, and
/// if a move took place it will return <c>true</c>;
/// </summary>
public class Enumerator : IEnumerator<string>
{
private readonly TernaryTree outerInstance;
/// <summary>
/// current node index
/// </summary>
private int cur;
/// <summary>
/// current key
/// </summary>
private string curkey;
private class Item // LUCENENET specific: Not implementing ICloneable per Microsoft's recommendation
{
internal readonly char parent; // LUCENENET: marked readonly
internal char child;
// LUCENENET: This constructor is unnecessary
//public Item()
//{
// parent = (char)0;
// child = (char)0;
//}
public Item(char p, char c)
{
parent = p;
child = c;
}
public object Clone()
{
return new Item(parent, child);
}
}
/// <summary>
/// Node stack
/// </summary>
private readonly Stack<Item> ns;
/// <summary>
/// key stack implemented with a <see cref="StringBuilder"/>
/// </summary>
private readonly StringBuilder ks;
private bool isInitialized = false;
public Enumerator(TernaryTree ternaryTree)
{
this.outerInstance = ternaryTree;
cur = -1;
ns = new Stack<Item>();
ks = new StringBuilder();
isInitialized = false;
}
public virtual void Rewind()
{
ns.Clear();
ks.Length = 0;
cur = outerInstance.m_root;
Run();
}
public virtual char Value
{
get
{
if (cur >= 0)
{
return outerInstance.m_eq[cur];
}
return (char)0;
}
}
/// <summary>
/// traverse upwards
/// </summary>
private int Up()
{
Item i/* = new Item()*/; // LUCENENET: Removed unnecessary assignment
int res = 0;
if (ns.Count == 0)
{
return -1;
}
if (cur != 0 && outerInstance.m_sc[cur] == 0)
{
return outerInstance.m_lo[cur];
}
bool climb = true;
while (climb)
{
i = ns.Pop();
i.child++;
switch ((int)i.child)
{
case 1:
if (outerInstance.m_sc[i.parent] != 0)
{
res = outerInstance.m_eq[i.parent];
ns.Push((Item)i.Clone());
ks.Append(outerInstance.m_sc[i.parent]);
}
else
{
i.child++;
ns.Push((Item)i.Clone());
res = outerInstance.m_hi[i.parent];
}
climb = false;
break;
case 2:
res = outerInstance.m_hi[i.parent];
ns.Push((Item)i.Clone());
if (ks.Length > 0)
{
ks.Length = ks.Length - 1; // pop
}
climb = false;
break;
default:
if (ns.Count == 0)
{
return -1;
}
climb = true;
break;
}
}
return res;
}
/// <summary>
/// traverse the tree to find next key
/// </summary>
private int Run()
{
if (cur == -1)
{
return -1;
}
bool leaf = false;
while (true)
{
// first go down on low branch until leaf or compressed branch
while (cur != 0)
{
if (outerInstance.m_sc[cur] == 0xFFFF)
{
leaf = true;
break;
}
ns.Push(new Item((char)cur, '\u0000'));
if (outerInstance.m_sc[cur] == 0)
{
leaf = true;
break;
}
cur = outerInstance.m_lo[cur];
}
if (leaf)
{
break;
}
// nothing found, go up one node and try again
cur = Up();
if (cur == -1)
{
return -1;
}
}
// The current node should be a data node and
// the key should be in the key stack (at least partially)
StringBuilder buf = new StringBuilder(ks.ToString());
if (outerInstance.m_sc[cur] == 0xFFFF)
{
int p = outerInstance.m_lo[cur];
while (outerInstance.m_kv[p] != 0)
{
buf.Append(outerInstance.m_kv[p++]);
}
}
curkey = buf.ToString();
return 0;
}
#region Added for better .NET support
public string Current => curkey;
object IEnumerator.Current => Current;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// LUCENENET specific - implemented proper dispose pattern
protected virtual void Dispose(bool disposing)
{
// nothing to do
}
public bool MoveNext()
{
if (!isInitialized)
{
Rewind();
isInitialized = true;
return cur != -1;
}
if (cur == -1)
{
return false;
}
cur = Up();
Run();
return cur != -1;
}
public void Reset()
{
throw UnsupportedOperationException.Create();
}
#endregion
}
public virtual void PrintStats(TextWriter @out)
{
@out.WriteLine("Number of keys = " + Convert.ToString(m_length)); // LUCENENET: Intentionally using current culture
@out.WriteLine("Node count = " + Convert.ToString(m_freenode)); // LUCENENET: Intentionally using current culture
// System.out.println("Array length = " + Integer.toString(eq.length));
@out.WriteLine("Key Array length = " + Convert.ToString(m_kv.Length)); // LUCENENET: Intentionally using current culture
/*
* for(int i=0; i<kv.length(); i++) if ( kv.get(i) != 0 )
* System.out.print(kv.get(i)); else System.out.println("");
* System.out.println("Keys:"); for(Enumeration enum = keys();
* enum.hasMoreElements(); ) System.out.println(enum.nextElement());
*/
}
/*
public static void main(String[] args) {
TernaryTree tt = new TernaryTree();
tt.insert("Carlos", 'C');
tt.insert("Car", 'r');
tt.insert("palos", 'l');
tt.insert("pa", 'p');
tt.trimToSize();
System.out.println((char) tt.find("Car"));
System.out.println((char) tt.find("Carlos"));
System.out.println((char) tt.find("alto"));
tt.printStats(System.out);
}
*/
}
}