2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local t_insert
= table.insert
;
11 local pairs
, next, type = pairs
, next, type;
12 local unpack
= table.unpack
or unpack
; --luacheck: ignore 113
17 local function get(self
, ...)
19 for n
= 1,select('#', ...) do
20 t
= t
[select(n
, ...)];
21 if not t
then break; end
26 local function add(self
, ...)
28 local count
= select('#', ...);
30 local key
= select(n
, ...);
32 if not tab
then tab
= {}; t
[key
] = tab
; end
35 t_insert(t
, (select(count
, ...)));
38 local function set(self
, ...)
40 local count
= select('#', ...);
42 local key
= select(n
, ...);
44 if not tab
then tab
= {}; t
[key
] = tab
; end
47 t
[(select(count
-1, ...))] = (select(count
, ...));
50 local function r(t
, n
, _end
, ...)
51 if t
== nil then return; end
52 local k
= select(n
, ...);
66 for _
,b
in pairs(t
) do
75 local function remove(self
, ...)
76 local _end
= select('#', ...);
78 if select(n
, ...) then _end
= n
; break; end
80 r(self
.data
, 1, _end
, ...);
84 local function s(t
, n
, results
, _end
, ...)
85 if t
== nil then return; end
86 local k
= select(n
, ...);
89 for _
, v
in pairs(t
) do
93 t_insert(results
, t
[k
]);
100 s(v
, n
+1, results
, _end
, ...);
103 for _
,b
in pairs(t
) do
104 s(b
, n
+1, results
, _end
, ...);
109 -- Search for keys, nil == wildcard
110 local function search(self
, ...)
111 local _end
= select('#', ...);
113 if select(n
, ...) then _end
= n
; break; end
116 s(self
.data
, 1, results
, _end
, ...);
120 -- Append results to an existing list
121 local function search_add(self
, results
, ...)
122 if not results
then results
= {}; end
123 local _end
= select('#', ...);
125 if select(n
, ...) then _end
= n
; break; end
127 s(self
.data
, 1, results
, _end
, ...);
131 local function iter(self
, ...)
132 local query
= { ... };
133 local maxdepth
= select("#", ...);
134 local stack
= { self
.data
};
136 local function it(self
) -- luacheck: ignore 432/self
137 local depth
= #stack
;
138 local key
= next(stack
[depth
], keys
[depth
]);
139 if key
== nil then -- Go up the stack
140 stack
[depth
], keys
[depth
] = nil, nil;
148 local value
= stack
[depth
][key
];
149 if query
[depth
] == nil or key
== query
[depth
] then
150 if depth
== maxdepth
then -- Result
151 local result
= {}; -- Collect keys forming path to result
155 result
[depth
+1] = value
;
156 return unpack(result
, 1, depth
+1);
157 elseif type(value
) == "table" then
158 t_insert(stack
, value
); -- Descend
174 search_add
= search_add
;