Quantcast
Download
(4 Kb)
Download
Updated: 05/16/21 11:57 AM
Pictures
File Info
Compatibility:
Blackwood (7.0.5)
Flames of Ambition (6.3.5)
Updated:05/16/21 11:57 AM
Created:05/16/21 12:46 AM
Monthly downloads:1,146
Total downloads:7,966
Favorites:0
MD5:
LibDeque (double-ended queue)  Popular! (More than 5000 hits)
Version: 2
by: Scootworks [More]
Lua 11.4 Queues and Double Queues



Although we can implement queues trivially using table.insert and table.remove, this implementation can be too slow for large structures.

If you use this structure in a strict queue discipline, calling only PushLast and PopFirst, both first and last will increase continually.

Most functions are equal Python deque functions.

A Test.lua file is attached with a few examples.

Create a new instance first!
lua Code:
  1. local deque = LibDeque:New()
  2. -- or
  3. local deque = LibDeque()

API
lua Code:
  1. -- Adds an element to the last
  2. LibDeque:PushLast(element)
lua Code:
  1. -- Pops the first element
  2. -- @return nilable:string|number|boolean|table|function|userdata
  3. LibDeque:PopFirst()
lua Code:
  1. -- Adds an element to the first
  2. LibDeque:PushFirst(element)
lua Code:
  1. -- Pops the last element
  2. -- @return nilable:string|number|boolean|table|function|userdata
  3. LibDeque:PopLast()
lua Code:
  1. -- Peek the first element, without Pop it
  2. -- @return nilable:element - string|number|boolean|table|function|userdata
  3. LibDeque:PeekFirst()
lua Code:
  1. -- Peek the last element, without Pop it
  2. -- @return nilable:element - string|number|boolean|table|function|userdata
  3. LibDeque:PeekLast()
lua Code:
  1. -- Extend the last-side of the deque by appending elements from the elements argument.
  2. -- reverseInput = nil|false -> elements[#elements] will be the the new last
  3. -- reverseInput = true -> elements[1] will be the the new last
  4. -- @param elements - table (indexed)
  5. -- @param reverseInput - boolean:nilable
  6. LibDeque:ExtendLast(elements, reverseInput)
lua Code:
  1. -- Extend the first-side of the deque by appending elements from the elements argument.
  2. -- reverseInput = nil|false -> elements[1] will be the the new first
  3. -- reverseInput = true -> elements[#elements] will be the the new first
  4. -- @param elements - table (indexed)
  5. -- @param reverseInput - boolean:nilable
  6. LibDeque:ExtendFirst(elements, reverseInput)
lua Code:
  1. -- Reverse the order of the elements
  2. LibDeque:Reverse()
lua Code:
  1. -- Return the length of a table (the number of elements in a table)
  2. -- @return number
  3. LibDeque:Len()
lua Code:
  1. -- Check if elements are empty
  2. -- @return boolean
  3. LibDeque:IsEmpty()
lua Code:
  1. -- Iterator from first-side. (First In - First Out)
  2. -- @return element - string|number|boolean|table|function|userdata
  3. LibDeque:Iterator()
lua Code:
  1. -- Iterator from last-side. (Last In - First Out)
  2. -- @return element - string|number|boolean|table|function|userdata
  3. LibDeque:ReversedIterator()
lua Code:
  1. -- Pop all elements from first to last (useReverseIterator == false|nil) or last to first (useReverseIterator == true).
  2. -- @param callback - function
  3. -- @param useReverseIterator - boolean:nilable
  4. LibDeque:Run(callback, useReverseIterator)
lua Code:
  1. -- Count the number of elements equal to the element.
  2. -- @param element - string|number|boolean|table|function|userdata
  3. -- @return count - number
  4. LibDeque:Count(element)
lua Code:
  1. -- Rotate the elements in positive or negative direction.
  2. -- Negative steps will bring the first element to last and positive steps will bring the last element to first.
  3. -- @param steps - number (integer)
  4. -- @return success - boolean:nilable
  5. LibDeque:Rotate(steps)
lua Code:
  1. -- Return the position of element in the deque (at or after index start and before index stop).
  2. -- Important: The index always starts with 1 and does not refer to self.first or self.last!
  3. -- The same goes for start and stop, they are based on the index and not on the table key!
  4. -- @param element - string|number|boolean|table|function|userdata
  5. -- @param start - number
  6. -- @param end - number
  7. -- @return index - number:nilable
  8. LibDeque:Index(element, start, stop)
lua Code:
  1. -- Insert element into the deque at position index.
  2. -- @param index - number
  3. -- @param element - string|number|boolean|table|function|userdata
  4. -- @return success - boolean:nilable
  5. LibDeque:Insert(index, element)
lua Code:
  1. -- Remove the first occurrence of value.
  2. -- @param element - string|number|boolean|table|function|userdata
  3. -- @return success - boolean:nilable
  4. LibDeque:Remove(element)
lua Code:
  1. -- Creates a new instance by copying all elements from the source table.
  2. -- @return instance
  3. LibDeque:Copy()
lua Code:
  1. -- Clears all elements and reset to default.
  2. -- Attention: Be aware, collectGarbage can cause frame drops. Use it wisely!
  3. -- @param collectGarbage - boolean:nilable
  4. -- @return success - boolean:nilable
  5. LibDeque:Clear(collectGarbage)
v2
- fixed an issue with Index() function when using start and stop param
Archived Files (1)
File Name
Version
Size
Uploader
Date
1
4kB
Scootworks
05/16/21 12:46 AM


There have been no comments posted to this file.
Be the first to add one.



Category Jump: