Advertisement
Guest User

Untitled

a guest
May 29th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. --take args and put them into a table with the func
  2. local function pre(func, arg1, arg2, arg3, arg4)
  3.      local pass = {func, arg1, arg2, arg3, arg4}
  4.      return pass
  5. end
  6. -- take the table you stored in the q and return it as a function call with args in place.
  7. local function post(passedTable)
  8.      return passedTable[1](passedTable[2],passedTable[3],passedTable[4],passedTable[5])
  9. end
  10. --new q with post and pre functions
  11. newQueue(pre(), post())
  12.  
  13. local function addToQ(func,arg1,arg2,arg3,arg4)
  14.           local pass = {func, arg1, arg2, arg3, arg4}
  15.           Q.push(pass)
  16. end
  17. -- usage scenario
  18. if go then
  19.      addToQ(modeSwitch(),soundDirName, gameMode, playTimes)
  20. end
  21.      --queue structure
  22. local function newQueue(preprocess, postprocess)
  23.     local first = 0
  24.     local last = -1
  25.     local queue = {}
  26.  
  27.     local push = preprocess and
  28.         function(value)
  29.             local last = last + 1
  30.             queue[last] = preprocess(value)
  31.         end
  32.     or
  33.         function(value)
  34.             local last = last + 1
  35.             queue[last] = value
  36.         end
  37.  
  38.     local pop = postprocess and
  39.         function()
  40.             if first > last then return end
  41.             local value = queue[first]
  42.             queue[first] = nil                -- to allow garbage collection
  43.             first = first + 1
  44.             return postprocess(value)
  45.         end
  46.     or
  47.         function()
  48.             if first > last then return end
  49.             local value = queue[first]
  50.             queue[first] = nil                -- to allow garbage collection
  51.             first = first + 1
  52.             return value
  53.         end
  54.  
  55.     return {push = push; pop = pop}
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement