For Gauche 0.9.5

Previous: , Up: ライブラリモジュール - SRFI   [Contents][Index]

### 11.25 `srfi-133` - ベクタライブラリ

Module: srfi-133

This module adds rich set of vector operations to the built-in / R7RS vector procedures.

The following procedures are built-in. See ベクタ, for the description. We only explain the procedures that are not built-in.

```make-vector          vector               vector?
vector-ref           vector-set!          vector-length
vector-fill!         vector-copy          vector-copy!
vector-append        vector->list         list->vector
reverse-list->vector vector->string       string->vector
vector-map           vector-map!          vector-for-each
```

This SRFI effectively supesedes `srfi-43` (see ベクタライブラリ(旧式)). Note that the interface of following procedures in `srfi-43` are changed for the consistency:

```vector-map           vector-map!          vector-for-each
vector-fold          vector-fold-right    vector-count
```

Some of the functionalities of `srfi-43` version is supported by built-in procedures (e.g. Built-in `vector-map-with-index` is the same as `srfi-43`’s `vector-map`). So there’s little point for new code to use `srfi-43`.

#### ベクタ構成子

Function: vector-unfold f length seed …

[SRFI-133] Creates a vector of length length, filling elements left to right by calling f repeatedly.

The procedure f must take as many arguments as one plus number of seed values, and must return the same number of values. The first argument is the index. The first return value is used for the element of the result vector, and the rest of return values are passed to the next call of f.

```(vector-unfold (^[i x] (values (cons i x) (* x 2))) 8 1)
⇒ #((0 . 1) (1 . 2) (2 . 4) (3 . 8)
(4 . 16) (5 . 32) (6 . 64) (7 . 128))
```
Function: vector-unfold-right f length seed …

[SRFI-133] Creates a vector of length length, filling elements right to left by calling f repeatedly.

The procedure f must take as many arguments as one plus number of seed values, and must return the same number of values. The first argument is the index. The first return value is used for the element of the result vector, and the rest of return values are passed to the next call of f.

```(vector-unfold-right (^[i x] (values (cons i x) (* x 2))) 8 1)
⇒ #((0 . 128) (1 . 64) (2 . 32) (3 . 16)
(4 . 8) (5 . 4) (6 . 2) (7 . 1))
```
Function: vector-reverse-copy vec :optional start end

[SRFI-133] Copies the vector vec with reversing its elements. Optional start and end arguments can limit the range of the input.

```(vector-reverse-copy '#(a b c d e) 1 4)
⇒ #(d c b)
```
Function: vector-concatenate list-of-vectors

[SRFI-133] Same as `(apply vector-append list-of-vectors)`.

Function: vector-append-subvectors spec …

[SRFI-133] The number of arguments must be multiple of 3. The argument list must be in the following format, where each vecN is a vector, and `startN` and `endN` are nonnegative integers:

```vec1 start1 end1 vec2 start2 end2 …
```

This procedure creates a new vector by concatenating subvectors specified by each triplet. That is, it works as if it’s the following code, except it avoids copying each subvector:

```(vector-append (vector-copy vec1 start1 end1)
(vector-copy vec2 start2 end2)
…)
```

Here’s an example:

```(vector-append-subvectors '#(a b c d e) 0 3
'#(f g h i j) 2 5)
⇒ #(a b c h i j)
```

#### ベクタ上の述語

Function: vector-empty? vec

[SRFI-133] Returns `#t` if vec’s length is zero, and `#f` if vec’s length is more than zero. Signals an error if vec is not a vector.

Function: vector= elt= vec …

[SRFI-133] Compares vecs element-wise, using given predicate elt=. Returns `#t` iff lengths of all the vectors are the same, and every corresponding elements are equal by elt=. Elt= is always called with two arguments and must return `#t` iff two are the same.

#### ベクタ上の繰り返し

Function: vector-fold kons knil vec1 vec2 …

[SRFI-133] Kons is a procedure that takes n+1 arguments, where n is the number of given vectors. For each element of the given vectors, kons is called as `(kons seed e_1i e_2i …)`, where and e_ni is the i-th element of the vector n. If the lengths of the vectors differ, iteration stops when the shortest vector is exhausted.

The initial value of seed is knil, and the return value from kons is used as the next seed value. The last return value of kons is returned from `vector-fold`.

The iteration is strictly left to right.

Note that the seed value precedes elements, which is opposite to `fold` (see コレクションに対するマッピング). It’s an unfortunate historical glitch; `vector-fold-left` would be more consistent name.

```(vector-fold (^[a b] (cons b a)) '() '#(a b c d))
⇒ (d c b a)
```
Function: vector-fold-right kons knil vec1 vec2 …

[SRFI-133] Like `vector-fold`, but elements in the vec1 vec2 … are visited from right to left.

Unlike `fold-right` (see シーケンス上のマップ), the procedure kons takes the accumulated value in the first argument.

```(vector-fold-right (^[a b] (cons b a)) '() '#(a b c d))
⇒ (a b c d)
```
Function: vector-count pred vec1 vec2 …

[SRFI-133] Applies pred on each elements in argument vectors (if N vectors are given, pred takes N arguments, the first being i-th element of vec1, the second being i-th element of vec2, etc.) Then returns the number of times pred returned true value. The order pred applied to each element is unspecified.

```(vector-count odd? '#(0 1 2 3 4)
⇒ 2

(vector-count < '#(7 3 9 1 5) '#(6 8 2 3 8 8))
⇒ 3
```
Function: vector-cumulate f seed vec

[SRFI-133] Returns a fresh vector with the same size of vec, with the elements calculated as follows:

The first element of result vector is a result of procedure f called with seed and the first element of vec.

The i-th element of result vector is a result of procedure f called with i-1-th element of result vector and i-th element of vec.

```(vector-cumulate string-append "z" '#("a" "b" "c"))
⇒ #("za" "zab" "zabc")
```

#### ベクタ上の検索

Function: vector-index pred vec1 vec2 …
Function: vector-index-right pred vec1 vec2 …

[SRFI-133] Returns the index of the first or the last elements in vec1 vec2 … that satisfy pred, respectively. Returns `#f` if no elements satisfy pred. In `vector-index`, comparison ends at the end of the shortest vector. For `vector-index-right`, all the vectors must have the same length.

Function: vector-skip pred vec1 vec2 …
Function: vector-skip-right pred vec1 vec2 …

[SRFI-133] Like `vector-index` and `vector-index-right`, except that the result of pred is negated. That is, returns the index of the first or the last elements that don’t satisfy pred.

Function: vector-binary-search vec value cmp :optional start end

[SRFI-133+] Look for value in a vector vec, and returns its index if it is found, or `#f` if it is not found. Comparison of value and an element in vec is done by a procedure cmp, which takes two arguments, and should return a negative integer if the first argument is less than the second, 0 if they are the same, and a positive integer if the first is greater than the second.

Elements in vec must be ordered from smaller to greater w.r.t. cmp. Using that fact, this procedure performs binary search instead of linear search.

The optional arguments start and end are an extention to SRFI-133, and can be used to limit the range of the search in start-th element (inclusive) to end-th element (exclusive).

Function: vector-any pred vec1 vec2 …

[SRFI-133] Applies pred on each corresponding elements of vec1 vec2 … left to right, and as soon as pred returns non-`#f` value, the procedure stops iteration and returns the value.

If no elements that satisfy pred are found, it returns `#f`.

Vectors can have different lengths. Iteration stops at the end of the shortest.

Function: vector-every pred vec1 vec2 …

[SRFI-133] Applies pred on each corresponding elements of vec1 vec2 … left to right. If all the elements (when the lengths of vectors differ, the first N elements where N is the length of the shortest) satisfy pred, returns the last result of pred. Otherwise returns `#f`.

Function: vector-partition pred vec

[SRFI-133]

#### ベクタの変更子

Function: vector-swap! vec i j

[SRFI-133] Swaps vector vec’s `i`-th and `j`-th elements.

Function: vector-reverse! vec :optional start end

[SRFI-133] Reverse the elements of vec. Returns an undefined value. Optional start and end arguments can limit the range of operation.

```(rlet1 v (vector 'a 'b 'c 'd 'e)
(vector-reverse! v 0 4))
⇒ #(d c b a e)
```
Function: vector-reverse-copy! target tstart source :optional sstart send

[SRFI-133] Like `vector-copy!`, but reverses the order of elements from start.

```(rlet1 v (vector 'a 'b 'c 'd 'e)
(vector-reverse-copy! v 2 '#(1 2)))
⇒ #(a b 2 1 e)
```

It is ok to pass the same vector to target and source; it always works even if the regions of source and destination are overlapping.

Function: vector-unfold! f rvec start end seeds …
Function: vector-unfold-right! f rvec start end seeds …

[SRFI-133]

#### ベクタの変換

Function: reverse-vector->list vec :optional start end

[SRFI-133] Same as `(reverse (vector->list vec start end))`, but more efficient.

Previous: , Up: ライブラリモジュール - SRFI   [Contents][Index]