OpenComputers Von Neumann Machine Programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
608 B

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. -- Simple FIFO Queue
  2. local Queue = {}
  3. Queue.__index = Queue
  4. setmetatable(Queue, {
  5. __call = function (cls)
  6. return cls.new()
  7. end,
  8. })
  9. function Queue.new()
  10. local self = setmetatable({}, Queue)
  11. self.first = 0
  12. self.last = -1
  13. return self
  14. end
  15. function Queue:push(e)
  16. local last = self.last + 1
  17. self.last = last
  18. self[last] = e
  19. end
  20. function Queue:pop()
  21. -- To save some accesses
  22. local first = self.first
  23. if first > self.last then return nil end
  24. local value = self[first]
  25. self[first] = nil
  26. self.first = first + 1
  27. return value
  28. end
  29. return Queue