Skip to content

List

final class List<Type> < Object

Doubly-linked list. Beyond its typical uses, because List is a recursive data structure, it provides reasonably good sharing under Birch's lazy deep clone mechanism, although not as good as Stack and Queue, as they are singly-linked.

Caution

Long lists (length in the tens of thousands) can cause a segfault on destruction due to stack overflow. Possible solutions are:

  1. Use Vector instead.
  2. Remove items one-by-one before the List object goes out of scope (with e.g. popBack()).
  3. Increase the stack size with ulimit or similar.

Member Functions

Name Description
size Number of elements.
empty Is this empty?
clear Clear all elements.
front Get the first element.
back Get the last element.
get Get an element.
set Set an element.
pushFront Insert a new element at the start.
pushBack Insert a new element at the end.
popFront Remove the first element.
popBack Remove the last element.
insert Insert a new element.
erase Erase an element.
begin First node, if any.
end Last node, if any.
getNode Get a node.

Member Fibers

Name Description
walk Forward iteration.

Member Function Details

back

function back() -> Type

Get the last element.

begin

function begin() -> ListNode<Type>?

First node, if any. This can be used to maintain a bidirectional iterator over the container.

clear

function clear()

Clear all elements.

empty

function empty() -> Boolean

Is this empty?

end

function end() -> ListNode<Type>?

Last node, if any. This can be used to maintain a bidirectional iterator over the container.

erase

function erase(i:Integer)

Erase an element.

  • i: Position.

front

function front() -> Type

Get the first element.

get

function get(i:Integer) -> Type

Get an element.

  • i: Position.

getNode

function getNode(i:Integer) -> ListNode<Type>

Get a node.

  • i: Position.

insert

function insert(i:Integer, x:Type)

Insert a new element.

  • i: Position.
  • x: Value.

Inserts the new element immediately before the current element at position i. To insert at the end of the container, use a position that is one more than the current size, or pushBack().

popBack

function popBack()

Remove the last element.

popFront

function popFront()

Remove the first element.

pushBack

function pushBack(x:Type)

Insert a new element at the end.

  • x: Value.

pushFront

function pushFront(x:Type)

Insert a new element at the start.

  • x: Value.

set

function set(i:Integer, x:Type)

Set an element.

  • i: Position.
  • x: Value.

size

function size() -> Integer

Number of elements.

Member Fiber Details

walk

fiber walk() -> Type

Forward iteration.

Return: a fiber object that yields each element in forward order.