Software /
code /
prosody
Annotate
util/multitable.lua @ 568:b2464849c1b0
Small fix for multitable
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 06 Dec 2008 03:40:51 +0000 |
parent | 554:9a695724681d |
child | 615:4ae3e81513f3 |
rev | line source |
---|---|
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 -- Prosody IM v0.1 |
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 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 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
|
51 if t == nil then return; end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 if n > _end then |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 for key in pairs(t) do |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 t[key] = nil; |
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 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 local k = select(n, ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 if k then |
554
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
59 v = t[k]; |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
60 if v then |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
61 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
|
62 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
|
63 t[k] = nil; |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
64 end |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
65 end |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 else |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 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
|
68 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
|
69 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
|
70 t[_] = nil; |
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
71 end |
552
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 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 local function remove(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 local _end = select('#', ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 for n = _end,1 do |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 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
|
80 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 r(self.data, 1, _end, ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 function new() |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 return { |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 data = {}; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 get = get; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 add = add; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 remove = remove; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 }; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 return _M; |