From f1c3cb9dc199c28cdd2c813eb1ea5c21345125d0 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Mon, 11 Jan 2010 15:15:52 +0200 Subject: 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. --- luaextra-set.lua | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 luaextra-set.lua (limited to 'luaextra-set.lua') 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')) + -- cgit v1.2.3