3.11 Vectors
Vectors in Guide: Racket introduces vectors.
A vector is a fixed-length array with constant-time access
and update of the vector slots, which are numbered from 0 to
one less than the number of slots in the vector.
Two vectors are equal? if they have the same length, and if
the values in corresponding slots of the vectors are
equal?.
A vector can be mutable or immutable. When an
immutable vector is provided to a procedure like vector-set!,
the exn:fail:contract exception is raised. Vectors generated by the default
reader (see Reading Strings) are immutable.
A vector can be used as a single-valued sequence (see
Sequences). The elements of the vector serve as elements
of the sequence. See also in-vector.
Returns #t if v is a vector, #f otherwise.
Returns a mutable vector with size slots, where all slots are
initialized to contain v.
Returns a newly allocated mutable vector with as many slots as provided vs,
where the slots are initialized to contain the given vs in
order.
Returns a newly allocated immutable vector with as many slots as provided
vs, where the slots are contain the given vs in
order.
Returns the length of vec (i.e., the number of slots in the
vector).
Returns the element in slot
pos of
vec. The first
slot is position
0, and the last slot is one less than
(vector-length vec).
Updates the slot pos of vec to contain v.
Returns a list with the same length and elements as vec.
Returns a mutable vector with the same length and elements as
lst.
Returns an immutable vector with the same length and elements as vec.
If vec is itself immutable, then it is returned as the result.
Changes all slots of vec to contain v.
Changes the elements of
dest starting at position
dest-start to match the elements in
src from
src-start (inclusive) to
src-end (exclusive). The
vectors
dest and
src can be the same vector, and in
that case the destination region can overlap with the source region;
the destination elements after the copy match the source elements
from before the copy. If any of
dest-start,
src-start, or
src-end are out of range (taking into
account the sizes of the vectors and the source and destination
regions), the
exn:fail:contract exception is raised.
Returns
end-pos - start-pos values, which are
the elements of
vec from
start-pos (inclusive) to
end-pos (exclusive). If
start-pos or
end-pos are greater than
(vector-length vec), or if
end-pos is less than
start-pos, the
exn:fail:contract exception is raised.
Creates a vector of
n elements by applying
proc to
the integers from
0 to
(sub1 n) in order. If
vec is the resulting vector, then
(vector-ref vec i) is the value produced by
(proc i).
3.11.1 Additional Vector Functions
Updates each slot pos of vec to contain each v.
The update takes place from the left so later updates overwrite earlier updates.
Applies proc to the elements of the vecs from the
first elements to the last. The proc argument must accept
the same number of arguments as the number of supplied vecs,
and all vecs must have the same number of elements. The
result is a fresh vector containing each result of proc in
order.
Applies proc to the elements of the vecs from the
first elements to the last. The proc argument must accept
the same number of arguments as the number of supplied vecs,
and all vecs must have the same number of elements. The
each result of proc is inserted into the first vec
at the index that the arguments to proc was taken from. The
result is the first vec.
Creates a fresh vector that contains all
of the elements of the given vectors in order.
Returns a fresh vector whose elements are the first
pos elements of
vec. If
vec has fewer than
pos elements, then the
exn:fail:contract exception is raised.
Returns a fresh vector whose elements are the last
pos elements of
vec. If
vec has fewer than
pos elements, then the
exn:fail:contract exception is raised.
Returns a fresh vector whose elements are the elements of
vec
after the first
pos elements. If
vec has fewer
than
pos elements, then the
exn:fail:contract exception is raised.
Returns a fresh vector whose elements are the elements of
vec
before the first
pos elements. If
vec has fewer
than
pos elements, then the
exn:fail:contract exception is raised.
Returns the same result as
(values (vector-take vec pos) (vector-drop vec pos))
except that it can be faster.
Returns the same result as
(values (vector-take-right vec pos) (vector-drop-right vec pos))
except that it can be faster.
Creates a fresh vector of size
(- end start), with all of the
elements of
vec from
start (inclusive) to
end (exclusive).
Returns a fresh vector with the elements of vec for which
pred produces a true value. The pred procedure is
applied to each element from first to last.
Like
vector-filter, but the meaning of the
pred predicate
is reversed: the result is a vector of all items for which
pred
returns
#f.
Returns the number of elements of the
vec ... (taken in
parallel) on which
proc does not evaluate to
#f.
This returns the first element in the non-empty vector vec that minimizes
the result of proc.
This returns the first element in the non-empty vector vec that maximizes
the result of proc.
Locates the first element of
vec that is
equal? to
v. If such an element exists, the index of that element in
vec is returned. Otherwise, the result is
#f.