Module utils.table
Table utils.
Type utils.table
utils.table.cache(store, timeout) |
Cached table Provide a RAM cache table. |
utils.table.cacheflush(cache) |
Flush a cached table |
utils.table.copy(src, dst, overwrite) |
Copies a table from the source to destination table. |
utils.table.diff(t1, t2, norecurse) |
Produces a diff of two tables. |
utils.table.isArray(T) |
Checks if the table is regular array. |
utils.table.keys(T) |
Extracts the keys of a table into a list. |
utils.table.map(T, func, recursive, inplace) |
Executes a function on each element of a table. |
utils.table.multiPairs(t1, ...) |
Iterator that returns key/value on multiple tables. |
utils.table.pack(varargs) |
Packs a variable number of arguments into a table. |
utils.table.recursivePairs(t, prefix) |
Iterator that returns key/value pairs except walking through sub tables and concatenating the path in the key. |
utils.table.sortedPairs(t) |
Iterator that iterates on the table in key ascending order. |
Type utils.table
Field(s)
- utils.table.cache(store, timeout)
-
Cached table
Provide a RAM cache table.
Write changes are propagated to the store table after the specified timeouts.
Note: this require sched module to be enabled (i.e. require 'sched' before calling this function)
Note: tables values are written as is (i.e. not cloned) into the cache.
Note: only writting to the first level of the cached table will trigger a timed cache write. Writting to sub tables will not cause the cache timer write to be rearmed.
Parameters
-
store
: table where the change are going to be written -
timeout
: is the maximum time the cache takes to write back (it can be less if there are several accesses)
Return value
the cached table
-
- utils.table.cacheflush(cache)
-
Flush a cached table
Parameter
-
cache
: cached table
Return value
"ok" on success, nil, error otherwise
-
- utils.table.copy(src, dst, overwrite)
-
Copies a table from the source to destination table.
The copy is recursive.
Parameters
-
src
: a table to be used as the source of the copy. -
dst
: a table to be used as the destination of the copy. -
overwrite
: boolean value, to enable overwrite of existing field in dst table.
Return value
the resulting dst table.
-
- utils.table.diff(t1, t2, norecurse)
-
Produces a diff of two tables.
Recursive diff by default.
Parameters
-
t1
: first table to compare. -
t2
: second table to compare. -
norecurse
: boolean value to disable recursive diff.
Return value
the diff result as a table.
Usage:
> t1 = { a = 1, b=3, c = "foo"} > t2 = { a = 1, b=3, c = "foo"} > :diff(t1, t2) = { } > t1 = { a = 1, b=3, c = "foo"} > t2 = { a = 1, b=4, c = "foo"} > :diff(t1, t2) = { "b" } > t1 = { a = 1, b=3, c = "foo"} > t2 = { a = 1, b=3} > :diff(t1, t2) = { "c" } > t1 = { b=3, c = "foo", d="bar"} > t2 = { a = 1, b=3, c = "foo"} > :diff(t1, t2) = { "a", "d" }
-
- utils.table.isArray(T)
-
Checks if the table is regular array.
Regular array is a table with only conscutive integer keys (no holes).
Parameter
-
T
: table to check.
Return value
true or false.
-
- utils.table.keys(T)
-
Extracts the keys of a table into a list.
Parameter
-
T
: the table to extract keys from.
Return value
a list of keys of T table.
-
- utils.table.map(T, func, recursive, inplace)
-
Executes a function on each element of a table.
If recursive is non false the map function will be called recursively.
If inplace is set to true, then the table is modified in place.
The function is called with key and value as a parameter.
Parameters
-
T
: the table to apply map on. -
func
: the function to call for each value of the map. -
recursive
: if non false, the map is executed recusively. -
inplace
: if non false, the table is modified in place.
Return value
the table with each value mapped by the function
Usage:
local data = { key1 = "val1", key2 = "val2"} local function foo(key, value) print("foo:" key, value) end map(data, foo) -> will print: foo: key1 val1 foo: key2 val2
-
- utils.table.multiPairs(t1, ...)
-
Iterator that returns key/value on multiple tables.
Parameters
-
t1
: first table to traverse. -
...
: additional tables to traverse.
Return value
iterator function.
-
- utils.table.pack(varargs)
-
Packs a variable number of arguments into a table.
This is a temporary function until we switch to Lua 5.2.
Returns a new table with all parameters stored into keys 1, 2, etc. and with a field "n" with the total number of parameters. Also returns, as a second result, the total number of parameters.
Note that the resulting table may not be a sequence.
Note that when loading utils.table module, this pack function is also added to global/std table module.
Parameter
-
varargs
: arguments to pack
Return value
a table that contains all the arguments followed by the number of arguments.
-
- utils.table.recursivePairs(t, prefix)
-
Iterator that returns key/value pairs except walking through sub tables and concatenating the path in the key.
This iterator break cycles: it does not recur. If it detects a cycle, the entry causing a cycle is ignored. However, if a table is repeated, its contents will be repeated by this function.
Parameters
-
t
: table to iterate. -
prefix
: path prefix to prepend to the key path returned.
Return value
iterator function.
Usage:
{toto={titi=1, tutu=2}, tata = 3, tonton={4, 5}} will iterate through ("toto.titi",1), ("toto.tutu",2), ("tata",3) ("tonton.1", 4), ("tonton.2"=5)
-
- utils.table.sortedPairs(t)
-
Iterator that iterates on the table in key ascending order.
Parameter
-
t
: table to iterate.
Return value
iterator function.
-