int_list #
line 62func int_list() -> &IntList
An empty IntList with a small initial capacity.
Source
func int_list() -> &IntList { return new IntList { items: new I32Values[8]{}, count: 0 }; }
Reed's standard library. Imported with use std.list; not on disk.
| int_list | An empty IntList with a small initial capacity. |
| int_list_with_capacity | An empty IntList sized to hold capacity elements without reallocating. |
| int_list_from | An IntList holding a copy of values. |
| long_list | An empty LongList. |
| ref_list | An empty RefList. |
func int_list() -> &IntList
An empty IntList with a small initial capacity.
func int_list() -> &IntList { return new IntList { items: new I32Values[8]{}, count: 0 }; }
func int_list_with_capacity(capacity: i32) -> &IntList
An empty IntList sized to hold capacity elements without reallocating.
Worth using when the eventual size is known: it turns a sequence of doubling copies into a single allocation. A capacity below 1 is raised to 1, since a zero-length backing array can never double.
func int_list_with_capacity(capacity: i32) -> &IntList { return new IntList { items: new I32Values[capacity < 1 ? 1 : capacity]{}, count: 0 }; }
func int_list_from(values: &I32Values) -> &IntList
An IntList holding a copy of values.
func int_list_from(values: &I32Values) -> &IntList { let items: &I32Values = new I32Values[#values < 1 ? 1 : #values]{}; items.copy(0, values, 0, #values); return new IntList { items: items, count: #values }; }
func long_list() -> &LongList
An empty LongList.
func long_list() -> &LongList { return new LongList { items: new I64Values[8]{}, count: 0 }; }
func ref_list() -> &RefList
An empty RefList.
func ref_list() -> &RefList { return new RefList { items: new AnyValues[8]{}, count: 0 }; }
| IntList.len | How many elements the list holds. |
| IntList.capacity | How many elements the list can hold before it must grow. |
| IntList.is_empty | Whether the list holds no elements. |
| IntList.reserve | Ensures the list can hold at least capacity elements without reallocating. |
| IntList.push | Appends value to the end of the list, growing it if necessary. |
| IntList.pop | Removes and returns the last element. |
| IntList.at | The element at index. |
| IntList.get_or | The element at index, or fallback when the index is out of range. |
| IntList.set | Overwrites the element at index. |
| IntList.insert | Inserts value at index, shifting later elements right. |
| IntList.remove | Removes the element at index, shifting later elements left. |
| IntList.clear | Removes every element, keeping the allocated capacity. |
| IntList.index_of | The index of the first element equal to value, or -1. |
| IntList.contains | Whether the list holds an element equal to value. |
| IntList.sum | The sum of every element, wrapping on overflow like +. |
| IntList.min | The smallest element, or (0, 0) when the list is empty. |
| IntList.max | The largest element, or (0, 0) when the list is empty. |
| IntList.reverse | Reverses the list in place. |
| IntList.extend | Appends every element of other to self. |
| IntList.clone | An independent copy of the list. |
| IntList.to_array | The list's elements as a plain array, sized exactly to the length. |
| LongList.len | How many elements the list holds. |
| LongList.is_empty | Whether the list holds no elements. |
| LongList.push | Appends value, growing the list if necessary. |
| LongList.pop | Removes and returns the last element, or (0, 0) when empty. |
| LongList.at | The element at index, trapping when out of range. |
| LongList.set | Overwrites the element at index, ignoring an out-of-range index. |
| LongList.clear | Removes every element, keeping the allocated capacity. |
| LongList.sum | The sum of every element, wrapping on overflow. |
| RefList.len | How many elements the list holds. |
| RefList.is_empty | Whether the list holds no elements. |
| RefList.push | Appends value, growing the list if necessary. |
| RefList.pop | Removes and returns the last element, or (null, 0) when empty. |
| RefList.at | The element at index, or null when out of range. |
| RefList.set | Overwrites the element at index, ignoring an out-of-range index. |
| RefList.clear | Removes every element, releasing the list's references to them. |
func IntList.len(self: &IntList) -> i32
How many elements the list holds.
Not the same as #list.items, which is the capacity.
func IntList.len(self: &IntList) -> i32 { return self->count; }
func IntList.capacity(self: &IntList) -> i32
How many elements the list can hold before it must grow.
func IntList.capacity(self: &IntList) -> i32 { return #self->items; }
func IntList.is_empty(self: &IntList) -> i32
Whether the list holds no elements.
func IntList.is_empty(self: &IntList) -> i32 { return self->count == 0; }
func IntList.reserve(self: &IntList, capacity: i32) -> void
Ensures the list can hold at least capacity elements without reallocating.
func IntList.reserve(self: &IntList, capacity: i32) { if (capacity <= #self->items) { return; } var next: i32 = #self->items; loop grow() { next = next * 2; if (next >= capacity) { break grow(); } continue grow(); } let bigger: &I32Values = new I32Values[next]{}; bigger.copy(0, self->items, 0, self->count); self->items = bigger; }
func IntList.push(self: &IntList, value: i32) -> void
Appends value to the end of the list, growing it if necessary.
func IntList.push(self: &IntList, value: i32) { self.reserve(self->count + 1); self->items[self->count] = value; self->count = self->count + 1; }
func IntList.pop(self: &IntList) -> (i32, i32)
Removes and returns the last element.
Returns (0, 0) for an empty list. The second result distinguishes "popped a zero" from "there was nothing to pop", which a bare sentinel cannot.
func IntList.pop(self: &IntList) -> (i32, i32) { if (self->count == 0) { return (0, 0); } self->count = self->count - 1; return (self->items[self->count], 1); }
func IntList.at(self: &IntList, index: i32) -> i32
The element at index.
Traps when index is out of range, matching std.str's byte: a direct index that is out of range is a caller bug, not a value to paper over.
func IntList.at(self: &IntList, index: i32) -> i32 { if (index < 0 || index >= self->count) { unreachable; } return self->items[index]; }
func IntList.get_or(self: &IntList, index: i32, fallback: i32) -> i32
The element at index, or fallback when the index is out of range.
func IntList.get_or(self: &IntList, index: i32, fallback: i32) -> i32 { if (index < 0 || index >= self->count) { return fallback; } return self->items[index]; }
func IntList.set(self: &IntList, index: i32, value: i32) -> void
Overwrites the element at index.
Out-of-range indices are ignored rather than growing the list, since a write past the end has no obvious meaning (should the gap be zero-filled?).
func IntList.set(self: &IntList, index: i32, value: i32) { if (index < 0 || index >= self->count) { return; } self->items[index] = value; }
func IntList.insert(self: &IntList, index: i32, value: i32) -> void
Inserts value at index, shifting later elements right.
An index at or past the length appends; a negative one prepends.
func IntList.insert(self: &IntList, index: i32, value: i32) { let at: i32 = index < 0 ? 0 : (index > self->count ? self->count : index); self.reserve(self->count + 1); // Copy backwards through the overlap: `array.copy` handles overlapping ranges correctly // (it is specified as if through an intermediate), so one call moves the whole tail. self->items.copy(at + 1, self->items, at, self->count - at); self->items[at] = value; self->count = self->count + 1; }
func IntList.remove(self: &IntList, index: i32) -> (i32, i32)
Removes the element at index, shifting later elements left.
Returns (0, 0) when index is out of range.
func IntList.remove(self: &IntList, index: i32) -> (i32, i32) { if (index < 0 || index >= self->count) { return (0, 0); } let removed: i32 = self->items[index]; self->items.copy(index, self->items, index + 1, self->count - index - 1); self->count = self->count - 1; return (removed, 1); }
func IntList.clear(self: &IntList) -> void
Removes every element, keeping the allocated capacity.
func IntList.clear(self: &IntList) { self->count = 0; }
func IntList.index_of(self: &IntList, value: i32) -> i32
The index of the first element equal to value, or -1.
func IntList.index_of(self: &IntList, value: i32) -> i32 { for i in 0..self->count { if (self->items[i] == value) { return i; } } return -1; }
func IntList.contains(self: &IntList, value: i32) -> i32
Whether the list holds an element equal to value.
func IntList.contains(self: &IntList, value: i32) -> i32 { return self.index_of(value) >= 0; }
func IntList.sum(self: &IntList) -> i32
The sum of every element, wrapping on overflow like +.
func IntList.sum(self: &IntList) -> i32 { var total: i32 = 0; for i in 0..self->count { total = total + self->items[i]; } return total; }
func IntList.min(self: &IntList) -> (i32, i32)
The smallest element, or (0, 0) when the list is empty.
func IntList.min(self: &IntList) -> (i32, i32) { if (self->count == 0) { return (0, 0); } var best: i32 = self->items[0]; for i in 1..self->count { if (self->items[i] < best) { best = self->items[i]; } } return (best, 1); }
func IntList.max(self: &IntList) -> (i32, i32)
The largest element, or (0, 0) when the list is empty.
func IntList.max(self: &IntList) -> (i32, i32) { if (self->count == 0) { return (0, 0); } var best: i32 = self->items[0]; for i in 1..self->count { if (self->items[i] > best) { best = self->items[i]; } } return (best, 1); }
func IntList.reverse(self: &IntList) -> void
Reverses the list in place.
func IntList.reverse(self: &IntList) { var lo: i32 = 0; var hi: i32 = self->count - 1; loop swap() { if (lo >= hi) { break swap(); } let t: i32 = self->items[lo]; self->items[lo] = self->items[hi]; self->items[hi] = t; lo = lo + 1; hi = hi - 1; continue swap(); } }
func IntList.extend(self: &IntList, other: &IntList) -> void
Appends every element of other to self.
func IntList.extend(self: &IntList, other: &IntList) { self.reserve(self->count + other->count); self->items.copy(self->count, other->items, 0, other->count); self->count = self->count + other->count; }
func IntList.clone(self: &IntList) -> &IntList
An independent copy of the list.
func IntList.clone(self: &IntList) -> &IntList { let items: &I32Values = new I32Values[#self->items]{}; items.copy(0, self->items, 0, self->count); return new IntList { items: items, count: self->count }; }
func IntList.to_array(self: &IntList) -> &I32Values
The list's elements as a plain array, sized exactly to the length.
This is the bridge to std.sort and to any code that wants a bare array rather than a list; the array is a copy, so mutating it does not disturb the list.
func IntList.to_array(self: &IntList) -> &I32Values { let out: &I32Values = new I32Values[self->count]{}; out.copy(0, self->items, 0, self->count); return out; }
func LongList.len(self: &LongList) -> i32
How many elements the list holds.
func LongList.len(self: &LongList) -> i32 { return self->count; }
func LongList.is_empty(self: &LongList) -> i32
Whether the list holds no elements.
func LongList.is_empty(self: &LongList) -> i32 { return self->count == 0; }
func LongList.push(self: &LongList, value: i64) -> void
Appends value, growing the list if necessary.
func LongList.push(self: &LongList, value: i64) { if (self->count == #self->items) { let bigger: &I64Values = new I64Values[#self->items * 2]{}; bigger.copy(0, self->items, 0, self->count); self->items = bigger; } self->items[self->count] = value; self->count = self->count + 1; }
func LongList.pop(self: &LongList) -> (i64, i32)
Removes and returns the last element, or (0, 0) when empty.
func LongList.pop(self: &LongList) -> (i64, i32) { if (self->count == 0) { return (0, 0); } self->count = self->count - 1; return (self->items[self->count], 1); }
func LongList.at(self: &LongList, index: i32) -> i64
The element at index, trapping when out of range.
func LongList.at(self: &LongList, index: i32) -> i64 { if (index < 0 || index >= self->count) { unreachable; } return self->items[index]; }
func LongList.set(self: &LongList, index: i32, value: i64) -> void
Overwrites the element at index, ignoring an out-of-range index.
func LongList.set(self: &LongList, index: i32, value: i64) { if (index < 0 || index >= self->count) { return; } self->items[index] = value; }
func LongList.clear(self: &LongList) -> void
Removes every element, keeping the allocated capacity.
func LongList.clear(self: &LongList) { self->count = 0; }
func LongList.sum(self: &LongList) -> i64
The sum of every element, wrapping on overflow.
func LongList.sum(self: &LongList) -> i64 { var total: i64 = 0; for i in 0..self->count { total = total + self->items[i]; } return total; }
func RefList.len(self: &RefList) -> i32
How many elements the list holds.
func RefList.len(self: &RefList) -> i32 { return self->count; }
func RefList.is_empty(self: &RefList) -> i32
Whether the list holds no elements.
func RefList.is_empty(self: &RefList) -> i32 { return self->count == 0; }
func RefList.push(self: &RefList, value: ?any) -> void
Appends value, growing the list if necessary.
value may be null: a RefList distinguishes "holds null at index 3" from "has no index 3", which is why at does not use null as its out-of-range answer.
func RefList.push(self: &RefList, value: ?any) { if (self->count == #self->items) { let bigger: &AnyValues = new AnyValues[#self->items * 2]{}; bigger.copy(0, self->items, 0, self->count); self->items = bigger; } self->items[self->count] = value; self->count = self->count + 1; }
func RefList.pop(self: &RefList) -> (?any, i32)
Removes and returns the last element, or (null, 0) when empty.
func RefList.pop(self: &RefList) -> (?any, i32) { if (self->count == 0) { return (null, 0); } self->count = self->count - 1; let value: ?any = self->items[self->count]; // Drop the list's own reference so a popped object can be collected while the list // lives on. Without this, a long-lived list pins every object it ever held. self->items[self->count] = null; return (value, 1); }
func RefList.at(self: &RefList, index: i32) -> ?any
The element at index, or null when out of range.
Narrow the result with is (spec section 9) to recover its type.
func RefList.at(self: &RefList, index: i32) -> ?any { if (index < 0 || index >= self->count) { return null; } return self->items[index]; }
func RefList.set(self: &RefList, index: i32, value: ?any) -> void
Overwrites the element at index, ignoring an out-of-range index.
func RefList.set(self: &RefList, index: i32, value: ?any) { if (index < 0 || index >= self->count) { return; } self->items[index] = value; }
func RefList.clear(self: &RefList) -> void
Removes every element, releasing the list's references to them.
Unlike IntList.clear, this nulls the vacated slots: leaving stale references would keep every object the list ever held alive for as long as the list itself.
func RefList.clear(self: &RefList) { self->items.fill(0, null, self->count); self->count = 0; }
| IntList | A growable list of i32 values. |
| LongList | A growable list of i64 values. |
| RefList | A growable list of nullable references, for heterogeneous contents. |
struct IntList { items: mut &I32Values, count: mut i32, }
A growable list of i32 values.
struct IntList { items: mut &I32Values, count: mut i32, }
struct LongList { items: mut &I64Values, count: mut i32, }
A growable list of i64 values.
struct LongList { items: mut &I64Values, count: mut i32, }
struct RefList { items: mut &AnyValues, count: mut i32, }
A growable list of nullable references, for heterogeneous contents.
Elements are ?any: a struct, an array, a string, a boxed i31, or null. Read one back with at and narrow it with is (spec section 9).
struct RefList { items: mut &AnyValues, count: mut i32, }