diff options
author | Philipp Gesang <phg42.2a@gmail.com> | 2014-02-02 01:10:08 -0800 |
---|---|---|
committer | Philipp Gesang <phg42.2a@gmail.com> | 2014-02-02 01:10:08 -0800 |
commit | d758aba692b362c8653e1e4b1b5c13df4346652b (patch) | |
tree | 85352d4c1c48f6df474ff9f330cf7207f726d1a5 /lualibs-table.lua | |
parent | 954d59d0e7c66cbeb72c9d323706dafdd7ac3a5b (diff) | |
parent | 43ffeea64e686e25807959575d07162bc8bb02b6 (diff) | |
download | lualibs-d758aba692b362c8653e1e4b1b5c13df4346652b.tar.gz |
Merge pull request #23 from phi-gamma/master
sync with Context
Diffstat (limited to 'lualibs-table.lua')
-rw-r--r-- | lualibs-table.lua | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/lualibs-table.lua b/lualibs-table.lua index 11cb66b..c318c57 100644 --- a/lualibs-table.lua +++ b/lualibs-table.lua @@ -129,10 +129,13 @@ local function sortedhash(t,cmp) s = sortedkeys(t) -- the robust one end local n = 0 + local m = #s local function kv(s) - n = n + 1 - local k = s[n] - return k, t[k] + if n < m then + n = n + 1 + local k = s[n] + return k, t[k] + end end return kv, s else @@ -1003,7 +1006,9 @@ function table.print(t,...) end end -setinspector(function(v) if type(v) == "table" then serialize(print,v,"table") return true end end) +if setinspector then + setinspector(function(v) if type(v) == "table" then serialize(print,v,"table") return true end end) +end -- -- -- obsolete but we keep them for a while and might comment them later -- -- -- @@ -1054,3 +1059,24 @@ function table.sorted(t,...) sort(t,...) return t -- still sorts in-place end + +-- + +function table.values(t,s) -- optional sort flag + if t then + local values, keys, v = { }, { }, 0 + for key, value in next, t do + if not keys[value] then + v = v + 1 + values[v] = value + keys[k] = key + end + end + if s then + sort(values) + end + return values + else + return { } + end +end |