Skip to content

Commit

Permalink
Fix for Lua 5.2+ which removed table.maxn
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmreynolds committed Nov 20, 2015
1 parent f8a3fd2 commit a7a661d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions gmodule.lua
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion init.lua
Expand Up @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions utils.lua
Expand Up @@ -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

0 comments on commit a7a661d

Please sign in to comment.