Skip to content

List

A List is a flexible collection that stores elements in a specific order using a doubly linked data structure. It allows efficient insertion and removal of items at both the front and back of the list, as well as sequential traversal using iterators. This class provides methods to add, remove, search, and iterate over elements, making it suitable for scenarios where dynamic, ordered collections are needed.

Usage

To use this class, add the following require at the top of your script:

local List = require 'engine/data/list'

Reference

new

static new(object)

Create a new List

Parameters

  • object (table) (optional)

Returns

  • List

pushBack

pushBack(value)

Push a value to the back of the list

Parameters

  • value (any)

popBack

popBack()

Pop the last value from the list

Returns

  • any

pushFront

pushFront(value)

Push a value to the front of the list

Parameters

  • value (any)

popFront

popFront()

Pop the first value from the list

Returns

  • any

front

front()

Get the first value

Returns

  • any

back

back()

Get the last value

Returns

  • any

empty

empty()

Check if the list is empty

Returns

  • boolean

clear

clear()

Remove all elements from the list

begin

begin()

Get an iterator to the beginning of the list

Returns

  • List_Iterator

end

_end()

Get an iterator to the end of the list

Returns

  • List_Iterator

find

find(v, start)

Find a value starting from an iterator (or from beginning)

Parameters

  • v (any)
  • start (List_Iterator) (optional)

Returns

  • List_Iterator|nil

findLast

findLast(v, start)

Find a value starting from the end (or from a given iterator)

Parameters

  • v (any)
  • start (List_Iterator) (optional)

Returns

  • List_Iterator|nil

erase

erase(itr)

Erase the node at the iterator

Parameters

  • itr (List_Iterator)

Returns

  • List_Iterator

eraseValue

eraseValue(value)

Erase the first occurrence of a value

Parameters

  • value (any)

eraseAll

eraseAll(value)

Erase all occurrences of a value

Parameters

  • value (any)

insert

insert(itr, value)

Insert a value after the iterator

Parameters

  • itr (List_Iterator)
  • value (any)

Returns

  • List_Iterator|nil

insertBefore

insertBefore(itr, value)

Insert a value before the iterator

Parameters

  • itr (List_Iterator)
  • value (any)

Returns

  • List_Iterator|nil

print

print()

Print all elements (in reverse order)

size

size()

Count the number of elements

Returns

  • number

clone

clone()

Create a clone of the list

Returns

  • List

Class: List

new

static new(object)

Create a new List

Parameters

  • object (table) (optional)

Returns

  • List

pushBack

pushBack(value)

Push a value to the back of the list

Parameters

  • value (any)

popBack

popBack()

Pop the last value from the list

Returns

  • any

pushFront

pushFront(value)

Push a value to the front of the list

Parameters

  • value (any)

popFront

popFront()

Pop the first value from the list

Returns

  • any

front

front()

Get the first value

Returns

  • any

back

back()

Get the last value

Returns

  • any

empty

empty()

Check if the list is empty

Returns

  • boolean

clear

clear()

Remove all elements from the list

begin

begin()

Get an iterator to the beginning of the list

Returns

  • List_Iterator

end

_end()

Get an iterator to the end of the list

Returns

  • List_Iterator

find

find(v, start)

Find a value starting from an iterator (or from beginning)

Parameters

  • v (any)
  • start (List_Iterator) (optional)

Returns

  • List_Iterator|nil

findLast

findLast(v, start)

Find a value starting from the end (or from a given iterator)

Parameters

  • v (any)
  • start (List_Iterator) (optional)

Returns

  • List_Iterator|nil

erase

erase(itr)

Erase the node at the iterator

Parameters

  • itr (List_Iterator)

Returns

  • List_Iterator

eraseValue

eraseValue(value)

Erase the first occurrence of a value

Parameters

  • value (any)

eraseAll

eraseAll(value)

Erase all occurrences of a value

Parameters

  • value (any)

insert

insert(itr, value)

Insert a value after the iterator

Parameters

  • itr (List_Iterator)
  • value (any)

Returns

  • List_Iterator|nil

insertBefore

insertBefore(itr, value)

Insert a value before the iterator

Parameters

  • itr (List_Iterator)
  • value (any)

Returns

  • List_Iterator|nil
print()

Print all elements (in reverse order)

size

size()

Count the number of elements

Returns

  • number

clone

clone()

Create a clone of the list

Returns

  • List