summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/util-pck.lua
diff options
context:
space:
mode:
authorTaco Hoekwater <taco@elvenkind.com>2011-06-01 08:54:21 +0000
committerTaco Hoekwater <taco@elvenkind.com>2011-06-01 08:54:21 +0000
commitd7ccb42582f85acf30568913610ccf4d602023fb (patch)
tree7292e3545a420676878e7451b68892d360c62cb6 /Master/texmf-dist/tex/context/base/util-pck.lua
parent2d62a6fe9b80def59c392268022f1f9a2d6e358f (diff)
commit context 2011.05.18
git-svn-id: svn://tug.org/texlive/trunk@22719 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/context/base/util-pck.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/util-pck.lua142
1 files changed, 142 insertions, 0 deletions
diff --git a/Master/texmf-dist/tex/context/base/util-pck.lua b/Master/texmf-dist/tex/context/base/util-pck.lua
new file mode 100644
index 00000000000..c802a09ed81
--- /dev/null
+++ b/Master/texmf-dist/tex/context/base/util-pck.lua
@@ -0,0 +1,142 @@
+if not modules then modules = { } end modules ['util-pck'] = {
+ version = 1.001,
+ comment = "companion to luat-lib.mkiv",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+-- moved from core-uti
+
+local next, tostring, type = next, tostring, type
+local sort, concat = table.sort, table.concat
+
+utilities = utilities or { }
+utilities.packers = utilities.packers or { }
+local packers = utilities.packers
+packers.version = 1.00
+
+local function hashed(t)
+ local s, ns = { }, 0
+ for k, v in next, t do
+ ns = ns + 1
+ if type(v) == "table" then
+ s[ns] = k .. "={" .. hashed(v) .. "}"
+ else
+ s[ns] = k .. "=" .. tostring(v)
+ end
+ end
+ sort(s)
+ return concat(s,",")
+end
+
+local function simplehashed(t)
+ local s, ns = { }, 0
+ for k, v in next, t do
+ ns = ns + 1
+ s[ns] = k .. "=" .. v
+ end
+ sort(s)
+ return concat(s,",")
+end
+
+packers.hashed = hashed
+packers.simplehashed = simplehashed
+
+--~ local function pack(t,keys,hash,index)
+--~ for k,v in next, t do
+--~ if type(v) == "table" then
+--~ pack(v,keys,hash,index)
+--~ end
+--~ if keys[k] and type(v) == "table" then
+--~ local h = hashed(v)
+--~ local i = hash[h]
+--~ if not i then
+--~ i = #index + 1
+--~ index[i] = v
+--~ hash[h] = i
+--~ end
+--~ t[k] = i
+--~ end
+--~ end
+--~ end
+
+local function pack(t,keys,hash,index)
+ for k,v in next, t do
+ if type(v) == "table" then
+ pack(v,keys,hash,index)
+ if keys[k] then
+ local h = hashed(v)
+ local i = hash[h]
+ if not i then
+ i = #index + 1
+ index[i] = v
+ hash[h] = i
+ end
+ t[k] = i
+ end
+ end
+ end
+end
+
+local function unpack(t,keys,index)
+ for k,v in next, t do
+ if keys[k] and type(v) == "number" then
+ local iv = index[v]
+ if iv then
+ v = iv
+ t[k] = v
+ end
+ end
+ if type(v) == "table" then
+ unpack(v,keys,index)
+ end
+ end
+end
+
+function packers.new(keys,version)
+ return {
+ version = version or packers.version,
+ keys = table.tohash(keys),
+ hash = { },
+ index = { },
+ }
+end
+
+function packers.pack(t,p,shared)
+ if shared then
+ pack(t,p.keys,p.hash,p.index)
+ elseif not t.packer then
+ pack(t,p.keys,p.hash,p.index)
+ if #p.index > 0 then
+ t.packer = {
+ version = p.version or packers.version,
+ keys = p.keys,
+ index = p.index,
+ }
+ end
+ p.hash, p.index = { }, { }
+ end
+end
+
+function packers.unpack(t,p,shared)
+ if shared then
+ if p then
+ unpack(t,p.keys,p.index)
+ end
+ else
+ local tp = t.packer
+ if tp then
+ if tp.version == (p and p.version or packers.version) then
+ unpack(t,tp.keys,tp.index)
+ else
+ -- fatal error, wrong version
+ end
+ t.packer = nil
+ end
+ end
+end
+
+function packers.strip(p)
+ p.hash = nil
+end