-
Notifications
You must be signed in to change notification settings - Fork 72
/
Class_Trie.ahk
163 lines (144 loc) · 3.35 KB
/
Class_Trie.ahk
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
Class Trie {
__Data := {}
__Count := 0
__Bool := False
__New(Max := False, params*) {
this.__Max := Max
for i, v in params {
this.__Set(v, "")
}
}
__Get(key) {
if(!__StrInList(key, ["__Data", "__Count", "__Max", "__Bool"])) {
if(StrLen(key) > 0) {
obj := this.__Data
for i, char in StrSplit(key) {
obj := obj[char]
}
this.__Count := 0
return obj ? StrSplit(SubStr(this.getKeys(obj, key), 1, -1), "`n") : []
}
}
}
__Set(key, val) {
if(!__StrInList(key, ["__Data", "__Count", "__Max", "__Bool"])) {
if(StrLen(key) > 0) {
this.__Bool := True
obj := this.__Data
for i, char in StrSplit(key) {
; Save reference to the parent of the leaf for use after loop
preObj := obj
obj := this.addCharToTrie(obj, char)
}
; Handle if word is ending inside the trie
if(!this.objIsEmty(obj)) {
obj["word"] := True
}
; If the key ends in a leaf it don't need to be an object
if(this.objIsEmty(obj)) {
preObj[char] := True ; (this is to save memory)
}
}
}
}
remove(key) {
if(this.__Data.HasKey(SubStr(key, 1, 1))) {
; Get references to all the characters in the branch
branch := [this.__Data[SubStr(key, 1, 1)]]
for i, char in StrSplit(SubStr(key, 2)) {
if(branch[i].HasKey(char)) {
branch.Push(branch[i][char])
} else {
return
}
}
len := branch.Length()
; Handle if the key is not ending in a leaf
branch[branch.MaxIndex()].Delete("word")
; Deleat chars from the leaf to the root if nothing else will disappear as a result of the deletion
loop % len {
index := len + 1 - A_Index
if(this.objIsEmty(branch[index], "")) {
branch[index - 1].Delete(SubStr(key, index, 1))
} else {
if(this.objIsEmty(branch[index])) {
branch[index - 1][SubStr(key, index, 1)] := True ; (this is to save memory)
}
return
}
}
}
}
getKeys(obj, key) {
if(this.objIsEmty(obj)) {
this.__Count++
return key "`n"
}
if(obj.HasKey("word")) {
this.__Count++
res := key "`n"
}
for char in obj {
if(this.__Max && this.__Count >= this.__Max) {
Break
}
if(StrLen(char) == 1) {
res .= this.getKeys(obj[char], key char)
}
}
return res
}
addCharToTrie(obj, key) {
if(!obj.HasKey(key)) {
; If a new leaf is added from an empty leaf, there must have been a word ending in the late leaf
if(this.__Bool && this.objIsEmty(obj) && obj != this.__Data) {
obj["word"] := True
}
this.addLeaf(obj, key)
this.__Bool := False
}
; If it is a leaf it will not ba an object
if(!IsObject(obj[key])){
this.addLeaf(obj, key)
}
return obj[key]
}
addLeaf(obj, char) {
obj[char] := {}
obj.SetCapacity(0) ; (this is to save memory)
}
objIsEmty(obj, except := "word") {
for key in obj {
if(key != except) {
return False
}
}
return True
}
}
__StrInList(str, lst) {
if str in % __Join(",", lst)
return True
return False
}
__Join(sep, params*) {
for index, param in params {
str .= param sep
if (index>50) ; added
break
}
return SubStr(str, 1, -StrLen(sep))
}
MsgObj(obj) {
MsgBox, % SubStr(ObjectToString(obj), 1, -1)
}
ObjectToString(obj){
if(!IsObject(obj)){
return """" obj """"
}
res := "{"
for key, value in obj {
res .= """" key """ : " ObjectToString(value) ", "
}
return SubStr(res, 1, -2) "}"
}