Contiguous same-schema storage with explicit size and reserve management.
Storage and reserve model
sizeis the current item count.reserveis the current backing-storage capacity measured in items.datareturns the current first-item reference and may beNILbefore the first allocation or afterrelease.clearremoves all items while preserving the reserved capacity.releaseremoves all items and frees the backing storage.setReserveonly raises the reserved capacity.shrinkreduces only the item count and leaves the reserved capacity unchanged.enlargenever reduces the item count; it raises the reserved capacity when the new size does not fit.resizemay shrink or grow the item count according to the requested size.- When
Itemis managed (automatic?reportsTRUE), enlargement initializes each added item and shrinking destroys each removed item. Otherwise enlargement does not initialize the added element storage.resizefollows the same growing and shrinking rules.
Reference stability and invalidation
at,last,data,span, andslicereturn references or views into the current contiguous storage.- Any operation that reallocates the backing storage invalidates those earlier references and views.
- Reallocation can occur during
append,enlarge,resizegrowth, andsetReservewhen the current reserve is insufficient or the reserve is raised. releaseinvalidates all earlier references and views.clearpreserves capacity but destroys the former items, so references to those former items are no longer valid values.eraseshifts each item after the erased ordinal one ordinal lower,eraseUnstablemoves the last item into the erased ordinal, anderaseIfcompacts retained items. Earlier references to removed or moved items must be treated as invalid.popBack,shrink, andresizeshrink invalidate references to removed tail items.
makeArrayObject (Item memoryDebugObject -- emptyArray)
Constructs an empty array object whose schema is Array<ItemSchemaName>, for the supplied item schema and memory-debug selection.
Itemis an object of the item schema,memoryDebugObjectis a knownCond, andemptyArrayis the resulting empty array.FALSEselects the ordinary array storage path;TRUEselects the memory-debug object path used byMemoryDebugArray.Item Arrayis the normal construction form. CallmakeArrayObjectdirectly only when the memory-debug selection must be supplied explicitly; the publicArrayandMemoryDebugArraynames are its predefined wrappers.
Array
Methods
Array method ordinals, offsets, item counts and reserve counts have schema Int32. at rejects a key of any other schema at compile time (the error names ONLY_I32_ALLOWED); the size methods reject a non-Int32 size when comparing it with the current size.
append
(item/span/iter --) Appends item(s) to the end.Remarks
Appending data may cause reallocation if the reserve is insufficient, potentially invalidating obtained Refs. If the reserve is already sufficient, reallocation does not occur. If the source is a span or an iteration source with a size method, the storage is enlarged once before the items are stored.
The single-item form stores the item with set: an immutable view (s @a.append) is copied; a mutable view (@s @a.append) is moved and its source is left freshly constructed (an owned String becomes empty); a direct value (10 @a.append, a temporary) is moved into the new element.
Known issue: the single-item form grows the storage before it reads the item, so an item that references an element of this array is read from the released buffer when the append triggers a reallocation; the invalid read has no defined value. For a copyable item, copy first: 0 a.at new @a.append.
Example: single items and a tuple
append adds single items and the items of a tuple, giving (10 20 30 40 50).
"Array" use
"algorithm" use
"control" use
# "algorithm" supplies = between an Array and a Tuple
{} () {} [
numbers: Int32 Array;
# Append single items
10 @numbers.append
20 @numbers.append
[numbers (10 20) =] "[.append] produced wrong result" ensure
# Append multiple items
(30 40 50) @numbers.append
[numbers (10 20 30 40 50) =] "[.append] produced wrong result" ensure
"Done.\n" print
] "main" exportFunction
Expected Output
Done.
assign
(span/iter/array --) Replaces the array's contents with the source items.- The source's item schema is
Item; another item schema fails compilation (for exampleInconsistent schemas). - If the source reports a size, it is a nonnegative
Int32count.
Remarks
An Array of the same schema is copied when reached through an immutable view and moved when reached through a mutable view, which leaves the source empty.
at
(key -- ref) Returns Ref to the item at key.- key in [0..current item count - 1].
- Runtime bounds checks require
DEBUG; a failed check reportsIndex is out of range!, and with DEBUG disabled runtime bounds are the caller's responsibility.
clear
(--) Removes all items and preserves capacity.data
(-- ref) Returns the stored Ref to the first item.enlarge
(newSize --) Grows array size to newSize (≥ old size).- newSize ≥ current item count.
- The item lifecycle rules apply.
erase
(key --) Removes the item at key and shifts each following item one ordinal lower.- key in [0..current item count - 1].
predicateaccepts one item and reports aCond.
eraseUnstable
(key --) Removes the item at key; unless it was the last item, the last item moves to key.- key in [0..current item count - 1].
getHash
(-- hash) Combines 0x9E3779B9n32 and the hash of each item, left to right, with hashCombine.iter
(-- iter) Returns an iteration source over the current items.iterReverse
(-- iter) Returns a reverse iteration source over the current items.last
(-- ref) Returns Ref to the last item.- current item count > 0.
release
(--) Clears and frees all array memory.reserve
(-- count) Returns the current reserved capacity.Example: reserve and setReserve
A new array has a reserve of 0, and setReserve raises it to 4.
"Array" use
"control" use
{} () {} [
numbers: Int32 Array;
numbers.reserve printStack _:;
4 @numbers.setReserve
numbers.reserve printStack _:;
] "main" exportFunction
Expected Output During Compilation
0
4
resize
(newSize --) Changes size; can shrink or grow array.- newSize ≥ 0.
- The item lifecycle rules apply.
newReserve≥ current reserve.
- 0 ≤
newSize≤ current size.
- The item lifecycle rules apply.
size
(-- count) Returns the current number of items.slice
(offset size -- theSpan) Returns a span over the selected subrange.offsetin [0..current item count].size(the argument) in [0..current item count - offset].
Example: a two-item Array slice
Slicing two items from index 1 of (10 20 30 40) gives a view of 20 and 30.
"Array" use
"String" use
"control" use
{} Int32 {} [
numbers: Int32 Array;
(10 20 30 40) @numbers.assign
part: 1 2 @numbers.slice;
("size=" part.size LF
"first=" 0 @part.at new LF
"second=" 1 @part.at new LF) printList
0
] "main" exportFunction
Expected Output
size=2
first=20
second=30
span
(-- theSpan) Returns a span over [0..size-1].Example: append, last, and erase
After three appends, last is 30; erasing index 1 leaves two items, with 30 at index 1.
"Array" use
"String" use
"control" use
{} Int32 {} [
numbers: Int32 Array;
10 @numbers.append
20 @numbers.append
30 @numbers.append
("size=" numbers.size LF
"last=" @numbers.last new LF) printList
1 @numbers.erase
("size2=" numbers.size LF
"item1=" 1 @numbers.at new LF) printList
0
] "main" exportFunction
Expected Output
size=3
last=30
size2=2
item1=30
Example: eraseIf preserves retained order
eraseIf with the predicate [15 >] removes 20 and 30 from (10 20 30 15) and keeps 10 and 15 in order.
"Array" use
"String" use
"control" use
{} Int32 {} [
numbers: Int32 Array;
(10 20 30 15) @numbers.assign
[15 >] @numbers.eraseIf
("size=" numbers.size LF
"first=" 0 @numbers.at new LF
"last=" @numbers.last new LF) printList
0
] "main" exportFunction
Expected Output
size=2
first=10
last=15
Example: struct items
An array can hold structs; field x of item 0, read with .x, is an Int32 Cref.
"Array" use
"control" use
Point: [{x: Int32; y: Int32;}];
{} () {} [
points: Point Array;
{x: 10; y: 20;} @points.append
{x: 30; y: 40;} @points.append
0 @points.at .x printStack _:;
] "main" exportFunction
Expected Output During Compilation
Int32 Cref
MemoryDebugArray
Variant of Array with memory-debug instrumentation enabled.
Known issue: the instrumented allocation paths write to memoryDebugEnabled, a name that no module defines, so any allocation through MemoryDebugArray (or TRUE makeArrayObject) fails with Name was not found. Array is unaffected.
- Public methods follow the same interface as
Array. - The variant is intended for memory-accounting and debugging scenarios.
getHeapUsedSize (array -- size)
Returns total heap usage of the Array as Natx.
Known issue: with memory's getHeapUsedSize imported after Array, a call selects it instead; it returns 0nx for an Array.
- The outer array buffer is counted by reserved capacity, not only by current size.
- Item heap usage is included recursively. Known issue: the heap usage of an item that is not an Array counts as
0nxunless measuring it uses agetHeapUsedSizemarked withoverload; the Standard Library has none.
toArray (source -- array)
Creates a new Array from an existing Array, a span source, or an iteration source.
- An existing
Arraytakes precedence, followed by a source accepted bytoSpan, then one accepted bytoIter. - Existing
Arraysources are copied through immutable views and moved through mutable views; moving leaves the source empty. - For span sources, if
toSpanreturns a span whose items satisfyautomatic?, items are copied through immutable item views and moved through mutable item views, leaving moved-from items freshly constructed; the source range keeps its size. Items of other spans are byte-copied. - For iteration sources, items are copied through immutable references and moved through mutable references:
(@x @y) toArrayleaves theStringlocalsxandyempty.
Example: copy and move with toArray
toArray copies an immutable Array source and moves a mutable one, which leaves the source empty.
"Array" use
"String" use
"control" use
{} Int32 {} [
numbers: (10 20 30) toArray;
copy: numbers toArray;
moved: @numbers toArray;
("size=" copy.size LF "moved=" moved.size LF "left=" numbers.size LF) printList
0
] "main" exportFunction
Expected Output
size=3
moved=3
left=0
See also
- Span: Contiguous non-owning view of items of the same schema.
- SpanStatic: Contiguous non-owning view of items of the same schema with a schema-level fixed size.
- Deque: Double-ended queue data structure.
- PriorityQueue: Priority queue with customizable comparison.
- algorithm: Collection interfaces, comparison helpers, iteration adapters, and view slicing utilities.