[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Hashing on more than one attribute?
- From: Enrique Garcia Cota <kikito@...>
- Date: Thu, 8 Nov 2012 10:01:52 +0100
> How about simply having nested datastructures? Yes, it takes a few lookups instead of 1. But on the other hand you create less garbage to be collected when concating.
That is what I usually do when I have this problem.
I organize the cache in a "Tree-like" structure where each node has one child per key. Inserting/retrieving elements from the cache involves one loop instead of a single table.concat, but it avoids problems related with tostring + concat (name collisions are not possible and tables could all have the same tostring metamethod with no issue).
function multiGet(store, key1, ...)
assert(key1 ~= nil, "Must provide at least one key")
local keys = {key1, ...}
local node = store
for i=1, #keys do
if not node.children then return nil end
node = node.children[keys[i]]
if not node then return nil end
end
return node.value
end
function multiSet(store, value, key1, ...)
assert(key1 ~= nil, "Must provide at least one key")
local keys = {key1, ...}
local node = store
local key
for i=1, #keys do
key = keys[i]
node.children = node.children or {}
node.children[key] = node.children[key] or {}
node = node.children[key]
end
node.value = value
end
local t = {}
multiSet(t, "a value", "foo", "oo", "oo")
multiGet(t, "foo", "oo", "oo") -- "a value"
multiGet(t, "fooo", "ooo") -- nil
I've used this strategy to organize the cache of my memoize with multiple params method: