Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Yonaba/Moses.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYonaba <roland.yonaba@gmail.com>2019-01-29 03:46:24 +0300
committerYonaba <roland.yonaba@gmail.com>2019-01-29 03:46:24 +0300
commite1fbe125870585f8bdccfddf8df9f5edb4734904 (patch)
treebaf45d22f176ed9d08418f81b89360c8a25af6ec /moses.lua
parentc59d157f102a8b8c0a387cb236dd6906a33db448 (diff)
Fixed powerset (Fixes #64)
Updated powerset spec Fixed spec
Diffstat (limited to 'moses.lua')
-rw-r--r--moses.lua16
1 files changed, 9 insertions, 7 deletions
diff --git a/moses.lua b/moses.lua
index d1039a4..88a2cf2 100644
--- a/moses.lua
+++ b/moses.lua
@@ -1578,20 +1578,22 @@ function M.rep(value, n)
end
--- Returns the powerset of array values. For instance, when given the set {1,2,3},
--- returns `{{1},{2},{3},{1,2},{2,3},{1,2,3}}`.
+-- returns `{{},{1},{2},{3},{1,2},{2,3},{1,3},{1,2,3}}`.
-- @name powerset
-- @param array an array
-- @return an array
function M.powerset(array)
local n = #array
- if n == 0 then return {{}} end
- local t = {}
- for l = 1, n do
- for s = 1, n-l+1 do
- t[#t+1] = M.slice(array,s,s+l-1)
+ local powerset = {}
+ for i, v in ipairs(array) do
+ for j = 1, #powerset do
+ local set = powerset[j]
+ t_insert(powerset, M.push(M.slice(set), v))
end
+ t_insert(powerset, {v})
end
- return t
+ t_insert(powerset, {})
+ return powerset
end
--- Iterator returning partitions of an array. It returns arrays of length `n`