summaryrefslogtreecommitdiff
path: root/luaextra-set.lua
diff options
context:
space:
mode:
authorKhaled Hosny <khaledhosny@eglug.org>2010-01-11 15:15:52 +0200
committerKhaled Hosny <khaledhosny@eglug.org>2010-01-11 16:03:13 +0200
commitf1c3cb9dc199c28cdd2c813eb1ea5c21345125d0 (patch)
tree808986ca292251a39a2fc9fa3a7e01b9e761577e /luaextra-set.lua
parente5ad063f805ecbf5fd093712bc60ef5aec25a6fd (diff)
downloadlualibs-f1c3cb9dc199c28cdd2c813eb1ea5c21345125d0.tar.gz
Import ConTeX's lua libraries
Replace most of luaextra.lua with a bunch of require() calls and add renamed (but unmodified) ConTeX lua libraries to the repository. ConTeXt's l-xml.lua module has been excluded because it depends on a bunch of other ConTeXt specific modules. Also l-pdfview.lua has been dropped, I don't know what to use it for.
Diffstat (limited to 'luaextra-set.lua')
-rw-r--r--luaextra-set.lua84
1 files changed, 84 insertions, 0 deletions
diff --git a/luaextra-set.lua b/luaextra-set.lua
new file mode 100644
index 0000000..f844d0b
--- /dev/null
+++ b/luaextra-set.lua
@@ -0,0 +1,84 @@
+if not modules then modules = { } end modules ['l-set'] = {
+ 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"
+}
+
+set = set or { }
+
+local nums = { }
+local tabs = { }
+local concat = table.concat
+local next, type = next, type
+
+set.create = table.tohash
+
+function set.tonumber(t)
+ if next(t) then
+ local s = ""
+ -- we could save mem by sorting, but it slows down
+ for k, v in next, t do
+ if v then
+ -- why bother about the leading space
+ s = s .. " " .. k
+ end
+ end
+ local n = nums[s]
+ if not n then
+ n = #tabs + 1
+ tabs[n] = t
+ nums[s] = n
+ end
+ return n
+ else
+ return 0
+ end
+end
+
+function set.totable(n)
+ if n == 0 then
+ return { }
+ else
+ return tabs[n] or { }
+ end
+end
+
+function set.tolist(n)
+ if n == 0 or not tabs[n] then
+ return ""
+ else
+ local t = { }
+ for k, v in next, tabs[n] do
+ if v then
+ t[#t+1] = k
+ end
+ end
+ return concat(t," ")
+ end
+end
+
+function set.contains(n,s)
+ if type(n) == "table" then
+ return n[s]
+ elseif n == 0 then
+ return false
+ else
+ local t = tabs[n]
+ return t and t[s]
+ end
+end
+
+--~ local c = set.create{'aap','noot','mies'}
+--~ local s = set.tonumber(c)
+--~ local t = set.totable(s)
+--~ print(t['aap'])
+--~ local c = set.create{'zus','wim','jet'}
+--~ local s = set.tonumber(c)
+--~ local t = set.totable(s)
+--~ print(t['aap'])
+--~ print(t['jet'])
+--~ print(set.contains(t,'jet'))
+--~ print(set.contains(t,'aap'))
+