Software /
code /
prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
550:56bdfae9e4ea | 552:6db1f41e475a |
---|---|
1 -- Prosody IM v0.1 | |
2 -- Copyright (C) 2008 Matthew Wild | |
3 -- Copyright (C) 2008 Waqas Hussain | |
4 -- | |
5 -- This program is free software; you can redistribute it and/or | |
6 -- modify it under the terms of the GNU General Public License | |
7 -- as published by the Free Software Foundation; either version 2 | |
8 -- of the License, or (at your option) any later version. | |
9 -- | |
10 -- This program is distributed in the hope that it will be useful, | |
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 -- GNU General Public License for more details. | |
14 -- | |
15 -- You should have received a copy of the GNU General Public License | |
16 -- along with this program; if not, write to the Free Software | |
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
18 -- | |
19 | |
20 | |
21 | |
22 local select = select; | |
23 local t_insert = table.insert; | |
24 local pairs = pairs; | |
25 | |
26 module "multitable" | |
27 | |
28 local function get(self, ...) | |
29 local t = self.data; | |
30 for n = 1,select('#', ...) do | |
31 t = t[select(n, ...)]; | |
32 if not t then break; end | |
33 end | |
34 return t; | |
35 end | |
36 | |
37 local function add(self, ...) | |
38 local t = self.data; | |
39 local count = select('#', ...); | |
40 for n = 1,count-1 do | |
41 local key = select(n, ...); | |
42 local tab = t[key]; | |
43 if not tab then tab = {}; t[key] = tab; end | |
44 t = tab; | |
45 end | |
46 t_insert(t, select(count, ...)); | |
47 end | |
48 | |
49 local function r(t, n, _end, ...) | |
50 if t == nil then return; end | |
51 if n > _end then | |
52 for key in pairs(t) do | |
53 t[key] = nil; | |
54 end | |
55 end | |
56 local k = select(n, ...); | |
57 if k then | |
58 r(t[k], n+1, _end, ...); | |
59 else | |
60 for _,b in pairs(t) do | |
61 r(b, n+1, _end, ...); | |
62 end | |
63 end | |
64 end | |
65 | |
66 local function remove(self, ...) | |
67 local _end = select('#', ...); | |
68 for n = _end,1 do | |
69 if select(n, ...) then _end = n; break; end | |
70 end | |
71 r(self.data, 1, _end, ...); | |
72 end | |
73 | |
74 | |
75 function new() | |
76 return { | |
77 data = {}; | |
78 get = get; | |
79 add = add; | |
80 remove = remove; | |
81 }; | |
82 end | |
83 | |
84 return _M; |