Software / code / prosody
Comparison
util/multitable.lua @ 650:674531bcb5c4
Added util.multitable.set
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Sat, 27 Dec 2008 08:27:26 +0500 |
| parent | 615:4ae3e81513f3 |
| child | 758:b1885732e979 |
comparison
equal
deleted
inserted
replaced
| 649:5f8382768530 | 650:674531bcb5c4 |
|---|---|
| 45 t = tab; | 45 t = tab; |
| 46 end | 46 end |
| 47 t_insert(t, (select(count, ...))); | 47 t_insert(t, (select(count, ...))); |
| 48 end | 48 end |
| 49 | 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 | |
| 50 local function r(t, n, _end, ...) | 62 local function r(t, n, _end, ...) |
| 51 if t == nil then return; end | 63 if t == nil then return; end |
| 52 if n > _end then | 64 local k = select(n, ...); |
| 53 for key in pairs(t) do | 65 if n == _end then |
| 54 t[key] = nil; | 66 t[k] = nil; |
| 55 end | 67 return; |
| 56 end | 68 end |
| 57 local k = select(n, ...); | |
| 58 if k then | 69 if k then |
| 59 v = t[k]; | 70 v = t[k]; |
| 60 if v then | 71 if v then |
| 61 r(v, n+1, _end, ...); | 72 r(v, n+1, _end, ...); |
| 62 if not next(v) then | 73 if not next(v) then |
| 85 function new() | 96 function new() |
| 86 return { | 97 return { |
| 87 data = {}; | 98 data = {}; |
| 88 get = get; | 99 get = get; |
| 89 add = add; | 100 add = add; |
| 101 set = set; | |
| 90 remove = remove; | 102 remove = remove; |
| 91 }; | 103 }; |
| 92 end | 104 end |
| 93 | 105 |
| 94 return _M; | 106 return _M; |