Trie.cs
3.28 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
using LAViD.Unity.Utils;
using LAViD.Utils;
using System.Collections.Generic;
namespace LAViD.Structures
{
public class Trie
{
private char[] characters;
private Dictionary<char, int> keys;
private Node root;
private readonly static JSONObject<Trie> Parser = new JSONObject<Trie>(delegate () { return new Trie(); })
.Add("keys", delegate (JSONParser jsonParser, Trie obj)
{
PlayerLogger.Log("T", "FJ", "Checking 'keys'.");
obj.keys = jsonParser.ParseDictionary<char, int>(char.Parse, JSONParser.IntParseCallback);
})
.Add("trie", delegate (JSONParser jsonParser, Trie obj)
{
PlayerLogger.Log("T", "FJ", "Checking 'trie'.");
obj.root = Node.FromJSON(jsonParser, obj.keys.Count);
})
.Add("characters", delegate (JSONParser jsonParser, Trie obj)
{
PlayerLogger.Log("T", "FJ", "Checking 'characters'.");
obj.characters = jsonParser.ParseList<char>(char.Parse).ToArray();
});
public static Trie FromJSON(JSONParser json) {
return Trie.Parser.Parse(json);
}
public void Add(string word)
{
Node node = this.root;
foreach (char c in word)
{
int key = this.keys[c];
if (node.children[key] == null)
node.children[key] = new Node(this.root.maxChildren);
node = node.children[key];
}
node.end = true;
}
public bool Contains(string word)
{
Node node = this.root;
if (node == null) return false;
foreach (char c in word)
{
node = node.children[this.keys[c]];
if (node == null) break;
}
if (node == null) return false;
else return node.end;
}
public List<string> StartsWith(string word)
{
PlayerLogger.Log("T", "SW", "Searching for '" + word + "'.");
List<string> list = new List<string>();
Node node = this.root;
if (node == null) return list;
foreach (char c in word)
{
node = node.children[this.keys[c]];
if (node == null) break;
}
if (node != null) feed(list, word, node);
return list;
}
private void feed(List<string> list, string word, Node node)
{
if (node.end) list.Add(word);
for (int i = 0; i < node.children.Length; i++)
if (node.children[i] != null)
feed(list, word + this.characters[i], node.children[i]);
}
private class Node
{
public bool end = false;
public Node[] children = null;
public int maxChildren;
public Node(int maxChildren) {
this.maxChildren = maxChildren;
this.children = new Node[maxChildren];
}
private readonly static JSONObject<Node> Parser = new JSONObject<Node>()
.Add("keys", delegate (JSONParser jsonParser, Node obj)
{
jsonParser.ParseList<int>(JSONParser.IntParseCallback);
})
.Add("end", delegate (JSONParser jsonParser, Node obj)
{
obj.end = jsonParser.ParseBoolean();
})
.Add("children", delegate (JSONParser jsonParser, Node obj)
{
Dictionary<int, Node> children = jsonParser.ParseDictionary<int, Node>(
int.Parse,
delegate (JSONParser innerJSONParser) {
return Node.FromJSON(innerJSONParser, obj.maxChildren);
}
);
foreach (KeyValuePair<int, Node> pair in children)
obj.children[pair.Key] = pair.Value;
});
public static Node FromJSON(JSONParser json, int maxChildren) {
return Node.Parser.Parse(json, delegate () { return new Node(maxChildren); });
}
}
}
}