Added util.multitable.set
[prosody.git] / util / multitable.lua
1 -- Prosody IM v0.2
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 local next = next;
26
27 module "multitable"
28
29 local function get(self, ...)
30         local t = self.data;
31         for n = 1,select('#', ...) do
32                 t = t[select(n, ...)];
33                 if not t then break; end
34         end
35         return t;
36 end
37
38 local function add(self, ...)
39         local t = self.data;
40         local count = select('#', ...);
41         for n = 1,count-1 do
42                 local key = select(n, ...);
43                 local tab = t[key];
44                 if not tab then tab = {}; t[key] = tab; end
45                 t = tab;
46         end
47         t_insert(t, (select(count, ...)));
48 end
49
50 local function set(self, ...)
51         local t = self.data;
52         local count = select('#', ...);
53         for n = 1,count-2 do
54                 local key = select(n, ...);
55                 local tab = t[key];
56                 if not tab then tab = {}; t[key] = tab; end
57                 t = tab;
58         end
59         t[(select(count-1, ...))] = (select(count, ...));
60 end
61
62 local function r(t, n, _end, ...)
63         if t == nil then return; end
64         local k = select(n, ...);
65         if n == _end then
66                 t[k] = nil;
67                 return;
68         end
69         if k then
70                 v = t[k];
71                 if v then
72                         r(v, n+1, _end, ...);
73                         if not next(v) then
74                                 t[k] = nil;
75                         end
76                 end
77         else
78                 for _,b in pairs(t) do
79                         r(b, n+1, _end, ...);
80                         if not next(b) then
81                                 t[_] = nil;
82                         end
83                 end
84         end
85 end
86
87 local function remove(self, ...)
88         local _end = select('#', ...);
89         for n = _end,1 do
90                 if select(n, ...) then _end = n; break; end
91         end
92         r(self.data, 1, _end, ...);
93 end
94
95
96 function new()
97         return {
98                 data = {};
99                 get = get;
100                 add = add;
101                 set = set;
102                 remove = remove;
103         };
104 end
105
106 return _M;