summaryrefslogtreecommitdiff
path: root/lualibs-set.lua
diff options
context:
space:
mode:
authorKhaled Hosny <khaledhosny@eglug.org>2010-05-10 17:23:06 +0300
committerKhaled Hosny <khaledhosny@eglug.org>2010-05-10 17:23:06 +0300
commit7002dfe5556f503320d9bee480e5fb43bdb6e7a3 (patch)
tree384ee73556657b6818c78b006cfa625776c980cd /lualibs-set.lua
parent20864d5fdb81f248500f2e94c95b3b15569cc277 (diff)
downloadlualibs-7002dfe5556f503320d9bee480e5fb43bdb6e7a3.tar.gz
Rename to lualibs
Rename luaextra->lualibs when sensible, also use luatexbase instead of luatextra.
Diffstat (limited to 'lualibs-set.lua')
-rw-r--r--lualibs-set.lua84
1 files changed, 84 insertions, 0 deletions
diff --git a/lualibs-set.lua b/lualibs-set.lua
new file mode 100644
index 0000000..f844d0b
--- /dev/null
+++ b/lualibs-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'))
+