A list is an ordered sequence of values.
class base readonly list[value element type]
implements readonly list[element type]
namespace parameters
Wrapper of an array that implements on-demand resizing and copy-on-write semantics. The same list_state can be shared by multiple instances of base_readonly_lists.
protected class list state[value element type]
import idealmachineelementsarray
Specifies whether this instance of list_state is writable. A single non-writable copy can be shared among multiple instances of base_readonly_list.
var boolean writable
An array used to store the elements.
var array[element type] the elements
Specifies how many elements are stored in this list_state. The size is less or equal to the_elements.size.
var nonnegative size
Construct a list state with an array of specified size.
overload list state(nonnegative initial size)
the elements = array[element type] • new(initial size)
size = 0
Construct a list state with an array of default size.
overload list state()
Construct a list state from a given array of elements. Assumes noone mutates the elements.
overload list state(array[element type] immutable elements)
the elements = immutable elements
size = immutable elementssize
Make sure the array is of at least the specified size.
void reserve(nonnegative reserve size)
if the elementssize >= reserve size
var new size : the elementssize * 2
if new size < reserve size
new size = reserve size
new elements : array[element type] • new(new size)
the elementscopy(0, new elements, 0, size)
the elements = new elements
Insert elements at the specified index.
void insert all(nonnegative index, readonly list[element type] new elements)
if new elementsis empty
else if new elementssize == 1
insert(index, new elementsfirst)
assert writable
extra size : new elementssize
reserve and move(index, extra size)
new elements arraycopy(0, the elements, index, extra size)
Insert an element at the specified index.
void insert(nonnegative index, element type element)
assert writable
reserve and move(index, 1)
the elements[index] = element
Helper method used to create space in the middle of an array.
private void reserve and move(nonnegative index, nonnegative extra size)
reserve(size + extra size)
if index < size
tail size : size - index
assert tail size is nonnegative
the elementsmove(index, index + extra size, tail size)
size += extra size
void clear(nonnegative begin, nonnegative length)
if begin + length < size
the elementsmove(begin + length, begin, length)
new size : size - length
assert new size is nonnegative
size = new size
the elementsscrub(size, length)
list state[element type] copy()
the elementscopy(0, new statethe elements, 0, size)
new statesize = size
return new state
protected var list state[element type] state
protected overload base readonly list()
protected overload base readonly list(list state[element type] state)
this • state = state
The number of elements in the collection.
implement nonnegative size => statesize
Specifies whether the collection has zero elements.
implement boolean is empty => statesize == 0
Specifies whether the collection has more than zero elements. Shortcut for !is_empty.
implement boolean is not empty => statesize != 0
Access the first element of the list. Assumes the list is not empty.
implement element type first()
assert is not empty
return statethe elements[0]
Access the last element of the list. Assumes the list is not empty.
implement element type last()
assert is not empty
last index : statesize - 1
assert last index is nonnegative
return statethe elements[last index]
Read the list's element for the specified index.
implement implicit readonly reference[element type] get(nonnegative index) pure
assert index < statesize
return statethe elements[index]
Enumerates elements in some collection-defined order. This method returns a snapshot of the collection state, so subsequent mutations of the collection do not cause changes in the returned list.
implement immutable list[element type] elements() => frozen copy()
Returns an immutable copy of this list.
implement immutable list[element type] frozen copy()
Returns an immutable sublist with the given the starting and ending indices.
The starting index is inclusive, the ending is exclusive.
implement immutable list[element type] slice(nonnegative begin, nonnegative end)
assert begin >= 0 && end <= size
length : end - begin
assert length is nonnegative
slice state : list state[element type] • new(length)
slice statesize = length
statethe elementscopy(begin, slice statethe elements, 0, length)
return base immutable list[element type] • new(slice state)
Skips over the specified count of elements and returns an immutable slice that begins with count and ends with the end of this list.
implement immutable list[element type] skip(nonnegative count)
return slice(count, size)