Re: Hashing on more than one attribute?
[
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:
On Thu, Nov 8, 2012 at 9:15 AM, Axel Kittenberger
<axkibe@gmail.com> wrote:
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.
On Thu, Nov 8, 2012 at 4:36 AM, Marc Lepage
<mlepage@antimeta.com> wrote:
I know it's trivial to hash on a single value, by putting it into a table field. I want to effectively store a composite key in a table: say, a string, a number, and another string.
Is there a way I can somehow produce a single hash of that combination of three values, to use as a key in a field? Something not obvious? Or must I effectively have a few levels of tables to hash 3 values? (And more for 4, 5, 6 values...)
Thanks,
Marc