Array[A: A]

Contiguous, resizable memory to store elements of type A.

class ref Array[A: A] is
  Seq[A] ref

Implements


Constructors

create

Create an array with zero elements, but space for len elements.

new ref create(
  len: USize val = seq)
: Array[A] ref^

Parameters

Returns


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


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 = seq)
: Array[A] ref^

Parameters

Returns


Public Functions

cpointer

Return the underlying C-style pointer.

fun box cpointer(
  offset: USize val = seq)
: Pointer[A] tag

Parameters

  • offset: USize val = seq

Returns


size

The number of elements in the array.

fun box size()
: USize val

Returns


space

The available space in the array.

fun box space()
: USize val

Returns


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

Returns


compact

Try to remove unused space, making it available for garbage collection. The request may be ignored.

fun ref compact()
: None val

Returns


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

Returns


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

Returns

  • this->A ?

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

Returns

  • A^ ?

insert

Insert an element into the array. Elements after this are moved up by one index, extending the array. An out of bounds index raises an error.

fun ref insert(
  i: USize val,
  value: A)
: None val ?

Parameters

Returns


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

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

Returns


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 = seq,
  to: USize val = seq)
: None val

Parameters

Returns


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 = seq,
  to: USize val = seq)
: Array[A] val

Parameters

Returns


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


remove

Remove n elements from the array, beginning at index i.

fun ref remove(
  i: USize val,
  n: USize val)
: None val

Parameters

Returns


clear

Remove all elements from the array.

fun ref clear()
: None val

Returns


push

Add an element to the end of the array.

fun ref push(
  value: A)
: None val

Parameters

  • value: A

Returns


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


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 = seq,
  len: USize val = seq)
: None val

Parameters

Returns


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 = seq,
  len: USize val = seq)
: None val

Parameters

Returns


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 = seq,
  nth: USize val = seq,
  predicate: {(box->A!, box->A!): Bool}[A] val = seq)
: USize val ?

Parameters

  • value: A!
  • offset: USize val = seq
  • nth: USize val = seq
  • predicate: {(box->A!, box->A!): Bool}[A] val = seq

Returns


contains

Returns true if the array contains value, false otherwise.

fun box contains(
  value: A!,
  predicate: {(box->A!, box->A!): Bool}[A] val = seq)
: Bool val

Parameters

  • value: A!
  • predicate: {(box->A!, box->A!): Bool}[A] val = seq

Returns


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 = seq,
  nth: USize val = seq,
  predicate: {(box->A!, box->A!): Bool}[A] val = seq)
: USize val ?

Parameters

  • value: A!
  • offset: USize val = seq
  • nth: USize val = seq
  • predicate: {(box->A!, box->A!): Bool}[A] val = seq

Returns


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


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 = seq,
  to: USize val = seq,
  step: USize val = seq)
: Array[this->A!] ref^

Parameters

Returns


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


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


reverse_in_place

Reverse the array in place.

fun ref reverse_in_place()
: None val

Returns


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


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


pairs

Return an iterator over the (index, value) pairs in the array.

fun box pairs()
: ArrayPairs[A, this->Array[A] ref] ref^

Returns


Private Functions

_copy_to

Copy copy_len elements from this to that at specified offsets.

fun box _copy_to(
  ptr: Pointer[this->A!] ref,
  copy_len: USize val,
  from_offset: USize val = seq,
  to_offset: USize val = seq)
: None val

Parameters

Returns