Thu, 29 Jan 2009 02:13:30 +0000
configmanager: Add support for defining components
615 | 1 | -- Prosody IM v0.2 |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 | -- Copyright (C) 2008 Matthew Wild |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 | -- Copyright (C) 2008 Waqas Hussain |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 | -- |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 | -- This program is free software; you can redistribute it and/or |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 | -- modify it under the terms of the GNU General Public License |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 | -- as published by the Free Software Foundation; either version 2 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 | -- of the License, or (at your option) any later version. |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 | -- |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 | -- This program is distributed in the hope that it will be useful, |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 | -- GNU General Public License for more details. |
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 | -- You should have received a copy of the GNU General Public License |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 | -- along with this program; if not, write to the Free Software |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 | -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 | -- |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 | |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 | |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 | |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 | local select = select; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 | local t_insert = table.insert; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 | local pairs = pairs; |
554
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
25 | local next = next; |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 | |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 | module "multitable" |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 | |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 | local function get(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 | local t = self.data; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 | for n = 1,select('#', ...) do |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 | t = t[select(n, ...)]; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 | 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
|
34 | end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 | return t; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 | end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 | |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 | local function add(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 | local t = self.data; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 | local count = select('#', ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 | 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
|
42 | local key = select(n, ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 | local tab = t[key]; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 | 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
|
45 | t = tab; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 | end |
568 | 47 | 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
|
48 | end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 | |
650
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
50 | local function set(self, ...) |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
51 | local t = self.data; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
52 | local count = select('#', ...); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
53 | for n = 1,count-2 do |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
54 | local key = select(n, ...); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
55 | local tab = t[key]; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
56 | if not tab then tab = {}; t[key] = tab; end |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
57 | t = tab; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
58 | end |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
59 | t[(select(count-1, ...))] = (select(count, ...)); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
60 | end |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
61 | |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 | 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
|
63 | if t == nil then return; end |
650
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
64 | local k = select(n, ...); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
65 | if n == _end then |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
66 | t[k] = nil; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
67 | return; |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 | end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 | if k then |
554
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
70 | v = t[k]; |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
71 | if v then |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
72 | 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
|
73 | 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
|
74 | t[k] = nil; |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
75 | end |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
76 | end |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 | else |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 | 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
|
79 | 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
|
80 | 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
|
81 | t[_] = nil; |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
82 | end |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 | end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 | end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 | end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 | |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 | local function remove(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 | local _end = select('#', ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 | for n = _end,1 do |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 | 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
|
91 | end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 | r(self.data, 1, _end, ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 | end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 | |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 | |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 | function new() |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 | return { |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 | data = {}; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 | get = get; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 | add = add; |
650
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
101 | set = set; |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 | remove = remove; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 | }; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 | end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 | |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 | return _M; |