Skip to content

Commit

Permalink
refactor(hash): improve hashing algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Aug 22, 2024
1 parent 1dddd32 commit ada0798
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lua/onedarkpro/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ end
---Create a hash from the config
---@return string|number
function M.hash()
local hash = require("onedarkpro.lib.hash")(M.config)
local hash = require("onedarkpro.lib.hash").hash(M.config)
return hash and hash or 0
end

Expand Down
38 changes: 16 additions & 22 deletions lua/onedarkpro/lib/hash.lua
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
4 changes: 2 additions & 2 deletions tests/cache_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("Using the cache", function()
yaml = true,
},
}
hash = require("onedarkpro.lib.hash")(tbl)
hash = require("onedarkpro.lib.hash").hash(tbl)
end)

it("the SAME table should always return the SAME hash", function()
Expand All @@ -51,6 +51,6 @@ describe("Using the cache", function()
tbl.colors = {
red = "#ff0000",
}
assert.not_equals(hash, require("onedarkpro.lib.hash")(tbl))
assert.not_equals(hash, require("onedarkpro.lib.hash").hash(tbl))
end)
end)

0 comments on commit ada0798

Please sign in to comment.