summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/util-tab.lua
diff options
context:
space:
mode:
authorHans Hagen <pragma@wxs.nl>2019-01-17 23:06:17 +0100
committerContext Git Mirror Bot <phg@phi-gamma.net>2019-01-17 23:06:17 +0100
commit823bd4a7d8ff32c05807b02e650ecbd60b43e95d (patch)
treef6ac3760c1d5da04f8570587b76572cd4fb44ecd /tex/context/base/mkiv/util-tab.lua
parent17527db3823d6123f4e462d13244430c40b78adb (diff)
downloadcontext-823bd4a7d8ff32c05807b02e650ecbd60b43e95d.tar.gz
2019-01-17 22:18:00
Diffstat (limited to 'tex/context/base/mkiv/util-tab.lua')
-rw-r--r--tex/context/base/mkiv/util-tab.lua50
1 files changed, 50 insertions, 0 deletions
diff --git a/tex/context/base/mkiv/util-tab.lua b/tex/context/base/mkiv/util-tab.lua
index f395f43de..ed4cef996 100644
--- a/tex/context/base/mkiv/util-tab.lua
+++ b/tex/context/base/mkiv/util-tab.lua
@@ -820,3 +820,53 @@ if setinspector then
end)
end
+-- ordered hashes (for now here but in the table namespace):
+
+-- local t = table.orderedhash()
+--
+-- t["1"] = { "a", "b" }
+-- t["2"] = { }
+-- t["2a"] = { "a", "c", "d" }
+--
+-- for k, v in table.ordered(t) do
+-- ...
+-- end
+
+local mt = {
+ __newindex = function(t,k,v)
+ local n = t.last + 1
+ t.last = n
+ t.list[n] = k
+ t.hash[k] = v
+ end,
+ __index = function(t,k)
+ return t.hash[k]
+ end,
+ __len = function(t)
+ return t.last
+ end,
+}
+
+function table.orderedhash()
+ return setmetatable({ list = { }, hash = { }, last = 0 }, mt)
+end
+
+function table.ordered(t)
+ local n = t.last
+ if n > 0 then
+ local l = t.list
+ local i = 1
+ local h = t.hash
+ local f = function()
+ if i <= n then
+ local k = i
+ local v = h[l[k]]
+ i = i + 1
+ return k, v
+ end
+ end
+ return f, 1, h[l[1]]
+ else
+ return function() end
+ end
+end