-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchEngine.cs
More file actions
189 lines (164 loc) · 3.94 KB
/
SearchEngine.cs
File metadata and controls
189 lines (164 loc) · 3.94 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
using System.Collections;
using System.Diagnostics;
namespace SimpleSearchEngine;
public class SearchEngine<T>
{
private Node? _root;
public SearchEngine()
{
}
public SearchEngine(IEnumerable<T> elementsToIndex, Func<T, string> indexer)
{
Index(elementsToIndex, indexer);
}
public SearchResult Search(string text)
{
var stopwatch = Stopwatch.StartNew();
var node = FindNode(text.ToLowerInvariant());
stopwatch.Stop();
return new SearchResult(node.Elements, stopwatch.Elapsed);
}
public void Index(IEnumerable<T> elements, Func<T, string> indexer)
{
_root = null;
var tree = new SearchEngine<T>();
var entries = new Dictionary<string, HashSet<T>>();
foreach (var element in elements)
{
var index = indexer(element);
if (string.IsNullOrWhiteSpace(index))
continue;
var indexEntries = new List<string>();
indexEntries.Add(index);
indexEntries.AddRange(index.Split(new[] { ' ', '\n', '\r', '\t', '-' }, StringSplitOptions.RemoveEmptyEntries));
foreach (var indexEntry in indexEntries)
{
var key = indexEntry.ToLowerInvariant();
if (entries.ContainsKey(key))
entries[key].Add(element);
else
entries.Add(key, new HashSet<T>() { element });
}
}
var indexKeys = entries.Select(x => x.Key).ToArray();
tree.BuildBalancedTree(indexKeys, 0, indexKeys.Length);
foreach (var entry in entries)
tree.AddToLists(entry.Key, entry.Value);
_root = tree._root;
}
private void BuildBalancedTree(string[] suggestions, int left, int right)
{
if (left < right)
{
int mid = (left + right) / 2;
_root = Insert(suggestions[mid], 0, _root);
BuildBalancedTree(suggestions, left, mid);
BuildBalancedTree(suggestions, mid + 1, right);
}
}
private Node Insert(string suggestion, int i, Node? ls)
{
if (ls == null)
return new Node(suggestion, suggestion.Length, null);
if (suggestion[i] < ls.First[i])
ls.Left = Insert(suggestion, i, ls.Left);
else if (suggestion[i] > ls.First[i])
ls.Right = Insert(suggestion, i, ls.Right);
else
{
while (++i < ls.CharEnd)
{
if (i == suggestion.Length || suggestion[i] != ls.First[i])
{
ls.Mid = new Node(ls.First, ls.CharEnd, ls.Mid);
ls.CharEnd = i;
break;
}
}
if (i < suggestion.Length)
ls.Mid = Insert(suggestion, i, ls.Mid);
}
return ls;
}
private void AddToLists(string suggestion, IEnumerable<T> elements)
{
var i = 0;
var ls = _root;
while (true)
{
if (suggestion[i] < ls.First[i])
ls = ls.Left;
else if (suggestion[i] > ls.First[i])
ls = ls.Right;
else
{
foreach (var el in elements)
ls.Elements.Add(el);
i = ls.CharEnd;
if (i == suggestion.Length)
return;
ls = ls.Mid;
}
}
}
private Node? FindNode(string prefix)
{
int i = 0;
var ls = _root;
while (ls != null)
{
if (prefix[i] < ls.First[i])
ls = ls.Left;
else if (prefix[i] > ls.First[i])
ls = ls.Right;
else
{
while (++i < ls.CharEnd)
{
if (i == prefix.Length)
return ls;
if (prefix[i] != ls.First[i])
return null;
}
if (i == prefix.Length)
return ls;
ls = ls.Mid;
}
}
return null;
}
public class SearchResult : IEnumerable<T>
{
private readonly IEnumerable<T> _results;
internal SearchResult(IEnumerable<T> results, TimeSpan elapsed)
{
_results = results;
Elapsed = elapsed;
}
public TimeSpan Elapsed { get; private set; }
public IEnumerator<T> GetEnumerator()
{
return _results.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
private class Node
{
public string First { get; private set; }
public HashSet<T> Elements { get; private set; }
public int CharEnd { get; set; }
public Node? Left { get; set; }
public Node? Mid { get; set; }
public Node? Right { get; set; }
public Node(string first, int charEnd, Node? mid)
{
First = first;
Elements = new HashSet<T>();
CharEnd = charEnd;
Mid = mid;
}
}
}