doc: revisit gears.table
This commit is contained in:
parent
6d2fc5c12b
commit
25fca23772
|
@ -10,12 +10,12 @@ local rtable = table
|
||||||
local gmath = require("gears.math")
|
local gmath = require("gears.math")
|
||||||
local gtable = {}
|
local gtable = {}
|
||||||
|
|
||||||
--- Join all tables given as parameters.
|
--- Join all tables given as arguments.
|
||||||
-- This will iterate all tables and insert all their keys into a new table.
|
-- This will iterate over all tables and insert their entries into a new table.
|
||||||
-- @class function
|
-- @class function
|
||||||
-- @name join
|
-- @name join
|
||||||
-- @param args A list of tables to join
|
-- @tparam table ... Tables to join.
|
||||||
-- @return A new table containing all keys from the arguments.
|
-- @treturn table A new table containing all entries from the arguments.
|
||||||
function gtable.join(...)
|
function gtable.join(...)
|
||||||
local ret = {}
|
local ret = {}
|
||||||
for i = 1, select("#", ...) do
|
for i = 1, select("#", ...) do
|
||||||
|
@ -40,7 +40,7 @@ end
|
||||||
-- @name crush
|
-- @name crush
|
||||||
-- @tparam table t the table to be overriden
|
-- @tparam table t the table to be overriden
|
||||||
-- @tparam table set the table used to override members of `t`
|
-- @tparam table set the table used to override members of `t`
|
||||||
-- @tparam[opt=false] boolean raw Use rawset (avoid the metatable)
|
-- @tparam[opt=false] bool raw Use rawset (avoid the metatable)
|
||||||
-- @treturn table t (for convenience)
|
-- @treturn table t (for convenience)
|
||||||
function gtable.crush(t, set, raw)
|
function gtable.crush(t, set, raw)
|
||||||
if raw then
|
if raw then
|
||||||
|
@ -56,17 +56,17 @@ function gtable.crush(t, set, raw)
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Pack all elements with an integer key into a new table
|
--- Pack all elements with an integer key into a new table.
|
||||||
-- While both lua and luajit implement __len over sparse
|
-- While both lua and luajit implement __len over sparse
|
||||||
-- table, the standard define it as an implementation
|
-- tables, the standard defines it as an implementation
|
||||||
-- detail.
|
-- detail.
|
||||||
--
|
--
|
||||||
-- This function remove any non numeric keys from the value set
|
-- This function removes any entries with non-numeric keys.
|
||||||
--
|
--
|
||||||
-- @class function
|
-- @class function
|
||||||
-- @name from_sparse
|
-- @name from_sparse
|
||||||
-- @tparam table t A potentially sparse table
|
-- @tparam table t A potentially sparse table.
|
||||||
-- @treturn table A packed table with all numeric keys
|
-- @treturn table A packed table with only numeric keys.
|
||||||
function gtable.from_sparse(t)
|
function gtable.from_sparse(t)
|
||||||
local keys= {}
|
local keys= {}
|
||||||
for k in pairs(t) do
|
for k in pairs(t) do
|
||||||
|
@ -88,9 +88,10 @@ end
|
||||||
--- Check if a table has an item and return its key.
|
--- Check if a table has an item and return its key.
|
||||||
-- @class function
|
-- @class function
|
||||||
-- @name hasitem
|
-- @name hasitem
|
||||||
-- @param t The table.
|
-- @tparam table t The table.
|
||||||
-- @param item The item to look for in values of the table.
|
-- @param item The item to look for in values of the table.
|
||||||
-- @return The key were the item is found, or nil if not found.
|
-- @treturn[1] string|number The key of the item.
|
||||||
|
-- @treturn[2] nil
|
||||||
function gtable.hasitem(t, item)
|
function gtable.hasitem(t, item)
|
||||||
for k, v in pairs(t) do
|
for k, v in pairs(t) do
|
||||||
if v == item then
|
if v == item then
|
||||||
|
@ -99,11 +100,11 @@ function gtable.hasitem(t, item)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Get a sorted table with all integer keys from a table
|
--- Get a sorted table with all integer keys from a table.
|
||||||
-- @class function
|
-- @class function
|
||||||
-- @name keys
|
-- @name keys
|
||||||
-- @param t the table for which the keys to get
|
-- @tparam table t The table for which the keys to get.
|
||||||
-- @return A table with keys
|
-- @treturn table A table with keys
|
||||||
function gtable.keys(t)
|
function gtable.keys(t)
|
||||||
local keys = { }
|
local keys = { }
|
||||||
for k, _ in pairs(t) do
|
for k, _ in pairs(t) do
|
||||||
|
@ -115,12 +116,12 @@ function gtable.keys(t)
|
||||||
return keys
|
return keys
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Filter a tables keys for certain content types
|
--- Filter a table's keys for certain content type.
|
||||||
-- @class function
|
-- @class function
|
||||||
-- @name keys_filter
|
-- @name keys_filter
|
||||||
-- @param t The table to retrieve the keys for
|
-- @tparam table t The table to retrieve the keys for.
|
||||||
-- @param ... the types to look for
|
-- @tparam string ... The types to look for.
|
||||||
-- @return A filtered table with keys
|
-- @treturn table A filtered table.
|
||||||
function gtable.keys_filter(t, ...)
|
function gtable.keys_filter(t, ...)
|
||||||
local keys = gtable.keys(t)
|
local keys = gtable.keys(t)
|
||||||
local keys_filtered = { }
|
local keys_filtered = { }
|
||||||
|
@ -135,18 +136,18 @@ function gtable.keys_filter(t, ...)
|
||||||
return keys_filtered
|
return keys_filtered
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Reverse a table
|
--- Reverse a table.
|
||||||
-- @class function
|
-- @class function
|
||||||
-- @name reverse
|
-- @name reverse
|
||||||
-- @param t the table to reverse
|
-- @tparam table t The table to reverse.
|
||||||
-- @return the reversed table
|
-- @treturn table A reversed table.
|
||||||
function gtable.reverse(t)
|
function gtable.reverse(t)
|
||||||
local tr = { }
|
local tr = { }
|
||||||
-- reverse all elements with integer keys
|
-- Reverse all elements with integer keys.
|
||||||
for _, v in ipairs(t) do
|
for _, v in ipairs(t) do
|
||||||
rtable.insert(tr, 1, v)
|
rtable.insert(tr, 1, v)
|
||||||
end
|
end
|
||||||
-- add the remaining elements
|
-- Add the remaining elements.
|
||||||
for k, v in pairs(t) do
|
for k, v in pairs(t) do
|
||||||
if type(k) ~= "number" then
|
if type(k) ~= "number" then
|
||||||
tr[k] = v
|
tr[k] = v
|
||||||
|
@ -155,12 +156,12 @@ function gtable.reverse(t)
|
||||||
return tr
|
return tr
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Clone a table
|
--- Clone a table.
|
||||||
-- @class function
|
-- @class function
|
||||||
-- @name clone
|
-- @name clone
|
||||||
-- @param t the table to clone
|
-- @tparam table t The table to clone.
|
||||||
-- @param deep Create a deep clone? (default: true)
|
-- @tparam[opt=true] bool deep Create a deep clone?
|
||||||
-- @return a clone of t
|
-- @treturn table A clone of `t`.
|
||||||
function gtable.clone(t, deep)
|
function gtable.clone(t, deep)
|
||||||
deep = deep == nil and true or deep
|
deep = deep == nil and true or deep
|
||||||
local c = { }
|
local c = { }
|
||||||
|
@ -174,9 +175,9 @@ function gtable.clone(t, deep)
|
||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
|
||||||
---
|
--- Iterate over a table.
|
||||||
-- Returns an iterator to cycle through, starting from the first element or the
|
-- Returns an iterator to cycle through all elements of a table that match a
|
||||||
-- given index, all elements of a table that match a given criteria.
|
-- given criteria, starting from the first element or the given index.
|
||||||
--
|
--
|
||||||
-- @class function
|
-- @class function
|
||||||
-- @name iterate
|
-- @name iterate
|
||||||
|
@ -202,13 +203,12 @@ function gtable.iterate(t, filter, start)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Merge items from one table to another one.
|
||||||
--- Merge items from the one table to another one
|
|
||||||
-- @class function
|
-- @class function
|
||||||
-- @name merge
|
-- @name merge
|
||||||
-- @tparam table t the container table
|
-- @tparam table t The container table
|
||||||
-- @tparam table set the mixin table
|
-- @tparam table set The mixin table.
|
||||||
-- @treturn table Return `t` for convenience
|
-- @treturn table (for convenience)
|
||||||
function gtable.merge(t, set)
|
function gtable.merge(t, set)
|
||||||
for _, v in ipairs(set) do
|
for _, v in ipairs(set) do
|
||||||
table.insert(t, v)
|
table.insert(t, v)
|
||||||
|
@ -216,13 +216,14 @@ function gtable.merge(t, set)
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Map a function to a table. The function is applied to each value
|
--- Map a function to a table.
|
||||||
-- on the table, returning a table of the results.
|
-- The function is applied to each value on the table, returning a modified
|
||||||
|
-- table.
|
||||||
-- @class function
|
-- @class function
|
||||||
-- @name map
|
-- @name map
|
||||||
-- @tparam function f the function to be applied to each value on the table
|
-- @tparam function f The function to be applied to each value in the table.
|
||||||
-- @tparam table tbl the container table whose values will be operated on
|
-- @tparam table tbl The container table whose values will be operated on.
|
||||||
-- @treturn table Return a table of the results
|
-- @treturn table
|
||||||
function gtable.map(f, tbl)
|
function gtable.map(f, tbl)
|
||||||
local t = {}
|
local t = {}
|
||||||
for k,v in pairs(tbl) do
|
for k,v in pairs(tbl) do
|
||||||
|
@ -231,5 +232,4 @@ function gtable.map(f, tbl)
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return gtable
|
return gtable
|
||||||
|
|
Loading…
Reference in New Issue