-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
---@class TrieNode | ||
---@field children table<string, TrieNode> | ||
---@field end_of_word boolean | ||
local TrieNode = {} | ||
|
||
---@return TrieNode | ||
function TrieNode.new() | ||
return { children = {}, end_of_word = false } | ||
end | ||
|
||
---@class Trie | ||
---@field root TrieNode | ||
local Trie = {} | ||
|
||
---@return Trie | ||
function Trie.new() | ||
return setmetatable({ | ||
root = TrieNode.new(), | ||
}, { __index = Trie }) | ||
end | ||
|
||
---@param word string | ||
function Trie:insert(word) | ||
local current = self.root | ||
for char in vim.gsplit(word, "") do | ||
local node = current.children[char] or TrieNode.new() | ||
current.children[char] = node | ||
current = node | ||
end | ||
current.end_of_word = true | ||
end | ||
|
||
---@private | ||
---@param node TrieNode | ||
---@param prefix string | ||
---@param word_list string[] | ||
---@param limit integer | ||
function Trie:search_prefix(node, prefix, word_list, limit) | ||
if limit >= 0 and #word_list >= limit then | ||
return | ||
end | ||
if node.end_of_word then | ||
table.insert(word_list, prefix) | ||
end | ||
for char, child in pairs(node.children) do | ||
self:search_prefix(child, prefix .. char, word_list, limit) | ||
end | ||
end | ||
|
||
---@param prefix string | ||
---@param limit integer | ||
---@return string[] | ||
function Trie:search(prefix, limit) | ||
local node = self.root | ||
for char in vim.gsplit(prefix, "") do | ||
node = node.children[char] | ||
if node == nil then | ||
return {} | ||
end | ||
end | ||
local word_list = {} | ||
self:search_prefix(node, prefix, word_list, limit) | ||
return word_list | ||
end | ||
|
||
return Trie |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters