turbo.structs – Data structures

Usefull data structures implemented using Lua and the LuaJIT FFI.

deque, Double ended queue

A deque can insert items at both the beginning and then end in constant time, “O(1)” If you are going to insert things regurarly to the front it is wise to use this class instead of the standard Lua table. Keep in mind that inserting to the back is still slower than a Lua table.


Create a new deque class instance.

Return type:Deque class instance

Append elements to tail.


Append element to head.


Returns element at tail.


Returns element at front.


Removes element at tail and returns it.


Removes element at head and returns it.


Check if deque is empty.

Return type:Boolean

Returns the amount of elements in the deque.


Find length of all elements in deque combined. (Only works if the elements have a :len() method)


Concat elements in deque. Only works if the elements in the deque have a :__concat() method.


Get element at position.

Return type:Element or nil if not existing.

buffer, Low-level mutable buffer

Can be used to replace plain Lua strings where it is of importance to not create temporary strings, and there is little help in the Lua string interning. It is mutable and allows preallocations to be done on intialization. The data stored in a buffer is not handled by the LuaJIT 2.0 GC which in turn circumvents the memory limit.

Keep in mind that this class i “low-level” and giving the wrong arguments to its methods may cause memory segmentation fault. It is NOT protected.


Create a new buffer. May raise error if there is not enough memory available.

Parameters:size_hint (Number) – The buffer is preallocated with this amount (in bytes) of storage.
Return type:Buffer class instance.
Buffer:append_right(data, len)

Append data to buffer. Keep in mind that defining a length longer than the actual data, might lead to a segmentation fault.

  • data (char *) – The data to append in char * form.
  • len (Number) – The length of the data in bytes.

Append Lua string to buffer.

Parameters:str (String) – The data to append.
Buffer:append_left(data, len)

Prepend data to buffer.

  • data (char *) – The data to prepend in char * form.
  • len (Number) – The length of the data in bytes.

Prepend Lua string to the buffer.

Parameters:str (String) – The data to prepend.

Pop bytes from left side of buffer. If sz exceeds size of buffer then a error is raised. Note: does not release memory allocated.

Parameters:sz (Number) – Bytes to “pop”.

Pop bytes from right side of the buffer. If sz exceeds size of buffer then a error is raised. Note: does not release memory allocated.

Parameters:sz (Number) – Bytes to “pop”.

Get internal buffer pointer. Must be treated as a const value. Keep in mind that the internal pointer may or may not change when calling its methods.

Return type:Two values: const char * to data and current size in bytes.

Create a “deep” copy of the buffer.

Return type:Buffer class instance

Shrink buffer memory (deallocate) usage to its minimum.


Clear buffer. Note: does not release memory allocated.

Parameters:wipe (Boolean) – Optional switch to zero fill allocated memory range.

Get current size of the buffer.

Return type:Number. Size in bytes.

Get the total number of bytes currently allocated to this instance.

Return type:Number. Bytes allocated.

Convert to Lua type string using the tostring() builtin or implicit conversions.


Compare two buffers by using the == operator.


Concat by using the .. operator, Lua type strings can be concated also. Please note that all concatination involves deep copying and is slower than manually building a buffer with append methods.