2017-02-19 17:39:16 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
--- Math module for gears
|
|
|
|
--
|
2019-06-06 09:40:17 +02:00
|
|
|
-- @utillib gears.math
|
2017-02-19 17:39:16 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local rtable = table
|
|
|
|
|
|
|
|
local gmath = {}
|
|
|
|
|
|
|
|
local function subset_mask_apply(mask, set)
|
|
|
|
local ret = {}
|
|
|
|
for i = 1, #set do
|
|
|
|
if mask[i] then
|
|
|
|
rtable.insert(ret, set[i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
local function subset_next(mask)
|
|
|
|
local i = 1
|
|
|
|
while i <= #mask and mask[i] do
|
|
|
|
mask[i] = false
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if i <= #mask then
|
|
|
|
mask[i] = 1
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Return all subsets of a specific set.
|
|
|
|
-- This function, giving a set, will return all subset it.
|
|
|
|
-- For example, if we consider a set with value { 10, 15, 34 },
|
|
|
|
-- it will return a table containing 2^n set:
|
|
|
|
-- { }, { 10 }, { 15 }, { 34 }, { 10, 15 }, { 10, 34 }, etc.
|
|
|
|
-- @param set A set.
|
|
|
|
-- @return A table with all subset.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.math.subsets
|
2017-02-19 17:39:16 +01:00
|
|
|
function gmath.subsets(set)
|
|
|
|
local mask = {}
|
|
|
|
local ret = {}
|
|
|
|
for i = 1, #set do mask[i] = false end
|
|
|
|
|
|
|
|
-- Insert the empty one
|
|
|
|
rtable.insert(ret, {})
|
|
|
|
|
|
|
|
while subset_next(mask) do
|
|
|
|
rtable.insert(ret, subset_mask_apply(mask, set))
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Make i cycle.
|
|
|
|
-- @param t A length. Must be greater than zero.
|
|
|
|
-- @param i An absolute index to fit into #t.
|
|
|
|
-- @return An integer in (1, t) or nil if t is less than or equal to zero.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.math.cycle
|
2017-02-19 17:39:16 +01:00
|
|
|
function gmath.cycle(t, i)
|
|
|
|
if t < 1 then return end
|
|
|
|
i = i % t
|
|
|
|
if i == 0 then
|
|
|
|
i = t
|
|
|
|
end
|
|
|
|
return i
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Round a number to an integer.
|
|
|
|
-- @tparam number x
|
|
|
|
-- @treturn integer
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.math.round
|
2017-02-19 17:39:16 +01:00
|
|
|
function gmath.round(x)
|
|
|
|
return math.floor(x + 0.5)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return gmath
|