Mercurial > prosody-hg
comparison util/multitable.lua @ 840:ad842df925c7
util.multitable: Add mt:search(), use nil for wildcard keys
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 26 Feb 2009 16:55:46 +0000 |
| parents | 90ce865eebd8 |
| children | 2c0b9e3c11c3 |
comparison
equal
deleted
inserted
replaced
| 839:c45b5072f773 | 840:ad842df925c7 |
|---|---|
| 80 end | 80 end |
| 81 r(self.data, 1, _end, ...); | 81 r(self.data, 1, _end, ...); |
| 82 end | 82 end |
| 83 | 83 |
| 84 | 84 |
| 85 local function s(t, n, results, _end, ...) | |
| 86 if t == nil then return; end | |
| 87 local k = select(n, ...); | |
| 88 if n == _end then | |
| 89 if k == nil then | |
| 90 for _, v in pairs(t) do | |
| 91 t_insert(results, v); | |
| 92 end | |
| 93 else | |
| 94 t_insert(results, t[k]); | |
| 95 end | |
| 96 return; | |
| 97 end | |
| 98 if k then | |
| 99 v = t[k]; | |
| 100 if v then | |
| 101 s(v, n+1, results, _end, ...); | |
| 102 end | |
| 103 else | |
| 104 for _,b in pairs(t) do | |
| 105 s(b, n+1, results, _end, ...); | |
| 106 end | |
| 107 end | |
| 108 end | |
| 109 | |
| 110 -- Search for keys, nil == wildcard | |
| 111 local function search(self, ...) | |
| 112 local _end = select('#', ...); | |
| 113 for n = _end,1 do | |
| 114 if select(n, ...) then _end = n; break; end | |
| 115 end | |
| 116 local results = {}; | |
| 117 s(self.data, 1, results, _end, ...); | |
| 118 return results; | |
| 119 end | |
| 120 | |
| 121 -- Append results to an existing list | |
| 122 local function search_add(self, results, ...) | |
| 123 if not results then results = {}; end | |
| 124 local _end = select('#', ...); | |
| 125 for n = _end,1 do | |
| 126 if select(n, ...) then _end = n; break; end | |
| 127 end | |
| 128 s(self.data, 1, results, _end, ...); | |
| 129 return results; | |
| 130 end | |
| 131 | |
| 85 function new() | 132 function new() |
| 86 return { | 133 return { |
| 87 data = {}; | 134 data = {}; |
| 88 get = get; | 135 get = get; |
| 89 add = add; | 136 add = add; |
| 90 set = set; | 137 set = set; |
| 91 remove = remove; | 138 remove = remove; |
| 139 search = search; | |
| 140 search_add = search_add; | |
| 92 }; | 141 }; |
| 93 end | 142 end |
| 94 | 143 |
| 95 return _M; | 144 return _M; |
