Software /
code /
prosody
Annotate
util/multitable.lua @ 6700:0103dc8fa179
mod_storage_xep0227: Return error from io.open if unable to open file for writing
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 15 May 2015 15:25:59 +0200 |
parent | 5776:bd0ff8ae98a8 |
child | 6777:5de6b93d0190 |
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 | 5 -- This project is MIT/X11 licensed. Please see the |
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; |
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
|
11 local unpack, pairs, next, type = unpack, pairs, next, type; |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 module "multitable" |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 local function get(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 local t = self.data; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 for n = 1,select('#', ...) do |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 t = t[select(n, ...)]; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 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
|
20 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 return t; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 local function add(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 local t = self.data; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 local count = select('#', ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 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
|
28 local key = select(n, ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 local tab = t[key]; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 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
|
31 t = tab; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 end |
568 | 33 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
|
34 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 |
650
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
36 local function set(self, ...) |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
37 local t = self.data; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
38 local count = select('#', ...); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
39 for n = 1,count-2 do |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
40 local key = select(n, ...); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
41 local tab = t[key]; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
42 if not tab then tab = {}; t[key] = tab; end |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
43 t = tab; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
44 end |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
45 t[(select(count-1, ...))] = (select(count, ...)); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
46 end |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
47 |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 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
|
49 if t == nil then return; end |
650
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
50 local k = select(n, ...); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
51 if n == _end then |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
52 t[k] = nil; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
53 return; |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 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
|
56 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
|
57 if v then |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
58 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
|
59 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
|
60 t[k] = nil; |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
61 end |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
62 end |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 else |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 t[_] = nil; |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
68 end |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 end |
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 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 local function remove(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 local _end = select('#', ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 for n = _end,1 do |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 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
|
77 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 r(self.data, 1, _end, ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 |
840
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
82 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
|
83 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
|
84 local k = select(n, ...); |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
85 if n == _end then |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
86 if k == nil then |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
87 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
|
88 t_insert(results, v); |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
89 end |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
90 else |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
91 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
|
92 end |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
93 return; |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
94 end |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
95 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
|
96 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
|
97 if v then |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
98 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
|
99 end |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
100 else |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
101 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
|
102 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
|
103 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 |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
107 -- 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
|
108 local function search(self, ...) |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
109 local _end = select('#', ...); |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
110 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
|
111 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
|
112 end |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
113 local results = {}; |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
114 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
|
115 return results; |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
116 end |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
117 |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
118 -- 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
|
119 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
|
120 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
|
121 local _end = select('#', ...); |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
122 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
|
123 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
|
124 end |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
125 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
|
126 return results; |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
127 end |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
128 |
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
|
129 function iter(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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 function new() |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 return { |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 data = {}; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 get = get; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 add = add; |
650
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
169 set = set; |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 remove = remove; |
840
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
171 search = search; |
ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents:
760
diff
changeset
|
172 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
|
173 iter = iter; |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 }; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
175 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
176 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
177 return _M; |