-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(hash): improve hashing algorithm
- Loading branch information
Showing
3 changed files
with
19 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,29 @@ | ||
---This library allows us to hash the contents of a file. This gives us a ref | ||
---point in deciding if the theme should be re-compiled. | ||
---(Source: https://github.com/catppuccin/nvim/blob/main/lua/catppuccin/lib/hashing.lua) | ||
---(Source: https://github.com/EdenEast/nightfox.nvim/blob/main/lua/nightfox/lib/hash.lua) | ||
local bitop = bit or bit32 or require("onedarkpro.lib.native_bit") | ||
local M = {} | ||
local B = bit or bit32 or require("onedarkpro.lib.native_bit") | ||
|
||
-- https://theartincode.stanis.me/008-djb2/ | ||
---@param s string | ||
---@return number | ||
local function djb2(s) | ||
local h = 5381 | ||
for i = 1, #s do | ||
h = bitop.lshift(h, 5) + h + string.byte(s, i) -- h * 33 + c | ||
local hash_str = function(str) -- djb2, https://theartincode.stanis.me/008-djb2/ | ||
local hash = 5381 | ||
for i = 1, #str do | ||
hash = B.lshift(hash, 5) + hash + string.byte(str, i) | ||
end | ||
return h | ||
return hash | ||
end | ||
|
||
-- Reference: https://github.com/catppuccin/nvim/blob/bad9c23f12944683cd11484d9570560849efc101/lua/catppuccin/lib/hashing.lua | ||
---@param x table|function|number|string | ||
---@return string|number | ||
local function hash(x) | ||
local t = type(x) | ||
function M.hash(v) -- Xor hashing: https://codeforces.com/blog/entry/85900 | ||
local t = type(v) | ||
if t == "table" then | ||
local h = 0 | ||
for k, v in next, x do | ||
h = bitop.bxor(h, djb2(k .. hash(v))) | ||
local hash = 0 | ||
for p, u in next, v do | ||
hash = B.bxor(hash, hash_str(p .. M.hash(u))) | ||
end | ||
return h | ||
return hash | ||
elseif t == "function" then | ||
return djb2(string.dump(x)) | ||
return M.hash(string.dump(v)) | ||
end | ||
return tostring(x) | ||
return tostring(v) | ||
end | ||
|
||
return hash | ||
return M |
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