This repository has been archived by the owner on Aug 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
lualibs-set.lua
87 lines (77 loc) · 1.88 KB
/
lualibs-set.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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"
}
-- This will become obsolete when we have the bitset library embedded.
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, n = { }, 0
for k, v in next, tabs[n] do
if v then
n = n + 1
t[n] = 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'))