forked from manly/AdaptiveRadixTree
/
Program.cs
132 lines (112 loc) · 4.9 KB
/
Program.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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Collections.Specialized;
namespace AdaptiveRadixTree {
class Program {
public static IEnumerable<T[]> GenerateCombinations<T>(IEnumerable<T[]> source) {
bool found;
T[][] values = source.ToArray();
int[] counts = new int[values.Length];
T[] path = new T[values.Length];
int[] pathIndex = new int[counts.Length];
int max = values.Length - 1;
var count = values.Length;
for(int i = 0; i < count; i++) {
var v = values[i];
path[i] = v[0];
counts[i] = v.Length - 1;
}
yield return path;
do {
found = false;
int dimension = max;
while(dimension >= 0) {
if(pathIndex[dimension] < counts[dimension]) {
var index = pathIndex[dimension] + 1;
pathIndex[dimension] = index;
path[dimension] = values[dimension][index];
while(dimension < max) {
pathIndex[++dimension] = 0;
path[dimension] = values[dimension][0];
}
yield return path;
found = true;
break;
} else
dimension--;
}
} while(found);
}
static void Main(string[] args) {
var dist22 = new SpellingSuggestor(3);
dist22.AddRange("manly,abcde,hamburger,apple".Split(','));
var fufuufuff = dist22.Lookup("applr").ToList();
var art2 = new AdaptiveRadixTree<string, string>();
art2.Add("dfgdfg", "213123");
var dist22333 = FuzzyStringMatch.CombinedWordEditDistance("H7N0K3", "H7N0K3");
var art = new AdaptiveRadixTree<string, int>();
var dict = new Dictionary<string, int>();
int aa = 0;
var items = AdaptiveRadixTreeTest.GenerateTestKeys(100000).ToArray();
foreach(var item in items) {
art.Add(in item, aa);
dict.Add(item, aa);
aa++;
}
System.Diagnostics.Debug.Write(art.CalculateMetrics());
//// System.Diagnostics.Debug.Write(art.DebugDump(true));
//
var now = DateTime.UtcNow;
int readResults = 0;
// todo: read/write not using stream
for(int i=0; i<items.Length; i++){
var item = items[i];
//readResults += art.PartialMatch(item.Substring(0, item.Length - 1) + ".", '.').Count();
//readResults += art.PartialMatch("." + item.Substring(1), '.').Count();
//readResults += art.RegExpMatch("[A-B-D]" + item.Substring(1)).Count();
//readResults += art.StartsWithKeys(item.Substring(0, item.Length - 1)).Count();
//readResults += art.RegExpNearNeighbors("[A-B-D]" + item.Substring(1), 0).Count();
// remove()
// fix path filter enumerator to call the filter method in-order
// try with just one entry
//var sdfsdf = art.RegExpNearNeighbors("BBBI", 2).ToList();
if(i==99999)
"".ToLower();
if(!art.Remove(item)) // fuck at i=163 with offbranch code
"".ToLower();
//if(i > 145 && art.DebugDump(true).Contains("EXCEPTION"))
// "".ToString();
}
//var dict4 = new Dictionary<int, string>(100000);
//int coll = 0;
//for(int i = 0; i < 100000; i++) {
// //FuzzyStringMatch.GetStableHashCode(items[i % 100000]);
// //items[i % 100000].GetHashCode();
// if((i %100000) == 0)
// dict4.Clear();
// try {
// dict4.Add(FuzzyStringMatch.GetStableHashCode(items[i % 100000]), items[i % 100000]);
// } catch {
// coll++;
// }
//}
var diff = DateTime.UtcNow - now;
System.Console.WriteLine(diff.ToString());
//System.Console.WriteLine(readResults);
System.Console.ReadLine();
}
private class stringcom : IEqualityComparer<string> {
public bool Equals(string x, string y) {
return x == y;
}
public int GetHashCode(string obj) {
return FuzzyStringMatch.GetStableHashCode(obj);
}
}
}
}