X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Fmultitable.lua;h=66b9bd8aba1a90481d80efcaa056773552a61f60;hb=bc1374cd5504f95c9b7a1b3c95950753d414b105;hp=e5e875216970b980baa0e2ecc02082b3adb0f4a4;hpb=a4d1701bd5e08cce7b221200394776a3171ddcac;p=prosody.git diff --git a/util/multitable.lua b/util/multitable.lua index e5e87521..66b9bd8a 100644 --- a/util/multitable.lua +++ b/util/multitable.lua @@ -1,6 +1,6 @@ --- Prosody IM v0.3 --- Copyright (C) 2008-2009 Matthew Wild --- Copyright (C) 2008-2009 Waqas Hussain +-- Prosody IM +-- Copyright (C) 2008-2010 Matthew Wild +-- Copyright (C) 2008-2010 Waqas Hussain -- -- This project is MIT/X11 licensed. Please see the -- COPYING file in the source package for more information. @@ -56,7 +56,7 @@ local function r(t, n, _end, ...) return; end if k then - v = t[k]; + local v = t[k]; if v then r(v, n+1, _end, ...); if not next(v) then @@ -82,6 +82,53 @@ local function remove(self, ...) end +local function s(t, n, results, _end, ...) + if t == nil then return; end + local k = select(n, ...); + if n == _end then + if k == nil then + for _, v in pairs(t) do + t_insert(results, v); + end + else + t_insert(results, t[k]); + end + return; + end + if k then + local v = t[k]; + if v then + s(v, n+1, results, _end, ...); + end + else + for _,b in pairs(t) do + s(b, n+1, results, _end, ...); + end + end +end + +-- Search for keys, nil == wildcard +local function search(self, ...) + local _end = select('#', ...); + for n = _end,1 do + if select(n, ...) then _end = n; break; end + end + local results = {}; + s(self.data, 1, results, _end, ...); + return results; +end + +-- Append results to an existing list +local function search_add(self, results, ...) + if not results then results = {}; end + local _end = select('#', ...); + for n = _end,1 do + if select(n, ...) then _end = n; break; end + end + s(self.data, 1, results, _end, ...); + return results; +end + function new() return { data = {}; @@ -89,6 +136,8 @@ function new() add = add; set = set; remove = remove; + search = search; + search_add = search_add; }; end