18 Vectors
A vector is an object that contains a sequence of arbitrary klisp objects. A vector has a length that is fixed at creation time, and as many objects, indexed from 0
to length-1
. Compared to lists, klisp vectors use less size and have constant access time for any element.
Vectors may be mutable or immutable. If an attempt is made to mutate an immutable vector, an error is signaled. Two immutable vectors are “eq?” iff they are “equal?”. Two mutable vectors are “eq?” if they were created by the same constructor call. Two mutable vectors are “equal?” iff they have the same length and have “equal?” objects in each position. As is the case for lists, in order to handle possibly cyclic structures, the “equal?” algorithm considers vectors as FSMs where it position is a state change. There is only one empty vector (that is, a vector of length 0) and that vector is immutable. The vector type is encapsulated.
SOURCE NOTE: The report doesn’t currently include vectors. They are taken from r7rs scheme.
The primitive type predicate for type vector.
vector?
returns true iff all the objects inobjects
are of type vector.
— Applicative: mutable-vector? (mutable-vector? objects)
The primitive type predicates for types immutable vector and mutable vector. These return true iff all the objects in
objects
are of type immutable vector or mutable vector respectively.
Applicative
make-vector
constructs and returns a new mutable vector of lengthk
. Ifobj
is specified, then all objects in the returned vector areobj
, otherwise the content of the vector is unspecified.
Applicative
vector-length
returns the length ofvector
.
Applicative
vector-ref
returns the object ofvector
at positionk
. Ifk
is out of bounds (i.e. less than0
or greater or equal than(vector-length vector)
) an error is signaled.
Applicative
vector-set!
replaces the object with indexk
invector
with objectobj
. Ifk
is out of bounds, orvector
is immutable, an error is signaled. The result returned byvector-set!
is inert.
Applicative
vector
contructs and return a new mutable vector composed of the object arguments.
— Applicative: list->vector (list->vector objs)
These applicatives convert between vectors and lists. The objects returned by these applicatives are always mutable.
Applicative
vector-copy
constructs and returns a new mutable vector with the same length and objects asvector
.
— Applicative: bytevector->vector (bytevector->vector bytevector)
These applicatives convert between vectors and bytevectors. If a vector containing objects other than exact integers between 0 and 255 inclusive are passed to
vector->bytevector
, an error is signaled. The objects returned by these applicatives are always mutable.
— Applicative: string->vector (string->vector string)
These applicatives convert between vectors and strings. If a vector containing objects other than characters is passed to
vector->string
, an error is signaled. The objects returned by these applicatives are always mutable.
vector2 should have a length greater than or equal to that of vector1.
Copies the values in vector1 to the corresponding positions in vector2. If vector2 is immutable, an error is signaled. The result returned by
vector-copy!
is inert.
Both
k1
&k2
should be valid indexes invector
. Also it should be the case thatk1 <= k2
.Applicative
vector-copy-partial
constructs and returns a new mutable vector with lengthk2 - k1
, with the objects fromvector
, starting at indexk1
(inclusive) and ending at indexk2
(exclusive).
Both
k1
&k2-1
should be valid indexes invector1
. Also it should be the case thatk1 <= k2
. Bothk3
&k3 + (k2-k1) - 1
should be valid indexes invector2
.Applicative
vector-copy-partial!
copies objects k1 (inclusive) through k2 (exclusive) fromvector1
to thek2-k1
positions invector2
starting atk3
. Ifvector2
is an immutable vector, an error is signaled. The result returned byvector-copy-partial!
is inert.