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

class List
module:

Doubly linked list with iterator support.

static new(object?)

Create a new List

Parameters:

object? (table)

Return type:

List

pushBack(value)

Push a value to the back of the list

Parameters:

value (any)

popBack()

Pop the last value from the list

Return type:

any

pushFront(value)

Push a value to the front of the list

Parameters:

value (any)

popFront()

Pop the first value from the list

Return type:

any

front()

Get the first value

Return type:

any

back()

Get the last value

Return type:

any

empty()

Check if the list is empty

Return type:

boolean

clear()

Remove all elements from the list

begin()

Get an iterator to the beginning of the list

Return type:

List_Iterator

find(v, start?)

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

Parameters:
  • v (any)

  • start? (List_Iterator)

Return type:

List_Iterator or nil

findLast(v, start?)

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

Parameters:
  • v (any)

  • start? (List_Iterator)

Return type:

List_Iterator or nil

erase(itr)

Erase the node at the iterator

Parameters:

itr (List_Iterator)

Return type:

List_Iterator

eraseValue(value)

Erase the first occurrence of a value

Parameters:

value (any)

eraseAll(value)

Erase all occurrences of a value

Parameters:

value (any)

insert(itr, value)

Insert a value after the iterator

Parameters:
  • itr (List_Iterator)

  • value (any)

Return type:

List_Iterator or nil

insertBefore(itr, value)

Insert a value before the iterator

Parameters:
  • itr (List_Iterator)

  • value (any)

Return type:

List_Iterator or nil

print()

Print all elements (in reverse order)

size()

Count the number of elements

Return type:

number

clone()

Create a clone of the list

Return type:

List