1
0
mirror of https://github.com/kepler155c/opus synced 2024-12-29 10:00:26 +00:00
opus/sys/apis/jumper/core/utils.lua

58 lines
1.1 KiB
Lua
Raw Normal View History

2016-12-11 19:24:52 +00:00
-- Various utilities for Jumper top-level modules
if (...) then
-- Dependencies
local _PATH = (...):gsub('%.utils$','')
local Path = require (_PATH .. '.path')
-- Local references
local pairs = pairs
local t_insert = table.insert
-- Raw array items count
local function arraySize(t)
local count = 0
2017-10-26 22:56:55 +00:00
for _ in pairs(t) do
2016-12-11 19:24:52 +00:00
count = count+1
end
return count
end
-- Extract a path from a given start/end position
local function traceBackPath(finder, node, startNode)
local path = Path:new()
path._grid = finder._grid
while true do
if node._parent then
t_insert(path._nodes,1,node)
node = node._parent
else
t_insert(path._nodes,1,startNode)
return path
end
end
end
-- Lookup for value in a table
local indexOf = function(t,v)
for i = 1,#t do
if t[i] == v then return i end
end
return nil
end
-- Is i out of range
local function outOfRange(i,low,up)
return (i< low or i > up)
end
2017-10-26 22:56:55 +00:00
2016-12-11 19:24:52 +00:00
return {
arraySize = arraySize,
indexOf = indexOf,
outOfRange = outOfRange,
traceBackPath = traceBackPath
}
end