diff options
author | Malcolm Reynolds <mareynolds@google.com> | 2015-11-20 19:38:55 +0300 |
---|---|---|
committer | Malcolm Reynolds <mareynolds@google.com> | 2015-11-20 19:38:55 +0300 |
commit | a7a661df1ddecc620a2e6a53de384b5e8995896a (patch) | |
tree | 9829cd593df3d301b4616920aa9d67bc15a6051b | |
parent | f8a3fd2b7b9e413f2e7e0524ff29f2f89e9e2419 (diff) |
Fix for Lua 5.2+ which removed table.maxn
-rw-r--r-- | gmodule.lua | 4 | ||||
-rw-r--r-- | init.lua | 2 | ||||
-rw-r--r-- | utils.lua | 14 |
3 files changed, 17 insertions, 3 deletions
diff --git a/gmodule.lua b/gmodule.lua index 65cf047..99698c0 100644 --- a/gmodule.lua +++ b/gmodule.lua @@ -49,14 +49,14 @@ function gModule:__init(inputs,outputs) -- input point for the backward graph local node local outnode = nngraph.Node({input={}}) - for i = 1, table.maxn(outputs) do + for i = 1, utils.tableMaxN(outputs) do node = outputs[i] if torch.typename(node) ~= 'nngraph.Node' then error(utils.expectingNodeErrorMessage(node, 'outputs', i)) end outnode:add(node, true) end - for i = 1, table.maxn(inputs) do + for i = 1, utils.tableMaxN(inputs) do node = inputs[i] if torch.typename(node) ~= 'nngraph.Node' then error(utils.expectingNodeErrorMessage(node, 'inputs', i)) @@ -41,7 +41,7 @@ function Module:__call__(...) local mnode = nngraph.Node({module=self}) local dnode - for i = 1, table.maxn(input) do + for i = 1, utils.tableMaxN(input) do dnode = input[i] if torch.typename(dnode) ~= 'nngraph.Node' then error(utils.expectingNodeErrorMessage(dnode, 'inputs', i)) @@ -25,4 +25,18 @@ function utils.expectingNodeErrorMessage(badVal, array, idx) end end +--[[ Lua 5.2+ removed table.maxn, provide fallback implementation. ]] +if table.maxn then + utils.tableMaxN = table.maxn +else + function utils.tableMaxN(tbl) + local max = 0 + for k, v in pairs(tbl) do + if type(k) == 'number' and k > max then + max = k + end + end + return max + end + end return utils |