Annotate

util/multitable.lua @ 4071:8739553aae3b

net.httpserver: Changed an unnecessary global access.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 05 Jan 2011 06:26:49 +0500
parent 2923:b7049746bd29
child 4894:c874dc4ccbd7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1104
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 650
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 650
diff changeset
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
b2464849c1b0 Small fix for multitable
Matthew Wild <mwild1@gmail.com>
parents: 554
diff changeset
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
1104
e9c1650054c4 util.multitable: No globals today, thank you. Fixes missing table entries and a potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
59 local v = t[k];
554
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
840
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
85 local function s(t, n, results, _end, ...)
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
86 if t == nil then return; end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
87 local k = select(n, ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
88 if n == _end then
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
89 if k == nil then
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
90 for _, v in pairs(t) do
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
91 t_insert(results, v);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
92 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
93 else
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
94 t_insert(results, t[k]);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
95 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
96 return;
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
97 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
98 if k then
1104
e9c1650054c4 util.multitable: No globals today, thank you. Fixes missing table entries and a potential traceback
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
99 local v = t[k];
840
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
100 if v then
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
101 s(v, n+1, results, _end, ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
102 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
103 else
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
104 for _,b in pairs(t) do
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
105 s(b, n+1, results, _end, ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
106 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
107 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
108 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
109
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
110 -- Search for keys, nil == wildcard
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
111 local function search(self, ...)
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
112 local _end = select('#', ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
113 for n = _end,1 do
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
114 if select(n, ...) then _end = n; break; end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
115 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
116 local results = {};
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
117 s(self.data, 1, results, _end, ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
118 return results;
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
119 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
120
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
121 -- Append results to an existing list
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
122 local function search_add(self, results, ...)
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
123 if not results then results = {}; end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
124 local _end = select('#', ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
125 for n = _end,1 do
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
126 if select(n, ...) then _end = n; break; end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
127 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
128 s(self.data, 1, results, _end, ...);
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
129 return results;
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
130 end
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
131
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 function new()
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 return {
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 data = {};
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 get = get;
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 add = add;
650
674531bcb5c4 Added util.multitable.set
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
137 set = set;
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 remove = remove;
840
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
139 search = search;
ad842df925c7 util.multitable: Add mt:search(), use nil for wildcard keys
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
140 search_add = search_add;
552
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 };
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 end
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143
6db1f41e475a util.multitable - A table with multiple key for each entry
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 return _M;