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.

deque()

Create a new deque class instance.

Return type:Deque class instance
deque:append(item)

Append elements to tail.

deque:appendleft(item)

Append element to head.

deque:peeklast()

Returns element at tail.

deque:peekfirst()

Returns element at front.

deque:pop()

Removes element at tail and returns it.

deque:popleft()

Removes element at head and returns it.

deque:not_empty()

Check if deque is empty.

Return type:Boolean
deque:size()

Returns the amount of elements in the deque.

deque:strlen()

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

deque:concat()

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

deque:getn(pos)

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.

Buffer(size_hint)

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.

Parameters:
  • data (char *) – The data to append in char * form.
  • len (Number) – The length of the data in bytes.
Buffer:append_luastr_right(str)

Append Lua string to buffer.

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

Prepend data to buffer.

Parameters:
  • data (char *) – The data to prepend in char * form.
  • len (Number) – The length of the data in bytes.
Buffer:append_luastr_left(str)

Prepend Lua string to the buffer.

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

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”.
Buffer:pop_right(sz)

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”.
Buffer:get()

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.
Buffer:copy()

Create a “deep” copy of the buffer.

Return type:Buffer class instance
Buffer:shrink()

Shrink buffer memory (deallocate) usage to its minimum.

Buffer:clear(wipe)

Clear buffer. Note: does not release memory allocated.

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

Get current size of the buffer.

Return type:Number. Size in bytes.
Buffer:mem()

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

Return type:Number. Bytes allocated.
Buffer:__tostring()

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

Buffer:__eq(cmp)

Compare two buffers by using the == operator.

Buffer:__concat(src)

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.