Software /
code /
prosody
Annotate
util/multitable.lua @ 552:6db1f41e475a
util.multitable - A table with multiple key for each entry
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 05 Dec 2008 04:56:04 +0500 |
child | 554:9a695724681d |
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; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 module "multitable" |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 local function get(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 local t = self.data; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 for n = 1,select('#', ...) do |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 t = t[select(n, ...)]; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 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
|
33 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 return t; |
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 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 local function add(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 local t = self.data; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 local count = select('#', ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 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
|
41 local key = select(n, ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 local tab = t[key]; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 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
|
44 t = tab; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 t_insert(t, select(count, ...)); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 |
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 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 if n > _end then |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 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
|
53 t[key] = nil; |
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 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 local k = select(n, ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 if k then |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 r(t[k], n+1, _end, ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 else |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 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
|
61 r(b, n+1, _end, ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 local function remove(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 local _end = select('#', ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 for n = _end,1 do |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 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
|
70 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 r(self.data, 1, _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 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 function new() |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 return { |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 data = {}; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 get = get; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 add = add; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 remove = remove; |
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 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 return _M; |