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 ideal • machine • elements • array
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 elements • size
Make sure the array is of at least the specified size.
void reserve(nonnegative reserve size)
if the elements • size >= reserve size
return
var new size : the elements • size * 2
if new size < reserve size
new size = reserve size
new elements : array[element type] • new(new size)
the elements • copy(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 elements • is empty
return
else if new elements • size == 1
insert(index, new elements • first)
return
assert writable
extra size : new elements • size
reserve and move(index, extra size)
new elements array • copy(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 elements • move(index, index + extra size, tail size)
size += extra size
void clear(nonnegative begin, nonnegative length)
if begin + length < size
the elements • move(begin + length, begin, length)
new size : size - length
assert new size is nonnegative
size = new size
the elements • scrub(size, length)
list state[element type] copy()
the elements • copy(0, new state • the elements, 0, size)
new state • size = 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 => state • size
Specifies whether the collection has zero elements.
implement boolean is empty => state • size == 0
Specifies whether the collection has more than zero elements. Shortcut for !is_empty.
implement boolean is not empty => state • size != 0
Access the first element of the list. Assumes the list is not empty.
implement element type first()
assert is not empty
return state • the 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 : state • size - 1
assert last index is nonnegative
return state • the elements[last index]
Read the list's element for the specified index.
implement implicit readonly reference[element type] get(nonnegative index) pure
assert index < state • size
return state • the 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 state • size = length
state • the elements • copy(begin, slice state • the 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)
Check whether the list has at least one element that satisfies the predicate.
implement boolean has(predicate[element type] the predicate) pure
for var nonnegative index : 0; index < state • size; index += 1
if the predicate(state • the elements[index])
return true
return false