aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/generators/hashy.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/generators/hashy.lua')
-rw-r--r--src/nvim/generators/hashy.lua83
1 files changed, 45 insertions, 38 deletions
diff --git a/src/nvim/generators/hashy.lua b/src/nvim/generators/hashy.lua
index b10bafb9f9..711e695742 100644
--- a/src/nvim/generators/hashy.lua
+++ b/src/nvim/generators/hashy.lua
@@ -3,7 +3,6 @@
local M = {}
_G.d = M
-
local function setdefault(table, key)
local val = table[key]
if val == nil then
@@ -16,28 +15,30 @@ end
function M.build_pos_hash(strings)
local len_buckets = {}
local maxlen = 0
- for _,s in ipairs(strings) do
- table.insert(setdefault(len_buckets, #s),s)
- if #s > maxlen then maxlen = #s end
+ for _, s in ipairs(strings) do
+ table.insert(setdefault(len_buckets, #s), s)
+ if #s > maxlen then
+ maxlen = #s
+ end
end
local len_pos_buckets = {}
local worst_buck_size = 0
- for len = 1,maxlen do
+ for len = 1, maxlen do
local strs = len_buckets[len]
if strs then
-- the best position so far generates `best_bucket`
-- with `minsize` worst case collisions
- local bestpos, minsize, best_bucket = nil, #strs*2, nil
- for pos = 1,len do
+ local bestpos, minsize, best_bucket = nil, #strs * 2, nil
+ for pos = 1, len do
local try_bucket = {}
- for _,str in ipairs(strs) do
+ for _, str in ipairs(strs) do
local poschar = string.sub(str, pos, pos)
table.insert(setdefault(try_bucket, poschar), str)
end
local maxsize = 1
- for _,pos_strs in pairs(try_bucket) do
+ for _, pos_strs in pairs(try_bucket) do
maxsize = math.max(maxsize, #pos_strs)
end
if maxsize < minsize then
@@ -46,7 +47,7 @@ function M.build_pos_hash(strings)
best_bucket = try_bucket
end
end
- len_pos_buckets[len] = {bestpos, best_bucket}
+ len_pos_buckets[len] = { bestpos, best_bucket }
worst_buck_size = math.max(worst_buck_size, minsize)
end
end
@@ -55,73 +56,79 @@ end
function M.switcher(put, tab, maxlen, worst_buck_size)
local neworder = {}
- put " switch (len) {\n"
+ put ' switch (len) {\n'
local bucky = worst_buck_size > 1
- for len = 1,maxlen do
+ for len = 1, maxlen do
local vals = tab[len]
if vals then
- put(" case "..len..": ")
+ put(' case ' .. len .. ': ')
local pos, posbuck = unpack(vals)
local keys = vim.tbl_keys(posbuck)
if #keys > 1 then
table.sort(keys)
- put("switch (str["..(pos-1).."]) {\n")
- for _,c in ipairs(keys) do
+ put('switch (str[' .. (pos - 1) .. ']) {\n')
+ for _, c in ipairs(keys) do
local buck = posbuck[c]
local startidx = #neworder
vim.list_extend(neworder, buck)
local endidx = #neworder
- put(" case '"..c.."': ")
- put("low = "..startidx.."; ")
- if bucky then put("high = "..endidx.."; ") end
- put "break;\n"
+ put(" case '" .. c .. "': ")
+ put('low = ' .. startidx .. '; ')
+ if bucky then
+ put('high = ' .. endidx .. '; ')
+ end
+ put 'break;\n'
end
- put " default: break;\n"
- put " }\n "
+ put ' default: break;\n'
+ put ' }\n '
else
- local startidx = #neworder
- table.insert(neworder, posbuck[keys[1]][1])
- local endidx = #neworder
- put("low = "..startidx.."; ")
- if bucky then put("high = "..endidx.."; ") end
+ local startidx = #neworder
+ table.insert(neworder, posbuck[keys[1]][1])
+ local endidx = #neworder
+ put('low = ' .. startidx .. '; ')
+ if bucky then
+ put('high = ' .. endidx .. '; ')
+ end
end
- put "break;\n"
+ put 'break;\n'
end
end
- put " default: break;\n"
- put " }\n"
+ put ' default: break;\n'
+ put ' }\n'
return neworder
end
function M.hashy_hash(name, strings, access)
local stats = {}
- local put = function(str) table.insert(stats, str) end
+ local put = function(str)
+ table.insert(stats, str)
+ end
local len_pos_buckets, maxlen, worst_buck_size = M.build_pos_hash(strings)
- put("int "..name.."_hash(const char *str, size_t len)\n{\n")
+ put('int ' .. name .. '_hash(const char *str, size_t len)\n{\n')
if worst_buck_size > 1 then
- put(" int low = 0, high = 0;\n")
+ put(' int low = 0, high = 0;\n')
else
- put(" int low = -1;\n")
+ put(' int low = -1;\n')
end
local neworder = M.switcher(put, len_pos_buckets, maxlen, worst_buck_size)
if worst_buck_size > 1 then
- put ([[
+ put([[
for (int i = low; i < high; i++) {
- if (!memcmp(str, ]]..access("i")..[[, len)) {
+ if (!memcmp(str, ]] .. access('i') .. [[, len)) {
return i;
}
}
return -1;
]])
else
- put ([[
- if (low < 0 || memcmp(str, ]]..access("low")..[[, len)) {
+ put([[
+ if (low < 0 || memcmp(str, ]] .. access('low') .. [[, len)) {
return -1;
}
return low;
]])
end
- put "}\n\n"
+ put '}\n\n'
return neworder, table.concat(stats)
end