Annotate

util/multitable.lua @ 7484:d962ccf90735

test_util_cache: remove unused argument c3 [luacheck]
author Anton Shestakov <av6@dwimlabs.net>
date Sat, 09 Jul 2016 17:30:23 +0800
parent 7185:5687788a2e4d
child 8382:e5d00bf4a4d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1104
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 4901
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 650
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 650
diff changeset
6 -- COPYING file in the source package for more information.
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local select = select;
4895
36df30395c44 util.multitable: No longer use table.remove to drop elements from the stack, when key is nil immediately (on empty tables) the previous stack entry's key gets removed instead
Matthew Wild <mwild1@gmail.com>
parents: 4894
diff changeset
10 local t_insert = table.insert;
7185
5687788a2e4d util.multitable: Localize unpack() in Lua 5.2 compatible way
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
11 local pairs, next, type = pairs, next, type;
5687788a2e4d util.multitable: Localize unpack() in Lua 5.2 compatible way
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
12 local unpack = table.unpack or unpack; --luacheck: ignore 113
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
14 local _ENV = nil;
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 local function get(self, ...)
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local t = self.data;
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 for n = 1,select('#', ...) do
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 t = t[select(n, ...)];
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 if not t then break; end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 return t;
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local function add(self, ...)
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 local t = self.data;
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 local count = select('#', ...);
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 for n = 1,count-1 do
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 local key = select(n, ...);
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 local tab = t[key];
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 if not tab then tab = {}; t[key] = tab; end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 t = tab;
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 end
568
b2464849c1b0 Small fix for multitable
Matthew Wild <mwild1@gmail.com>
parents: 554
diff changeset
34 t_insert(t, (select(count, ...)));
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36
650
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
37 local function set(self, ...)
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
38 local t = self.data;
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
39 local count = select('#', ...);
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
40 for n = 1,count-2 do
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
41 local key = select(n, ...);
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
42 local tab = t[key];
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
43 if not tab then tab = {}; t[key] = tab; end
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
44 t = tab;
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
45 end
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
46 t[(select(count-1, ...))] = (select(count, ...));
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
47 end
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
48
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 local function r(t, n, _end, ...)
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 if t == nil then return; end
650
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
51 local k = select(n, ...);
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
52 if n == _end then
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
53 t[k] = nil;
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
54 return;
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 if k then
1104
e9c1650054c4 util.multitable: No globals today, thank you. Fixes missing table entries and a potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
57 local v = t[k];
554
9a695724681d MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents: 552
diff changeset
58 if v then
9a695724681d MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents: 552
diff changeset
59 r(v, n+1, _end, ...);
9a695724681d MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents: 552
diff changeset
60 if not next(v) then
9a695724681d MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents: 552
diff changeset
61 t[k] = nil;
9a695724681d MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents: 552
diff changeset
62 end
9a695724681d MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents: 552
diff changeset
63 end
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 else
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 for _,b in pairs(t) do
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 r(b, n+1, _end, ...);
554
9a695724681d MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents: 552
diff changeset
67 if not next(b) then
9a695724681d MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents: 552
diff changeset
68 t[_] = nil;
9a695724681d MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents: 552
diff changeset
69 end
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 local function remove(self, ...)
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 local _end = select('#', ...);
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 for n = _end,1 do
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 if select(n, ...) then _end = n; break; end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 r(self.data, 1, _end, ...);
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82
840
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
83 local function s(t, n, results, _end, ...)
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
84 if t == nil then return; end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
85 local k = select(n, ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
86 if n == _end then
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
87 if k == nil then
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
88 for _, v in pairs(t) do
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
89 t_insert(results, v);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
90 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
91 else
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
92 t_insert(results, t[k]);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
93 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
94 return;
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
95 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
96 if k then
1104
e9c1650054c4 util.multitable: No globals today, thank you. Fixes missing table entries and a potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
97 local v = t[k];
840
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
98 if v then
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
99 s(v, n+1, results, _end, ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
100 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
101 else
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
102 for _,b in pairs(t) do
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
103 s(b, n+1, results, _end, ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
104 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
105 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
106 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
107
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
108 -- Search for keys, nil == wildcard
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
109 local function search(self, ...)
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
110 local _end = select('#', ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
111 for n = _end,1 do
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
112 if select(n, ...) then _end = n; break; end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
113 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
114 local results = {};
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
115 s(self.data, 1, results, _end, ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
116 return results;
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
117 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
118
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
119 -- Append results to an existing list
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
120 local function search_add(self, results, ...)
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
121 if not results then results = {}; end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
122 local _end = select('#', ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
123 for n = _end,1 do
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
124 if select(n, ...) then _end = n; break; end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
125 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
126 s(self.data, 1, results, _end, ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
127 return results;
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
128 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
129
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
130 local function iter(self, ...)
4894
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
131 local query = { ... };
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
132 local maxdepth = select("#", ...);
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
133 local stack = { self.data };
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
134 local keys = { };
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
135 local function it(self)
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
136 local depth = #stack;
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
137 local key = next(stack[depth], keys[depth]);
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
138 if key == nil then -- Go up the stack
4895
36df30395c44 util.multitable: No longer use table.remove to drop elements from the stack, when key is nil immediately (on empty tables) the previous stack entry's key gets removed instead
Matthew Wild <mwild1@gmail.com>
parents: 4894
diff changeset
139 stack[depth], keys[depth] = nil, nil;
4894
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
140 if depth > 1 then
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
141 return it(self);
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
142 end
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
143 return; -- The end
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
144 else
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
145 keys[depth] = key;
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
146 end
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
147 local value = stack[depth][key];
4901
05ea6c1ae393 util.multitable: Some fixes for iter()... always match against query, and pass value after path results
Matthew Wild <mwild1@gmail.com>
parents: 4895
diff changeset
148 if query[depth] == nil or key == query[depth] then
05ea6c1ae393 util.multitable: Some fixes for iter()... always match against query, and pass value after path results
Matthew Wild <mwild1@gmail.com>
parents: 4895
diff changeset
149 if depth == maxdepth then -- Result
05ea6c1ae393 util.multitable: Some fixes for iter()... always match against query, and pass value after path results
Matthew Wild <mwild1@gmail.com>
parents: 4895
diff changeset
150 local result = {}; -- Collect keys forming path to result
05ea6c1ae393 util.multitable: Some fixes for iter()... always match against query, and pass value after path results
Matthew Wild <mwild1@gmail.com>
parents: 4895
diff changeset
151 for i = 1, depth do
05ea6c1ae393 util.multitable: Some fixes for iter()... always match against query, and pass value after path results
Matthew Wild <mwild1@gmail.com>
parents: 4895
diff changeset
152 result[i] = keys[i];
05ea6c1ae393 util.multitable: Some fixes for iter()... always match against query, and pass value after path results
Matthew Wild <mwild1@gmail.com>
parents: 4895
diff changeset
153 end
05ea6c1ae393 util.multitable: Some fixes for iter()... always match against query, and pass value after path results
Matthew Wild <mwild1@gmail.com>
parents: 4895
diff changeset
154 result[depth+1] = value;
05ea6c1ae393 util.multitable: Some fixes for iter()... always match against query, and pass value after path results
Matthew Wild <mwild1@gmail.com>
parents: 4895
diff changeset
155 return unpack(result, 1, depth+1);
05ea6c1ae393 util.multitable: Some fixes for iter()... always match against query, and pass value after path results
Matthew Wild <mwild1@gmail.com>
parents: 4895
diff changeset
156 elseif type(value) == "table" then
05ea6c1ae393 util.multitable: Some fixes for iter()... always match against query, and pass value after path results
Matthew Wild <mwild1@gmail.com>
parents: 4895
diff changeset
157 t_insert(stack, value); -- Descend
4894
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
158 end
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
159 end
4901
05ea6c1ae393 util.multitable: Some fixes for iter()... always match against query, and pass value after path results
Matthew Wild <mwild1@gmail.com>
parents: 4895
diff changeset
160 return it(self);
4894
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
161 end;
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
162 return it, self;
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
163 end
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
164
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
165 local function new()
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 return {
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 data = {};
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 get = get;
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 add = add;
650
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
170 set = set;
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 remove = remove;
840
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
172 search = search;
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
173 search_add = search_add;
4894
c874dc4ccbd7 util.multitable: Add :iter() method to iterate over results at a fixed depth (parameters are equivalent to :search())
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
174 iter = iter;
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 };
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
178 return {
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
179 iter = iter;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
180 new = new;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
181 };