array: Generic and Dynamic Arraysπ i
The array module provides a generic interface for the array, a data structure that uses compact, constant-time, natural-number indexing and updating. Racket has many built-in types which act as either homogeneous (all elements have the same type) or heterogeneous (elements can have different types) arrays, such as the vector, byte string, string, flvector, and others. This module also provides an implementation of the dynamic array, an array which grows exponentially, allowing for an amortized O(1) append operation.
1Generic Arraysπ i
Returns
#t if the object implements the
gen:array interface,
#f otherwise.
Sets slot idx of array to be val.
Returns slot idx of array.
Returns the number of valid slots in array.
Returns a new array of the same type as array with len elements. The elements of the returned array are not defined.
dest-start
array
[ array-start
Changes the elements of dest starting at position dest-start to match the elements in array from array-start (inclusive) to array-end (exclusive). There is a fallback implementation for this method.
Returns a sequence containing all the elements of array. There is a fallback implementation for this method. Using this is often faster than indexing each element individually, as it avoids redundant method lookups.
Returns #t if array has a length of zero, #f otherwise.
Returns the element at index zero of array.
Returns the elements of array as a newly allocated list.
Returns the elements of array as a newly allocated vector.
2Dynamic Arraysπ i
(struct dynamic-array(bufferlength))
A
dynamic array (also called an
array-list or
growable array) is an array that can grow in amortized
O(1) time. It’s implemented using a traditional, static
array (called the
dynamic-array-buffer), where only the first
dynamic-array-length elements are meaningful. By increasing the length field of the
dynamic array, we can increase the number of elements until we run out of space in the underlying buffer, in which case we allocate a new underlying buffer and continue. Notice that for a given
dynamic array arr,
(dynamic-array-lengtharr) is not the same as
(array-length (dynamic-array-bufferarr)).
Returns #t if obj is an instance of the dynamic-array structure type, #f otherwise.
( dynamic-arrayarr[len])→dynamic-array?
( dynamic-array-bufferarray)→array?
array:dynamic-array?
Returns the capacity of the
dynamic array array. This is increased automatically by
dynamic-array-append! and
dynamic-array-push! and can be increased manually by calling
dynamic-array-ensure-capacity!.
( dynamic-array-ensure-capacity array
array:dynamic-array?
Grows the underlying buffer of array until it has a capacity of at least min-cap.
( dynamic-array-append!arraynew-values)→void?
array:dynamic-array?
Pushes an
array of new values onto the
array. This will cause at most one new allocation.
( dynamic-array-push!arraynew-value)
array:dynamic-array?
new-value:any/c
Pushes a new values onto the array. This will cause at most one new allocation. Returns the index of the element pushed.
( dynamic-array-pop!array)→any/c
array:dynamic-array?
Returns the last element of
array and decreases the
dynamic-array-length by one. Raises an
exn:fail:contract if the array is empty.
( dynamic-array-contentsarray)→array?
array:dynamic-array?
Returns a newly allocated
array of the same type as
(dynamic-array-bufferarray) with the contents of
array.