diff options
author | Karl Berry <karl@freefriends.org> | 2019-02-28 15:11:56 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2019-02-28 15:11:56 +0000 |
commit | cc17ad601696f5cf966c8a0b9d79144cd42828a6 (patch) | |
tree | f6007b9b2f886d942f9b30b4797e8718001f65bd /Master/texmf-dist/tex/context/base/mkiv/util-tab.lua | |
parent | e298746bb92cdf7e87bc3b5b9bd973b6c429a47f (diff) |
context for tl19
git-svn-id: svn://tug.org/texlive/trunk@50165 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/context/base/mkiv/util-tab.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/mkiv/util-tab.lua | 78 |
1 files changed, 65 insertions, 13 deletions
diff --git a/Master/texmf-dist/tex/context/base/mkiv/util-tab.lua b/Master/texmf-dist/tex/context/base/mkiv/util-tab.lua index 1b069e2ae21..be5f1318cbc 100644 --- a/Master/texmf-dist/tex/context/base/mkiv/util-tab.lua +++ b/Master/texmf-dist/tex/context/base/mkiv/util-tab.lua @@ -22,7 +22,8 @@ local utftoeight = utf.toeight local splitter = lpeg.tsplitat(".") function utilities.tables.definetable(target,nofirst,nolast) -- defines undefined tables - local composed, t = nil, { } + local composed = nil + local t = { } local snippets = lpegmatch(splitter,target) for i=1,#snippets - (nolast and 1 or 0) do local name = snippets[i] @@ -334,7 +335,6 @@ function table.fastserialize(t,prefix) local r = { type(prefix) == "string" and prefix or "return" } local m = 1 - local function fastserialize(t,outer) -- no mixes local n = #t m = m + 1 @@ -654,19 +654,20 @@ local function serialize(root,name,specification) -- we could check for k (index) being number (cardinal) if root and next(root) ~= nil then local first = nil - local last = 0 - last = #root - for k=1,last do - if rawget(root,k) == nil then - -- if root[k] == nil then - last = k - 1 - break - end - end + local last = #root if last > 0 then - first = 1 + for k=1,last do + if rawget(root,k) == nil then + -- if root[k] == nil then + last = k - 1 + break + end + end + if last > 0 then + first = 1 + end end - local sk = sortedkeys(root) -- inline fast version?\ + local sk = sortedkeys(root) for i=1,#sk do local k = sk[i] local v = root[k] @@ -819,3 +820,54 @@ if setinspector then end 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 |