aboutsummaryrefslogtreecommitdiff
path: root/lua/trie.lua
blob: e74d9eb9600f66b1e5e8fdb472d74fab67d3a4c4 (plain)
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
--- Trie implementation in luajit
-- Copyright © 2019 Ashkan Kiani

-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.

-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.

-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
local ffi = require 'ffi'

local bnot = bit.bnot
local band, bor, bxor = bit.band, bit.bor, bit.bxor
local lshift, rshift, rol = bit.lshift, bit.rshift, bit.rol

ffi.cdef [[
struct Trie {
	bool is_leaf;
	struct Trie* character[62];
};
void *malloc(size_t size);
void free(void *ptr);
]]

local Trie_t = ffi.typeof('struct Trie')
local Trie_ptr_t = ffi.typeof('$ *', Trie_t)
local Trie_size = ffi.sizeof(Trie_t)

local function byte_to_index(b)
	-- 0-9 starts at string.byte('0') == 0x30 == 48 == 0b0011_0000
	-- A-Z starts at string.byte('A') == 0x41 == 65 == 0b0100_0001
	-- a-z starts at string.byte('a') == 0x61 == 97 == 0b0110_0001

	-- This works for mapping characters to
	-- 0-9 A-Z a-z in that order
	-- Letters have bit 0x40 set, so we use that as an indicator for
	-- an additional offset from the space of the digits, and then
	-- add the 10 allocated for the range of digits.
	-- Then, within that indicator for letters, we subtract another
	-- (65 - 97) which is the difference between lower and upper case
	-- and add back another 26 to allocate for the range of uppercase
	-- letters.
	-- return b - 0x30
	-- 	+ rshift(b, 6) * (
	-- 		0x30 - 0x41
	-- 		+ 10
	-- 		+ band(1, rshift(b, 5)) * (
	-- 			0x61 - 0x41
	-- 			+ 26
	-- 		))
	return b - 0x30 - rshift(b, 6) * (7 + band(1, rshift(b, 5)) * 6)
end

local function insensitive_byte_to_index(b)
	-- return b - 0x30
	-- 	+ rshift(b, 6) * (
	-- 		0x30 - 0x61
	-- 		+ 10
	-- 	)
	b = bor(b, 0x20)
	return b - 0x30 - rshift(b, 6) * 39
end

local function verify_byte_to_index()
	local chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
	for i = 1, #chars do
		local c = chars:sub(i,i)
		local index = byte_to_index(string.byte(c))
		assert((i-1) == index, vim.inspect{index=index,c=c})
	end
end

local function new_trie()
	local ptr = ffi.C.malloc(Trie_size)
	ffi.fill(ptr, Trie_size)
	return ffi.cast(Trie_ptr_t, ptr)
end

local INDEX_LOOKUP_TABLE = ffi.new('uint8_t[256]')
local b_a = string.byte('a')
local b_z = string.byte('z')
local b_A = string.byte('A')
local b_Z = string.byte('Z')
local b_0 = string.byte('0')
local b_9 = string.byte('9')
for i = 0, 255 do
	if i >= b_0 and i <= b_9 then
		INDEX_LOOKUP_TABLE[i] = i - b_0
	elseif i >= b_A and i <= b_Z then
		INDEX_LOOKUP_TABLE[i] = i - b_A + 10
	elseif i >= b_a and i <= b_z then
		INDEX_LOOKUP_TABLE[i] = i - b_a + 10 + 26
	else
		INDEX_LOOKUP_TABLE[i] = 255
	end
end

local function insert(trie, value)
	if trie == nil then return false end
	local node = trie
	for i = 1, #value do
		local index = INDEX_LOOKUP_TABLE[value:byte(i)]
		if index == 255 then
			return false
		end
		if node.character[index] == nil then
			node.character[index] = new_trie()
		end
		node = node.character[index]
	end
	node.is_leaf = true
	return node, trie
end

local function search(trie, value)
	if trie == nil then return false end
	local node = trie
	for i = 1, #value do
		local index = INDEX_LOOKUP_TABLE[value:byte(i)]
		if index == 255 then
			return
		end
		local child = node.character[index]
		if child == nil then
			return false
		end
		node = child
	end
	return node.is_leaf
end

local function longest_prefix(trie, value)
	if trie == nil then return false end
	local node = trie
	local last_i = nil
	for i = 1, #value do
		local index = INDEX_LOOKUP_TABLE[value:byte(i)]
		if index == 255 then
			break
		end
		local child = node.character[index]
		if child == nil then
			break
		end
		if child.is_leaf then
			last_i = i
		end
		node = child
	end
	if last_i then
		return value:sub(1, last_i)
	end
end

--- Printing utilities

local function index_to_char(index)
	if index < 10 then
		return string.char(index + b_0)
	elseif index < 36 then
		return string.char(index - 10 + b_A)
	else
		return string.char(index - 26 - 10 + b_a)
	end
end

local function trie_structure(trie)
	if trie == nil then
		return nil
	end
	local children = {}
	for i = 0, 61 do
		local child = trie.character[i]
		if child ~= nil then
			local child_table = trie_structure(child)
			child_table.c = index_to_char(i)
			table.insert(children, child_table)
		end
	end
	return {
		is_leaf = trie.is_leaf;
		children = children;
	}
end

local function print_structure(s)
	local mark
	if not s then
		return nil
	end
	if s.c then
		if s.is_leaf then
			mark = s.c.."*"
		else
			mark = s.c.."─"
		end
	else
		mark = "├─"
	end
	if #s.children == 0 then
		return {mark}
	end
	local lines = {}
	for _, child in ipairs(s.children) do
		local child_lines = print_structure(child)
		for _, child_line in ipairs(child_lines) do
			table.insert(lines, child_line)
		end
	end
	for i, v in ipairs(lines) do
		if v:match("^[%w%d]") then
			if i == 1 then
				lines[i] = mark.."─"..v
			elseif i == #lines then
				lines[i] = "└──"..v
			else
				lines[i] = "├──"..v
			end
		else
			if i == 1 then
				lines[i] = mark.."─"..v
			elseif #s.children > 1 then
				lines[i] = "│  "..v
			else
				lines[i] = "   "..v
			end
		end
	end
	return lines
end

local function free_trie(trie)
	if trie == nil then
		return
	end
	for i = 0, 61 do
		local child = trie.character[i]
		if child ~= nil then
			free_trie(child)
		end
	end
	ffi.C.free(trie)
end

local Trie_mt = {
	__new = new_trie;
	__index = {
		insert = insert;
		search = search;
		longest_prefix = longest_prefix;
	};
	__tostring = function(trie)
		local structure = trie_structure(trie)
		if structure then
			return table.concat(print_structure(structure), '\n')
		else
			return 'nil'
		end
	end;
	__gc = free_trie;
}

return ffi.metatype('struct Trie', Trie_mt)

-- local tests = {
-- 	"cat";
-- 	"car";
-- 	"celtic";
-- 	"carb";
-- 	"carb0";
-- 	"CART0";
-- 	"CaRT0";
-- 	"Cart0";
-- 	"931";
-- 	"191";
-- 	"121";
-- 	"cardio";
-- 	"call";
-- 	"calcium";
-- 	"calciur";
-- 	"carry";
-- 	"dog";
-- 	"catdog";
-- }
-- local trie = Trie()
-- for i, v in ipairs(tests) do
-- 	trie:insert(v)
-- end

-- print(trie)
-- print(trie.character[0])
-- print("catdo", trie:longest_prefix("catdo"))
-- print("catastrophic", trie:longest_prefix("catastrophic"))

-- local COLOR_MAP = vim.api.nvim_get_color_map()
-- local start = os.clock()
-- for k, v in pairs(COLOR_MAP) do
-- 	insert(trie, k)
-- end
-- print(os.clock() - start)

-- print(table.concat(print_structure(trie_structure(trie)), '\n'))