## SRFI 214: Flexvectors

## TOC »

### Author

Adam Nelson

### Maintainer

Sören Tempel

### Repositories

- Canonical SRFI-214 repository: https://github.com/scheme-requests-for-implementation/srfi-214
- SRFI-214 packaged as a CHICKEN egg: https://github.com/nmeum/srfi-214 (the egg comes from this repository)

### Status

This SRFI is currently in *final* status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to `srfi+minus+214+at+srfi+dotschemers+dot+org`. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.

- Received: 2020-10-06
- Draft #1 published: 2020-10-07
- Draft #2 published: 2021-01-13
- Draft #3 published: 2021-03-11
- Finalized: 2021-03-18

### Abstract

A *flexvector*, also known as a dynamic array or an arraylist, is a mutable vector-like data structure with an adjustable size. Flexvectors allow fast random access and fast insertion/removal at the end. This SRFI defines a suite of operations on flexvectors, modeled after SRFI 133's vector operations.

### Rationale

Unlike the default vector data type of many other languages, Scheme vectors have a fixed length. This makes vectors unusable as mutable stacks or queues, and is the reason that SRFI 133 lacks common collection operations like `filter`. In fact, no Scheme standard defines a mutable data type that is suitable for this very common purpose, analogous to a Java `ArrayList` or to the default list data structure in JavaScript or Python. SRFI 117 defines the commonly-used "tconc" mutable queue, but it is a linked list. And SRFI 134 defines a deque data type, but that data type is immutable. Neither data structure has the (often useful) properties of being a mutable, contiguous, random-access sequence. This SRFI does not define a comparator or any sorting procedures, in order to ensure that it has no dependencies. These may be defined in future SRFIs.

#### Terminology

What this SRFI calls a *flexvector* is better known as a *dynamic array*. This data structure has a wide variety of names in different languages:

- JavaScript and Ruby call it an
*array* - Python calls it a
*list* - Java calls it an
`ArrayList`(and, before that, it was called a`Vector`)

Scheme literature already uses the terms *list* (for `cons` lists), *vector* (for fixed-length vectors), and *array* (for fixed-length numeric arrays), so a new term is needed. Because *array* in Scheme refers to a numeric array, the term *dynamic array* is not ideal. *Dynamic vector* is a possibility, but `dynamic-vector` would be an unwieldy prefix due to its length. The newly-minted term *flexvector* communicates this data structure's flexible size and its relationship to Scheme vectors, while being only as long as an existing Scheme data type name (`bytevector`).

#### Procedure inclusion and naming

This SRFI is primarily modeled on SRFI 133. It includes flexvector equivalents of all SRFI 133 procedures, most with the same names and argument order. There are three notable exceptions:

`flexvector-unfold`mimics the API of SRFI 1's`unfold`, not SRFI 133's`vector-unfold`.`vector-unfold`is limited by the necessity of a fixed vector length, while`flexvector-unfold`may generate a flexvector of any length, and so the`unfold`API is more useful.- There is no flexvector equivalent of
`vector-unfold!`because flexvectors use the list version of`unfold`, which has no`unfold!`equivalent with a similar API. - The flexvector equivalent of
`vector=`is`flexvector=?`. It is conventional for Scheme equality predicates to end in`=?`(e.g.,`symbol=?`,`string=?`), and most data structure SRFIs follow this convention (see SRFI 113, 125, 146). This SRFI follows established convention, even when it does not match SRFI 133's procedure names.

Additionally, this SRFI includes deque-like operations that reference, add to, and remove from both ends of a flexvector. The naming convention for these operations is taken from SRFI 134, which uses the terms *front* and *back*. *Front* refers to the start of the flexvector (index 0), while *back* refers to the end (index `(- (flexvector-length x) 1)`).

### Specification

Flexvectors have the same random-access performance guarantees as ordinary vectors. In particular, if a given Scheme implements vectors with contiguous memory locations and O(1) random access and mutation, flexvectors must also have these performance characteristics. Additionally, appending to the back of a flexvector has the same (amortized) performance as setting an existing location in the same flexvector. In this section, the following notation is used to specify parameters and examples:

- Parameters named
`fv`are always flexvectors. `x ...`indicates that zero or more of the parameter`x`are allowed.`[x]`indicates that the parameter`x`is optional. Multiple optional parameters may be written`[x [y]]`.`;=>`separates an example expression from its expected result.- {{λ}} is a shorthand alias for
`lambda`.

Additionally, examples include literal flexvector values written in pseudo-lexical syntax: `#<flexvector a b c>` is a flexvector of length 3 containing the symbol values `a`, `b`, and `c`. **This syntax is only used for example purposes.** This SRFI does not define the `#<flexvector ...>` syntax for actual use.

#### Constructors

`make-flexvector``size``#!optional``fill`procedureCreates and returns a flexvector of size

`size`. If`fill`is specified, all of the elements of the vector are initialized to`fill`. Otherwise, their contents are indeterminate.

`flexvector``x``...`procedureCreates and returns a flexvector whose elements are

`x ...`.

`flexvector-unfold``p``f``g``initial-seed``...`procedureThe fundamental flexvector constructor.

`flexvector-unfold`is modeled on SRFI 1`unfold`instead of SRFI 133`vector-unfold`because flexvectors are not limited to a predetermined length. For each step,`flexvector-unfold`evaluates`p`on the seed value(s) to determine whether it should stop unfolding. If`p`returns`#f`, it then evaluates`f`on the seed value(s) to produce the next element, then evaluates`g`on the seed value(s) to produce the seed value(s) for the next step. The recursion can be described with this algorithm: This is guaranteed to build a flexvector in O(n) if`flexvector-add-back!`is O(1).`flexvector-unfold-right`is a variant that constructs a flexvector right-to-left, and uses`flexvector-add-front!`instead, which may be slower than O(n).

`flexvector-copy``fv``#!optional``start``end`procedureAllocates a new flexvector whose length is

`(- end start)`and fills it with elements from`fv`, taking elements from`fv`starting at index`start`and stopping at index`end`.`start`defaults to`0`and`end`defaults to the value of`(flexvector-length fv)`.`flexvector-reverse-copy`is the same, but copies the elements in reverse order from`fv`. Both`start`and`end`are clamped to the range [0,`(flexvector-length fv)`). It is an error if`end`is less than`start`.`flexvector-copy`shares the performance characteristics of`vector-copy`— in particular, if a given Scheme's`vector-copy`uses a fast`memcpy`operation instead of an element-by-element loop,`flexvector-copy`should also use this operation.

`flexvector-append``fv``...`procedureReturns a newly allocated flexvector that contains all elements in order from the subsequent locations in

`fv ...`.

`flexvector-concatenate``list-of-flexvectors`procedureEquivalent to

`(apply flexvector-append list-of-flexvectors)`, but may be implemented more efficiently.

`(flexvector-append-subvectors [fv start end] ...)`procedureReturns a vector that contains every element of each

`fv`from`start`to`end`in the specified order. This procedure is a generalization of`flexvector-append`.

#### Predicates

`flexvector?``x`procedureDisjoint type predicate for flexvectors: this returns

`#t`if`x`is a flexvector, and`#f`otherwise.

`flexvector-empty?``fv`procedureReturns

`#t`if`fv`is empty (i.e., its length is`0`), and`#f`if not.

`flexvector=?``elt=?``fv``...`procedureFlexvector structural equality predicate, generalized across user-specified element equality predicates. Flexvectors

`a`and`b`are considered equal by`flexvector=?`iff their lengths are the same and, for each index`i`less than`(flexvector-length a)`,`(elt=? (flexvector-ref a i) (flexvector-ref b i))`is true.`elt=?`is always applied to two arguments.`flexvector=?`returns`#t`if it is passed zero or one`fv`arguments. The execution order of comparisons is intentionally left unspecified.

#### Selectors

`flexvector-ref``fv``i`procedureFlexvector element dereferencing: returns the value at location

`i`in`fv`. Indexing is zero-based. It is an error if`i`is outside the range [0,`(flexvector-length fv)`).`flexvector-ref`has the same computational complexity as`vector-ref`. In most Schemes, it will be O(1).

`flexvector-front``fv`procedureReturns the first element in

`fv`. It is an error if`fv`is empty. Alias for`(flexvector-ref fv 0)`.

`flexvector-back``fv`procedureReturns the last element in

`fv`. It is an error if`fv`is empty. Alias for`(flexvector-ref fv (- (flexvector-length fv) 1))`.

`flexvector-length``fv`procedureReturns the length of

`fv`, which is the number of elements contained in`fv`.`flexvector-length`has the same computational complexity as`vector-length`. In most Schemes, it will be O(1).

#### Mutators

`flexvector-add!``fv``i``x``...`procedureInserts the elements

`x ...`into`fv`at the location`i`, preserving their order and shifting all elements after`i`backward to make room. This increases the length of`fv`by the number of elements inserted. It is an error if`i`is outside the range [0,`(flexvector-length fv)`].`flexvector-add!`returns`fv`after mutating it.

`flexvector-add-front!``fv``x``...`procedureInserts the elements

`x ...`into the front or back of`fv`, preserving their order. This increases the length of`fv`by the number of elements inserted.`flexvector-add-back!`of one element has the same computational complexity as`vector-set!`, amortized. In most Schemes, this will be amortized O(1). These procedures return`fv`after mutating it.

`flexvector-add-all!``fv``i``xs`procedureInserts the elements of the list

`xs`into`fv`at location`i`. Equivalent to`(apply flexvector-add! fv i xs)`. Returns`fv`after mutating it.

`flexvector-append!``fv1``fv2``...`procedureInserts the elements of the flexvectors

`fv2 ...`at the end of the flexvector`fv1`, in order. Returns`fv1`after mutating it.

`flexvector-remove!``fv``i`procedureRemoves and returns the element at

`i`in`fv`, then shifts all subsequent elements forward, reducing the length of`fv`by 1. It is an error if`i`is outside the range [0,`(flexvector-length fv)`).

`flexvector-remove-front!``fv`procedureRemoves and returns the first element from

`fv`, then shifts all other elements forward.`flexvector-remove-back!`instead removes the last element, without moving any other elements, and has the same performance guarantees as`flexvector-add-back!`. It is an error if`fv`is empty.

`flexvector-remove-range!``fv``start``#!optional``end`procedureRemoves all elements from

`fv`between`start`and`end`, shifting all elements after`end`forward by`(- end start)`. If`end`is not present, it defaults to`(flexvector-length fv)`. Both`start`and`end`are clamped to the range [0,`(flexvector-length fv)`). It is an error if`end`is less than`start`.`flexvector-remove-range!`returns`fv`after mutating it.

`flexvector-clear!``fv`procedureRemoves all items from

`fv`, setting its length to 0. Returns`fv`after mutating it.

`flexvector-set!``fv``i``x`procedureAssigns the value of

`x`to the location`i`in`fv`. It is an error if`i`is outside the range [0,`(flexvector-length fv)`]. If`i`is equal to`(flexvector-length fv)`,`x`is appended after the last element in`fv`; this is equivalent to`flexvector-add-back!`. Returns the previous value at location`i`in`fv`, or an unspecified value if`i`is equal to`(flexvector-length fv)`.`flexvector-set!`has the same computational complexity as`vector-set!`. In most Schemes, it will be O(1).

`flexvector-swap!``fv``i``j`procedureSwaps or exchanges the values of the locations in

`fv`at indexes`i`and`j`. It is an error if either`i`or`j`is outside the range [0,`(flexvector-length fv)`). Returns`fv`after mutating it.

`flexvector-fill!``fv``fill``#!optional``start``end`procedureAssigns the value of every location in

`fv`between`start`, which defaults to 0 and`end`, which defaults to the length of`fv`, to`fill`. Returns`fv`after mutating it. Both`start`and`end`are clamped to the range [0,`(flexvector-length fv)`]. It is an error if`end`is less than`start`.

`flexvector-reverse!``fv`procedureDestructively reverses

`fv`in-place. Returns`fv`after mutating it.

`flexvector-copy!``to``at``from``#!optional``start``end`procedureCopies the elements of flexvector

`from`between`start`and`end`to flexvector`to`, starting at`at`. The order in which elements are copied is unspecified, except that if the source and destination overlap, copying takes place as if the source is first copied into a temporary vector and then into the destination. This can be achieved without allocating storage by making sure to copy in the correct direction in such circumstances.`flexvector-reverse-copy!`is the same, but copies elements in reverse order.`start`and`end`default to 0 and`(flexvector-length from)`if not present. Both`start`and`end`are clamped to the range [0,`(flexvector-length from)`]. It is an error if`end`is less than`start`. Unlike`vector-copy!`,`flexvector-copy!`may copy elements past the end of`to`, which will increase the length of`to`.`flexvector-copy!`shares the performance characteristics of`vector-copy!`— in particular, if a given Scheme's`vector-copy!`uses a fast`memcpy`operation instead of an element-by-element loop,`flexvector-copy!`should also use this operation. Both procedures return`to`after mutating it.

#### Iteration

`flexvector-fold``kons``knil``fv1``fv2``...`procedureThe fundamental flexvector iterator.

`kons`is iterated over each value in all of the vectors, stopping at the end of the shortest;`kons`is applied as`(kons state (flexvector-ref fv1 i) (flexvector-ref fv2 i) ...)`where`state`is the current state value—the current state value begins with`knil`, and becomes whatever`kons`returned on the previous iteration—and`i`is the current index. The iteration of`flexvector-fold`is strictly left-to-right. The iteration of`flexvector-fold-right`is strictly right-to-left.

`flexvector-map``f``fv1``fv2``...`procedureConstructs a new flexvector of the shortest size of the flexvector arguments. Each element at index

`i`of the new flexvector is mapped from the old flexvectors by`(f (flexvector-ref fv1 i) (flexvector-ref fv2 i) ...)`. The dynamic order of application of`f`is unspecified.`flexvector-map/index`is a variant that passes`i`as the first argument to`f`for each element.

`flexvector-map!``f``fv1``fv2``...`procedureSimilar to

`flexvector-map`, but rather than mapping the new elements into a new flexvector, the new mapped elements are destructively inserted into`fv1`. Again, the dynamic order of application of`f`is unspecified, so it is dangerous for`f`to apply either`flexvector-ref`or`flexvector-set!`to`fv1`in`f`.

`flexvector-append-map``f``fv1``fv2``...`procedureConstructs a new flexvector by appending the results of each call to

`f`on the elements of the flexvectors`fv1`,`fv2`, etc., in order. Each call is of the form`(f (flexvector-ref fv1 i) (flexvector-ref fv2 i) ...)`. Iteration stops when the end of the shortest flexvector argument is reached. The dynamic order of application of`f`is unspecified.`f`must return a flexvector. It is an error if`f`returns anything else.`flexvector-append-map/index`is a variant that passes the index`i`as the first argument to`f`for each element.

`flexvector-filter``pred?``fv`procedureConstructs a new flexvector consisting of only the elements of

`fv`for which`pred?`returns a non-`#f`value.`flexvector-filter/index`passes the index of each element as the first argument to`pred?`, and the element itself as the second argument.

`flexvector-filter!``pred?``fv`procedureSimilar to

`flexvector-filter`, but destructively updates`fv`by removing all elements for which`pred?`returns`#f`.`flexvector-filter/index!`passes the index of each element as the first argument to`pred?`, and the element itself as the second argument.

`flexvector-for-each``f``fv1``fv2``...`procedureSimple flexvector iterator: applies

`f`to the corresponding list of parallel elements from`fv1 fv2 ...`in the range [0,`length`), where`length`is the length of the smallest flexvector argument passed. In contrast with`flexvector-map`,`f`is reliably applied in left-to-right order, starting at index 0, in the flexvectors.`flexvector-for-each/index`is a variant that passes the index as the first argument to`f`for each element.

`flexvector-count``pred?``fv1``fv2``...`procedureCounts the number of parallel elements in the flexvectors that satisfy

`pred?`, which is applied, for each index`i`in the range [0,*length*) where*length*is the length of the smallest flexvector argument, to each parallel element in the flexvectors, in order.

`flexvector-cumulate``f``knil``fv`procedureReturns a newly-allocated flexvector

`new`with the same length as`fv`. Each element`i`of`new`is set to the result of`(f (flexvector-ref new (- i 1)) (flexvector-ref fv i))`, except that, for the first call on`f`, the first argument is`knil`. The`new`flexvector is returned.

#### Searching

`flexvector-index``pred?``fv1``fv2``...`procedureFinds and returns the index of the first elements in

`fv1 fv2 ...`that satisfy`pred?`. If no matching element is found by the end of the shortest flexvector,`#f`is returned.`flexvector-index-right`is similar, but returns the index of the*last*elements that satisfy`pred?`, and requires all flexvector arguments to have the same length. Given*n*arguments`fv1 fv2...`,`pred?`should be a function that takes*n*arguments and returns a single value, interpreted as a boolean.

`flexvector-skip``pred?``fv1``fv2``...`procedureFinds and returns the index of the first elements in

`fv1 fv2 ...`that do*not*satisfy`pred?`. If all the values in the flexvectors satisfy`pred?`until the end of the shortest flexvector, this returns`#f`.`flexvector-skip-right`is similar, but returns the index of the*last*elements that do not satisfy`pred?`, and requires all flexvector arguments to have the same length. Given*n*arguments`fv1 fv2...`,`pred?`should be a function that takes*n*arguments and returns a single value, interpreted as a boolean.

`flexvector-binary-search``fv``value``cmp``#!optional``start``end`procedureSimilar to

`flexvector-index`and`flexvector-index-right`, but, instead of searching left-to-right or right-to-left, this performs a binary search. If there is more than one element of`fv`that matches`value`in the sense of`cmp`,`flexvector-binary-search`may return the index of any of them. The search is performed on only the indexes of`fv`between`start`, which defaults to 0, and`end`, which defaults to the length of`fv`. Both`start`and`end`are clamped to the range [0,`(flexvector-length fv)`]. It is an error if`end`is less than`start`.`cmp`should be a procedure of two arguments that returns either a negative integer, which indicates that its first argument is less than its second; zero, which indicates that they are equal; or a positive integer, which indicates that the first argument is greater than the second argument. An example`cmp`might be:

`flexvector-any``pred?``fv1``fv2``...`procedureFinds the first set of elements in parallel from

`fv1 fv2 ...`for which`pred?`returns a true value. If such a parallel set of elements exists,`flexvector-any`returns the value that`pred?`returned for that set of elements. The iteration is strictly left-to-right.

`flexvector-every``pred?``fv1``fv2``...`procedureIf, for every index

`i`between 0 and the length of the shortest flexvector argument, the set of elements`(flexvector-ref fv1 i) (flexvector-ref fv2 i) ...`satisfies`pred?`,`flexvector-every`returns the value that`pred?`returned for the last set of elements, at the last index of the shortest flexvector. The iteration is strictly left-to-right.

`flexvector-partition``pred?``fv`procedureReturns two values: a flexvector containing all elements of

`fv`that satisfy`pred?`, and a flexvector containing all elements of`fv`that do not satisfy`pred?`. Elements remain in their original order.

#### Conversion

`flexvector->vector``fv``#!optional``start``end`procedureCreates a vector containing the elements in

`fv`between`start`, which defaults to 0, and`end`, which defaults to the length of`fv`. Both`start`and`end`are clamped to the range [0,`(flexvector-length fv)`). It is an error if`end`is less than`start`.

`vector->flexvector``vec``#!optional``start``end`procedureCreates a flexvector containing the elements in

`vec`between`start`, which defaults to 0, and`end`, which defaults to the length of`vec`. Both`start`and`end`are clamped to the range [0,`(vector-length vec)`). It is an error if`end`is less than`start`.

`flexvector->list``fv``#!optional``start``end`procedureCreates a list containing the elements in

`fv`between`start`, which defaults to 0, and`end`, which defaults to the length of`fv`.`reverse-flexvector->list`is similar, but creates a list with elements in reverse order of`fv`. Both`start`and`end`are clamped to the range [0,`(flexvector-length fv)`). It is an error if`end`is less than`start`.

`list->flexvector``proper-list`procedureCreates a flexvector of elements from

`proper-list`.`reverse-list->flexvector`is similar, but creates a flexvector with elements in reverse order of`proper-list`.

`flexvector->string``fv``#!optional``start``end`procedureCreates a string containing the elements in

`fv`between`start`, which defaults to 0, and`end`, which defaults to the length of`fv`. It is an error if the elements are not characters. Both`start`and`end`are clamped to the range [0,`(flexvector-length fv)`). It is an error if`end`is less than`start`.

`string->flexvector``string``#!optional``start``end`procedureCreates a flexvector containing the elements in

`string`between`start`, which defaults to 0, and`end`, which defaults to the length of`string`. Both`start`and`end`are clamped to the range [0,`(string-length string)`). It is an error if`end`is less than`start`.

`flexvector->generator``fv`procedureReturns a SRFI 158 generator that emits the elements of the flexvector

`fv`, in order. If`fv`is mutated before the generator is exhausted, the generator's remaining return values are undefined.

`generator->flexvector``gen`procedureCreates a flexvector containing all elements produced by the SRFI 158 generator

`gen`.

### Acknowledgements

Thanks to the authors of SRFI 133 (John Cowan, and, transitively, Taylor Campbell), on whose work this SRFI is based. Much of the language in this SRFI was copied directly from 133 with only minor changes.

### CHICKEN egg version history

#### 1.0.1

- Fix
`flexvector->list`implementation by supporting the optional`start`and`end`parameter

#### 1.0.0

- Initial release of SRFI-214 as a CHICKEN egg

### Copyright

Copyright (C) Adam Nelson (2020-2021). Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.