Array[A: A]
Contiguous, resizable memory to store elements of type A.
Usage
Creating an Array of String:
let array: Array[String] = ["dog"; "cat"; "wombat"] // array.size() == 3 // array.space() >= 3
Creating an empty Array of String, which may hold at least 10 elements before requesting more space:
let array = Array[String](10) // array.size() == 0 // array.space() >= 10
Accessing elements can be done via the apply(i: USize): this->A ?
method. The provided index might be out of bounds so apply
is partial and has to be called within a try-catch block or inside another partial method:
let array: Array[String] = ["dog"; "cat"; "wombat"] let is_second_element_wobat = try // indexes start from 0, so 1 is the second element array(1)? == "wombat" else false end
Adding and removing elements to and from the end of the Array can be done via push
and pop
methods. You could treat the array as a LIFO stack using those methods:
while (array.size() > 0) do let elem = array.pop()? // do something with element end
Modifying the Array can be done via update
, insert
and delete
methods which alter the Array at an arbitrary index, moving elements left (when deleting) or right (when inserting) as necessary.
Iterating over the elements of an Array can be done using the values
method:
for element in array.values() do // do something with element end
Memory allocation
Array allocates contiguous memory. It always allocates at least enough memory space to hold all of its elements. Space is the number of elements the Array can hold without allocating more memory. The space()
method returns the number of elements an Array can hold. The size()
method returns the number of elements the Array holds.
Different data types require different amounts of memory. Array[U64] with size of 6 will take more memory than an Array[U8] of the same size.
When creating an Array or adding more elements will calculate the next power of 2 of the requested number of elements and allocate that much space, with a lower bound of space for 8 elements.
Here's a few examples of the space allocated when initialising an Array with various number of elements:
size | space |
---|---|
0 | 0 |
1 | 8 |
8 | 8 |
9 | 16 |
16 | 16 |
17 | 32 |
Call the compact()
method to ask the GC to reclaim unused space. There are no guarantees that the GC will actually reclaim any space.
class ref Array[A: A] is Seq[A] ref
Implements
- Seq[A] ref
Constructors
create
Create an array with zero elements, but space for len elements.
new ref create( len: USize val = 0) : Array[A] ref^
Parameters
- len: USize val = 0
Returns
- Array[A] ref^
init
Create an array of len elements, all initialised to the given value.
new ref init( from: A^, len: USize val) : Array[A] ref^
Parameters
- from: A^
- len: USize val
Returns
- Array[A] ref^
from_cpointer
Create an array from a C-style pointer and length. The contents are not copied.
new ref from_cpointer( ptr: Pointer[A] ref, len: USize val, alloc: USize val = 0) : Array[A] ref^
Parameters
Returns
- Array[A] ref^
Public Functions
cpointer
Return the underlying C-style pointer.
fun box cpointer( offset: USize val = 0) : Pointer[A] tag
Parameters
- offset: USize val = 0
Returns
- Pointer[A] tag
size
The number of elements in the array.
fun box size() : USize val
Returns
- USize val
space
The available space in the array.
fun box space() : USize val
Returns
- USize val
reserve
Reserve space for len elements, including whatever elements are already in the array. Array space grows geometrically.
fun ref reserve( len: USize val) : None val
Parameters
- len: USize val
Returns
- None val
compact
Try to remove unused space, making it available for garbage collection. The request may be ignored.
fun ref compact() : None val
Returns
- None val
undefined[optional B: (A & Real[B] val & (I8 val | I16 val | I32 val | I64 val | I128 val | ILong val | ISize val | U8 val | U16 val | U32 val | U64 val | U128 val | ULong val | USize val | F32 val | F64 val))]
Resize to len elements, populating previously empty elements with random memory. This is only allowed for an array of numbers.
fun ref undefined[optional B: (A & Real[B] val & (I8 val | I16 val | I32 val | I64 val | I128 val | ILong val | ISize val | U8 val | U16 val | U32 val | U64 val | U128 val | ULong val | USize val | F32 val | F64 val))]( len: USize val) : None val
Parameters
- len: USize val
Returns
- None val
read_u8[optional B: (A & Real[B] val & U8 val)]
Reads a U8 from offset. This is only allowed for an array of U8s.
fun box read_u8[optional B: (A & Real[B] val & U8 val)]( offset: USize val) : U8 val ?
Parameters
- offset: USize val
Returns
- U8 val ?
read_u16[optional B: (A & Real[B] val & U8 val)]
Reads a U16 from offset. This is only allowed for an array of U8s.
fun box read_u16[optional B: (A & Real[B] val & U8 val)]( offset: USize val) : U16 val ?
Parameters
- offset: USize val
Returns
- U16 val ?
read_u32[optional B: (A & Real[B] val & U8 val)]
Reads a U32 from offset. This is only allowed for an array of U8s.
fun box read_u32[optional B: (A & Real[B] val & U8 val)]( offset: USize val) : U32 val ?
Parameters
- offset: USize val
Returns
- U32 val ?
read_u64[optional B: (A & Real[B] val & U8 val)]
Reads a U64 from offset. This is only allowed for an array of U8s.
fun box read_u64[optional B: (A & Real[B] val & U8 val)]( offset: USize val) : U64 val ?
Parameters
- offset: USize val
Returns
- U64 val ?
read_u128[optional B: (A & Real[B] val & U8 val)]
Reads a U128 from offset. This is only allowed for an array of U8s.
fun box read_u128[optional B: (A & Real[B] val & U8 val)]( offset: USize val) : U128 val ?
Parameters
- offset: USize val
Returns
- U128 val ?
apply
Get the i-th element, raising an error if the index is out of bounds.
fun box apply( i: USize val) : this->A ?
Parameters
- i: USize val
Returns
- this->A ?
update_u8[optional B: (A & Real[B] val & U8 val)]
Write a U8 at offset. This is only allowed for an array of U8s.
fun ref update_u8[optional B: (A & Real[B] val & U8 val)]( offset: USize val, value: U8 val) : U8 val ?
Parameters
Returns
- U8 val ?
update_u16[optional B: (A & Real[B] val & U8 val)]
Write a U16 at offset. This is only allowed for an array of U8s.
fun ref update_u16[optional B: (A & Real[B] val & U8 val)]( offset: USize val, value: U16 val) : U16 val ?
Parameters
Returns
- U16 val ?
update_u32[optional B: (A & Real[B] val & U8 val)]
Write a U32 at offset. This is only allowed for an array of U8s.
fun ref update_u32[optional B: (A & Real[B] val & U8 val)]( offset: USize val, value: U32 val) : U32 val ?
Parameters
Returns
- U32 val ?
update_u64[optional B: (A & Real[B] val & U8 val)]
Write a U64 at offset. This is only allowed for an array of U8s.
fun ref update_u64[optional B: (A & Real[B] val & U8 val)]( offset: USize val, value: U64 val) : U64 val ?
Parameters
Returns
- U64 val ?
update_u128[optional B: (A & Real[B] val & U8 val)]
Write a U128 at offset. This is only allowed for an array of U8s.
fun ref update_u128[optional B: (A & Real[B] val & U8 val)]( offset: USize val, value: U128 val) : U128 val ?
Parameters
Returns
- U128 val ?
update
Change the i-th element, raising an error if the index is out of bounds.
fun ref update( i: USize val, value: A) : A^ ?
Parameters
- i: USize val
- value: A
Returns
- A^ ?
insert
Insert an element into the array. Elements after this are moved up by one index, extending the array.
When inserting right beyond the last element, at index this.size()
, the element will be appended, similar to push()
, an insert at index 0
prepends the value to the array. An insert into an index beyond this.size()
raises an error.
let array = Array[U8](4) // [] array.insert(0, 0xDE)? // prepend: [0xDE] array.insert(array.size(), 0xBE)? // append: [0xDE; 0xBE] array.insert(1, 0xAD)? // insert: [0xDE; 0xAD; 0xBE] array.insert(array.size() + 1, 0xEF)? // error
fun ref insert( i: USize val, value: A) : None val ?
Parameters
- i: USize val
- value: A
Returns
- None val ?
delete
Delete an element from the array. Elements after this are moved down by one index, compacting the array. An out of bounds index raises an error. The deleted element is returned.
fun ref delete( i: USize val) : A^ ?
Parameters
- i: USize val
Returns
- A^ ?
truncate
Truncate an array to the given length, discarding excess elements. If the array is already smaller than len, do nothing.
fun ref truncate( len: USize val) : None val
Parameters
- len: USize val
Returns
- None val
trim_in_place
Trim the array to a portion of itself, covering from
until to
. Unlike slice, the operation does not allocate a new array nor copy elements.
fun ref trim_in_place( from: USize val = 0, to: USize val = call) : None val
Parameters
Returns
- None val
trim
Return a shared portion of this array, covering from
until to
. Both the original and the new array are immutable, as they share memory. The operation does not allocate a new array pointer nor copy elements.
fun val trim( from: USize val = 0, to: USize val = call) : Array[A] val
Parameters
Returns
- Array[A] val
chop[optional B: (A & Any #send)]
Chops the array in half at the split point requested and returns both the left and right portions. The original array is trimmed in place and returned as the left portion. If the split point is larger than the array, the left portion is the original array and the right portion is a new empty array. The operation does not allocate a new array pointer nor copy elements.
The entry type must be sendable so that the two halves can be isolated. Otherwise, two entries may have shared references to mutable data, or even to each other, such as in the code below:
class Example var other: (Example | None) = None let arr: Array[Example] iso = recover let obj1 = Example let obj2 = Example obj1.other = obj2 obj2.other = obj1 [obj1; obj2] end
fun iso chop[optional B: (A & Any #send)]( split_point: USize val) : (Array[A] iso^ , Array[A] iso^)
Parameters
- split_point: USize val
Returns
unchop
Unchops two iso arrays to return the original array they were chopped from. Both input arrays are isolated and mutable and were originally chopped from a single array. This function checks that they are indeed two arrays chopped from the same original array and can be unchopped before doing the unchopping and returning the unchopped array. If the two arrays cannot be unchopped it returns both arrays without modifying them. The operation does not allocate a new array pointer nor copy elements.
fun iso unchop( b: Array[A] iso) : ((Array[A] iso^ , Array[A] iso^) | Array[A] iso^)
Parameters
- b: Array[A] iso
Returns
copy_from[optional B: (A & Real[B] val & U8 val)]
Copy len elements from src(src_idx) to this(dst_idx). Only works for Array[U8].
fun ref copy_from[optional B: (A & Real[B] val & U8 val)]( src: Array[U8 val] box, src_idx: USize val, dst_idx: USize val, len: USize val) : None val
Parameters
Returns
- None val
copy_to
Copy len elements from this(src_idx) to dst(dst_idx).
fun box copy_to( dst: Array[this->A!] ref, src_idx: USize val, dst_idx: USize val, len: USize val) : None val
Parameters
Returns
- None val
remove
Remove n elements from the array, beginning at index i.
fun ref remove( i: USize val, n: USize val) : None val
Parameters
Returns
- None val
clear
Remove all elements from the array.
fun ref clear() : None val
Returns
- None val
push_u8[optional B: (A & Real[B] val & U8 val)]
Add a U8 to the end of the array. This is only allowed for an array of U8s.
fun ref push_u8[optional B: (A & Real[B] val & U8 val)]( value: U8 val) : None val
Parameters
- value: U8 val
Returns
- None val
push_u16[optional B: (A & Real[B] val & U8 val)]
Add a U16 to the end of the array. This is only allowed for an array of U8s.
fun ref push_u16[optional B: (A & Real[B] val & U8 val)]( value: U16 val) : None val
Parameters
- value: U16 val
Returns
- None val
push_u32[optional B: (A & Real[B] val & U8 val)]
Add a U32 to the end of the array. This is only allowed for an array of U8s.
fun ref push_u32[optional B: (A & Real[B] val & U8 val)]( value: U32 val) : None val
Parameters
- value: U32 val
Returns
- None val
push_u64[optional B: (A & Real[B] val & U8 val)]
Add a U64 to the end of the array. This is only allowed for an array of U8s.
fun ref push_u64[optional B: (A & Real[B] val & U8 val)]( value: U64 val) : None val
Parameters
- value: U64 val
Returns
- None val
push_u128[optional B: (A & Real[B] val & U8 val)]
Add a U128 to the end of the array. This is only allowed for an array of U8s.
fun ref push_u128[optional B: (A & Real[B] val & U8 val)]( value: U128 val) : None val
Parameters
- value: U128 val
Returns
- None val
push
Add an element to the end of the array.
fun ref push( value: A) : None val
Parameters
- value: A
Returns
- None val
pop
Remove an element from the end of the array. The removed element is returned.
fun ref pop() : A^ ?
Returns
- A^ ?
unshift
Add an element to the beginning of the array.
fun ref unshift( value: A) : None val
Parameters
- value: A
Returns
- None val
shift
Remove an element from the beginning of the array. The removed element is returned.
fun ref shift() : A^ ?
Returns
- A^ ?
append
Append the elements from a sequence, starting from the given offset.
fun ref append( seq: (ReadSeq[A] box & ReadElement[A^] box), offset: USize val = 0, len: USize val = call) : None val
Parameters
- seq: (ReadSeq[A] box & ReadElement[A^] box)
- offset: USize val = 0
- len: USize val = call
Returns
- None val
concat
Add len iterated elements to the end of the array, starting from the given offset.
fun ref concat( iter: Iterator[A^] ref, offset: USize val = 0, len: USize val = call) : None val
Parameters
Returns
- None val
find
Find the nth
appearance of value
from the beginning of the array, starting at offset
and examining higher indices, and using the supplied predicate
for comparisons. Returns the index of the value, or raise an error if the value isn't present.
By default, the search starts at the first element of the array, returns the first instance of value
found, and uses object identity for comparison.
fun box find( value: A!, offset: USize val = 0, nth: USize val = 0, predicate: {(box->A!, box->A!): Bool}[A] val = lambda) : USize val ?
Parameters
- value: A!
- offset: USize val = 0
- nth: USize val = 0
- predicate: {(box->A!, box->A!): Bool}[A] val = lambda
Returns
- USize val ?
contains
Returns true if the array contains value
, false otherwise.
The default predicate checks for matches by identity. To search for matches by structural equality, pass an object literal such as {(l, r) => l == r}
.
fun box contains( value: A!, predicate: {(box->A!, box->A!): Bool}[A] val = lambda) : Bool val
Parameters
- value: A!
- predicate: {(box->A!, box->A!): Bool}[A] val = lambda
Returns
- Bool val
rfind
Find the nth
appearance of value
from the end of the array, starting at offset
and examining lower indices, and using the supplied predicate
for comparisons. Returns the index of the value, or raise an error if the value isn't present.
By default, the search starts at the last element of the array, returns the first instance of value
found, and uses object identity for comparison.
fun box rfind( value: A!, offset: USize val = call, nth: USize val = 0, predicate: {(box->A!, box->A!): Bool}[A] val = lambda) : USize val ?
Parameters
- value: A!
- offset: USize val = call
- nth: USize val = 0
- predicate: {(box->A!, box->A!): Bool}[A] val = lambda
Returns
- USize val ?
clone
Clone the array. The new array contains references to the same elements that the old array contains, the elements themselves are not cloned.
fun box clone() : Array[this->A!] ref^
Returns
- Array[this->A!] ref^
slice
Create a new array that is a clone of a portion of this array. The range is exclusive and saturated. The new array contains references to the same elements that the old array contains, the elements themselves are not cloned.
fun box slice( from: USize val = 0, to: USize val = call, step: USize val = 1) : Array[this->A!] ref^
Parameters
Returns
- Array[this->A!] ref^
permute
Create a new array with the elements permuted. Permute to an arbitrary order that may include duplicates. An out of bounds index raises an error. The new array contains references to the same elements that the old array contains, the elements themselves are not copied.
fun box permute( indices: Iterator[USize val] ref) : Array[this->A!] ref^ ?
Parameters
Returns
- Array[this->A!] ref^ ?
reverse
Create a new array with the elements in reverse order. The new array contains references to the same elements that the old array contains, the elements themselves are not copied.
fun box reverse() : Array[this->A!] ref^
Returns
- Array[this->A!] ref^
reverse_in_place
Reverse the array in place.
fun ref reverse_in_place() : None val
Returns
- None val
swap_elements
Swap the element at index i with the element at index j. If either i or j are out of bounds, an error is raised.
fun ref swap_elements( i: USize val, j: USize val) : None val ?
Parameters
Returns
- None val ?
keys
Return an iterator over the indices in the array.
fun box keys() : ArrayKeys[A, this->Array[A] ref] ref^
Returns
values
Return an iterator over the values in the array.
fun box values() : ArrayValues[A, this->Array[A] ref] ref^
Returns
- ArrayValues[A, this->Array[A] ref] ref^
pairs
Return an iterator over the (index, value) pairs in the array.
fun box pairs() : ArrayPairs[A, this->Array[A] ref] ref^
Returns
- ArrayPairs[A, this->Array[A] ref] ref^
© 2016-2020, The Pony Developers
© 2014-2015, Causality Ltd.
Licensed under the BSD 2-Clause License.
https://stdlib.ponylang.io/builtin-Array