Advertisement
LoganDark

Traverse (ComputerCraft)

Dec 13th, 2021
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.24 KB | None | 0 0
  1. -- Copyright © 2021 LoganDark
  2. --
  3. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  4. -- of this software and associated documentation files (the "Software"), to deal
  5. -- in the Software without restriction, including without limitation the rights
  6. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. -- copies of the Software, and to permit persons to whom the Software is
  8. -- furnished to do so, subject to the following conditions:
  9. --
  10. -- The above copyright notice and this permission notice shall be included in
  11. -- all copies or substantial portions of the Software.
  12. --
  13. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. -- SOFTWARE.
  20.  
  21. local next = next
  22. local type = type
  23.  
  24. return function(start, callback)
  25.     local stack = {
  26.         data = {start, next(start)},
  27.         len = 1,
  28.         seen = {[start] = true}
  29.     }
  30.  
  31.     while stack.len > 0 do
  32.         local tblIdx = stack.len * 2 - 1
  33.         local keyIdx = tblIdx + 1
  34.  
  35.         local tbl = stack.data[tblIdx]
  36.         local key = stack.data[keyIdx]
  37.  
  38.         if key == nil then
  39.             stack.len = stack.len - 1
  40.             stack.data[stack.len * 2 + 1] = nil
  41.  
  42.             if stack.len > 0 then
  43.                 stack.data[keyIdx - 2] = next(stack.data[tblIdx - 2], stack.data[keyIdx - 2])
  44.             end
  45.         else
  46.             local value = tbl[key]
  47.  
  48.             callback(value, key, tbl, stack)
  49.  
  50.             if type(value) == 'table' and not stack.seen[value] then
  51.                 stack.seen[value] = true
  52.                 stack.data[stack.len * 2 + 1] = value
  53.                 stack.data[stack.len * 2 + 2] = next(value)
  54.                 stack.len = stack.len + 1
  55.             else
  56.                 stack.data[keyIdx] = next(tbl, key)
  57.             end
  58.         end
  59.     end
  60. end
  61.  
  62. --[[ Example usage:
  63. local traverse = require('traverse')
  64.  
  65. traverse(_G, function(v, k, tbl, stack)
  66.     local keys = {}
  67.  
  68.     for i = 2, stack.len * 2, 2 do
  69.         keys[i / 2] = stack.data[i]
  70.     end
  71.  
  72.     print(table.unpack(keys, 1, stack.len))
  73.     os.sleep(0.05)
  74. end) ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement