Software /
code /
prosody
Annotate
util/multitable.lua @ 827:e74045238ee3
MUC: Throw an error if we try talking to ourselves
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 20 Feb 2009 00:51:33 +0500 |
parent | 760:90ce865eebd8 |
child | 840:ad842df925c7 |
rev | line source |
---|---|
759 | 1 -- Prosody IM v0.3 |
760
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
2 -- Copyright (C) 2008-2009 Matthew Wild |
90ce865eebd8
Update copyright notices for 2009
Matthew Wild <mwild1@gmail.com>
parents:
759
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 -- |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 |
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 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local select = select; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 local t_insert = table.insert; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 local pairs = pairs; |
554
9a695724681d
MultiTable: Remove all empty sub-tables when elements are removed
Waqas Hussain <waqas20@gmail.com>
parents:
552
diff
changeset
|
14 local next = next; |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 module "multitable" |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 local function get(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 local t = self.data; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 for n = 1,select('#', ...) do |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 t = t[select(n, ...)]; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 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
|
23 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 return t; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 end |
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 local function add(self, ...) |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 local t = self.data; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 local count = select('#', ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 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
|
31 local key = select(n, ...); |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 local tab = t[key]; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 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
|
34 t = tab; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 end |
568 | 36 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
|
37 end |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 |
650
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
39 local function set(self, ...) |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
40 local t = self.data; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
41 local count = select('#', ...); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
42 for n = 1,count-2 do |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
43 local key = select(n, ...); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
44 local tab = t[key]; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
45 if not tab then tab = {}; t[key] = tab; end |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
46 t = tab; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
47 end |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
48 t[(select(count-1, ...))] = (select(count, ...)); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
49 end |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
50 |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 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
|
52 if t == nil then return; end |
650
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
53 local k = select(n, ...); |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
54 if n == _end then |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
55 t[k] = nil; |
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
56 return; |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 end |
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; |
650
674531bcb5c4
Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents:
615
diff
changeset
|
90 set = set; |
552
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 remove = remove; |
6db1f41e475a
util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 }; |
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 return _M; |