8.18
top
← prev up next →

array: Generic and Dynamic ArraysπŸ”— i

rvs314

(require array ) package: array

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

syntax

gen:array

A generic interface for arrays. The interface defines the following methods:

procedure

( array? obj)boolean?

obj:any/c
Returns #t if the object implements the gen:array interface, #f otherwise.

procedure

( array-set! arrayidxval)void?

array:array?
val:any/c
Sets slot idx of array to be val.

procedure

( array-ref arrayidx)any/c

array:array?
Returns slot idx of array.

array:array?
Returns the number of valid slots in array.

procedure

( array-alloc arraylen)array?

array:array?
Returns a new array of the same type as array with len elements. The elements of the returned array are not defined.

procedure

( array-copy! dest
dest-start
array
[ array-start
array-end])void?
dest:array?
array:array?
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.

procedure

( in-array array)sequence?

array:array?
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.

procedure

( array-empty? array)boolean?

array:array?
Returns #t if array has a length of zero, #f otherwise.

procedure

( array-first array)any/c

array:array?
Returns the element at index zero of array.

procedure

( array-last array)any/c

array:array?
Returns the element at index (sub1 (array-length array)) of array.

procedure

( array->list array)list?

array:array?
Returns the elements of array as a newly allocated list.

procedure

( array->vector array)vector?

array:array?
Returns the elements of array as a newly allocated vector.

2Dynamic ArraysπŸ”— i

struct

(struct dynamic-array(bufferlength))

buffer:array?
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)).

procedure

( dynamic-array?obj)boolean?

obj:any/c
Returns #t if obj is an instance of the dynamic-array structure type, #f otherwise.

procedure

( dynamic-arrayarr[len])dynamic-array?

arr:array?
Returns a new dynamic array.

procedure

( dynamic-array-bufferarray)array?

array:dynamic-array?
Returns the underling buffer of the dynamic array array.

procedure

( dynamic-array-lengtharray)exact-nonnegative-integer?

array:dynamic-array?
Returns the length of the dynamic array array.

procedure

( dynamic-array-capacityarray)exact-nonnegative-integer?

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!.

procedure

( dynamic-array-ensure-capacity array
min-cap)void?
array:dynamic-array?
Grows the underlying buffer of array until it has a capacity of at least min-cap.

procedure

( dynamic-array-append!arraynew-values)void?

array:dynamic-array?
new-values:array?
Pushes an array of new values onto the array. This will cause at most one new allocation.

procedure

( 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.

procedure

( 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.

procedure

( 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.

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /