Advertisement
Francoo

Simple Queue/List Class

Jun 30th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1. -- Copyright (c) 2015, Franco Sauvisky
  2. -- All rights reserved.
  3.  
  4. -- Redistribution and use in source and binary forms, with or without
  5. -- modification, are permitted provided that the following conditions
  6. -- are met:
  7. -- 1. Redistributions of source code must retain the above copyright
  8. --    notice, this list of conditions and the following disclaimer.
  9. -- 2. Redistributions in binary form must reproduce the above copyright
  10. --    notice, this list of conditions and the following disclaimer in the
  11. --    documentation and/or other materials provided with the distribution.
  12. -- 3. The name of the author may not be used to endorse or promote products
  13. --    derived from this software without specific prior written permission.
  14.  
  15. -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. -- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. -- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. -- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. -- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. -- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. -- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  
  26. -- Simple queue/list class:
  27.  
  28. -- It might be useful to change the popleft and popright errors to "return
  29. -- nil", depending on how you use it (if having nil elements is permitted).
  30.  
  31. local List = {begin = 0, final = 1}
  32.  
  33. function List:new()
  34.     local newlist = {}
  35.     setmetatable(newlist,self)
  36.     self.__index = self
  37.     return newlist
  38. end
  39.  
  40. function List:pushleft(value)
  41.     self[self.begin] = value
  42.     self.begin = self.begin - 1
  43. end
  44.  
  45. function List:pushright(value)
  46.     self[self.final] = value
  47.     self.final = self.final + 1
  48. end
  49.  
  50. function List:popright()
  51.     if self.final == self.begin + 1 then
  52.         error("No element to pop") -- Or return nil?
  53.     end
  54.  
  55.     local value = self[self.final - 1]
  56.     self[self.final] = nil
  57.     self.final = self.final - 1
  58.     return value
  59. end
  60.  
  61. function List:popleft()
  62.     if self.begin == self.final - 1 then
  63.         error("No element to pop") -- Or return nil?
  64.     end
  65.  
  66.     local value = self[self.begin + 1]
  67.     self[self.begin] = nil
  68.     self.begin = self.begin + 1
  69.     return value
  70. end
  71.  
  72. return List
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement