Vectors

Type init_op

type init_op<T> = fn(uint) -> T

A function used to initialize the elements of a vector

Implementation extensions for ~[T]

Method +

pure fn +(rhs: & [const T]) -> ~[T]

Implementation extensions for ~[mut T]

Method +

pure fn +(rhs: & [const T]) -> ~[mut T]

Implementation extensions for & [const T]

Extension methods for vectors

Method is_empty

pure fn is_empty() -> bool

Returns true if a vector contains no elements

Method is_not_empty

pure fn is_not_empty() -> bool

Returns true if a vector contains some elements

Method len

pure fn len() -> uint

Returns the length of a vector

Implementation extensions for & [const T]

Extension methods for vectors

Method head

pure fn head() -> T

Returns the first element of a vector

Method init

pure fn init() -> ~[T]

Returns all but the last elemnt of a vector

Method last

pure fn last() -> T

Returns the last element of a v, failing if the vector is empty.

Method slice

pure fn slice(start: uint, end: uint) -> ~[T]

Returns a copy of the elements from [start..end) from v.

Method tail

pure fn tail() -> ~[T]

Returns all but the first element of a vector

Implementation extensions for & [T]

Extension methods for vectors

Method foldr

pure fn foldr<U: copy>(z: U, p: fn(T, U) -> U) -> U

Reduce a vector from right to left

Method iter

pure fn iter(f: fn(T))

Iterates over a vector

Iterates over vector v and, for each element, calls function f with the element's value.

Method iteri

pure fn iteri(f: fn(uint, T))

Iterates over a vector's elements and indexes

Iterates over vector v and, for each element, calls function f with the element's value and index.

Method position

pure fn position(f: fn(T) -> bool) -> option<uint>

Find the first index matching some predicate

Apply function f to each element of v. When function f returns true then an option containing the index is returned. If f matches no elements then none is returned.

Method position_elem

pure fn position_elem(x: T) -> option<uint>

Find the first index containing a matching value

Method riter

pure fn riter(f: fn(T))

Iterates over a vector in reverse

Iterates over vector v and, for each element, calls function f with the element's value.

Method riteri

pure fn riteri(f: fn(uint, T))

Iterates over a vector's elements and indexes in reverse

Iterates over vector v and, for each element, calls function f with the element's value and index.

Method rposition

pure fn rposition(f: fn(T) -> bool) -> option<uint>

Find the last index matching some predicate

Apply function f to each element of v in reverse order. When function f returns true then an option containing the index is returned. If f matches no elements then none is returned.

Method rposition_elem

pure fn rposition_elem(x: T) -> option<uint>

Find the last index containing a matching value

Method map

pure fn map<U>(f: fn(T) -> U) -> ~[U]

Apply a function to each element of a vector and return the results

Method mapi

pure fn mapi<U>(f: fn(uint, T) -> U) -> ~[U]

Apply a function to the index and value of each element in the vector and return the results

Method map_r

fn map_r<U>(f: fn(x: &self .T) -> U) -> ~[U]

Method alli

pure fn alli(f: fn(uint, T) -> bool) -> bool

Returns true if the function returns true for all elements.

If the vector is empty, true is returned.

Method flat_map

pure fn flat_map<U>(f: fn(T) -> ~[U]) -> ~[U]

Apply a function to each element of a vector and return a concatenation of each result vector

Method filter_map

pure fn filter_map<U: copy>(f: fn(T) -> option<U>) -> ~[U]

Apply a function to each element of a vector and return the results

If function f returns none then that element is excluded from the resulting vector.

Implementation extensions for & [T]

Extension methods for vectors

Method filter

pure fn filter(f: fn(T) -> bool) -> ~[T]

Construct a new vector from the elements of a vector for which some predicate holds.

Apply function f to each element of v and return a vector containing only those elements for which f returned true.

Method find

pure fn find(f: fn(T) -> bool) -> option<T>

Search for the first element that matches a given predicate

Apply function f to each element of v, starting from the first. When function f returns true then an option containing the element is returned. If f matches no elements then none is returned.

Method rfind

pure fn rfind(f: fn(T) -> bool) -> option<T>

Search for the last element that matches a given predicate

Apply function f to each element of v in reverse order. When function f returns true then an option containing the element is returned. If f matches no elements then none is returned.

Implementation extensions of iter::base_iter<A> for & [const A]

Method each

fn each(blk: fn(A) -> bool)

Method size_hint

fn size_hint() -> option<uint>

Method eachi

fn eachi(blk: fn(uint, A) -> bool)

Method all

fn all(blk: fn(A) -> bool) -> bool

Method any

fn any(blk: fn(A) -> bool) -> bool

Method foldl

fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B

Method contains

fn contains(x: A) -> bool

Method count

fn count(x: A) -> uint

Implementation extensions for & [const A]

Method filter_to_vec

fn filter_to_vec(pred: fn(A) -> bool) -> ~[A]

Method map_to_vec

fn map_to_vec<B>(op: fn(A) -> B) -> ~[B]

Method to_vec

fn to_vec() -> ~[A]

Method min

fn min() -> A

Method max

fn max() -> A

Function all

pure fn all<T>(v: & [T], f: fn(T) -> bool) -> bool

Return true if a predicate matches all elements

If the vector contains no elements then true is returned.

Function all2

pure fn all2<T, U>(v0: & [T], v1: & [U], f: fn(T, U) -> bool) -> bool

Return true if a predicate matches all elements in both vectors.

If the vectors are not the same size then false is returned.

Function alli

pure fn alli<T>(v: & [T], f: fn(uint, T) -> bool) -> bool

Return true if a predicate matches all elements

If the vector contains no elements then true is returned.

Function any

pure fn any<T>(v: & [T], f: fn(T) -> bool) -> bool

Return true if a predicate matches any elements

If the vector contains no elements then false is returned.

Function any2

pure fn any2<T, U>(v0: & [T], v1: & [U], f: fn(T, U) -> bool) -> bool

Return true if a predicate matches any elements in both vectors.

If the vectors contains no elements then false is returned.

Function append

pure fn append<T: copy>(+lhs: ~[T], rhs: & [const T]) -> ~[T]

Function append_one

pure fn append_one<T>(+lhs: ~[T], +x: T) -> ~[T]

Function as_buf

fn as_buf<E, T>(v: & [E], f: fn(*E) -> T) -> T

Work with the buffer of a vector.

Allows for unsafe manipulation of vector contents, which is useful for foreign interop.

Function as_mut_buf

fn as_mut_buf<E, T>(v: & [mut E], f: fn(*mut E) -> T) -> T

Function capacity

pure fn capacity<T>(&&v: ~[const T]) -> uint

Returns the number of elements the vector can hold without reallocating

Function concat

pure fn concat<T: copy>(v: & [~[T]]) -> ~[T]

Concatenate a vector of vectors.

Flattens a vector of vectors of T into a single vector of T.

Function connect

pure fn connect<T: copy>(v: & [~[T]], sep: T) -> ~[T]

Concatenate a vector of vectors, placing a given separator between each

Function consume

fn consume<T>(+v: ~[T], f: fn(uint, +T))

Function contains

pure fn contains<T>(v: & [T], x: T) -> bool

Return true if a vector contains an element with the given value

Function count

pure fn count<T>(v: & [T], x: T) -> uint

Returns the number of elements that are equal to a given value

Function each

pure fn each<T>(v: & [const T], f: fn(T) -> bool)

Iterates over a vector, with option to break

Return true to continue, false to break.

Function eachi

pure fn eachi<T>(v: & [const T], f: fn(uint, T) -> bool)

Iterates over a vector's elements and indices

Return true to continue, false to break.

Function filter

pure fn filter<T: copy>(v: & [T], f: fn(T) -> bool) -> ~[T]

Construct a new vector from the elements of a vector for which some predicate holds.

Apply function f to each element of v and return a vector containing only those elements for which f returned true.

Function filter_map

pure fn filter_map<T, U: copy>(v: & [T], f: fn(T) -> option<U>) -> ~[U]

Apply a function to each element of a vector and return the results

If function f returns none then that element is excluded from the resulting vector.

Function find

pure fn find<T: copy>(v: & [T], f: fn(T) -> bool) -> option<T>

Search for the first element that matches a given predicate

Apply function f to each element of v, starting from the first. When function f returns true then an option containing the element is returned. If f matches no elements then none is returned.

Function find_between

pure fn find_between<T: copy>(v: & [T], start: uint, end: uint,
                              f: fn(T) -> bool) -> option<T>

Search for the first element that matches a given predicate within a range

Apply function f to each element of v within the range [start, end). When function f returns true then an option containing the element is returned. If f matches no elements then none is returned.

Function flat_map

pure fn flat_map<T, U>(v: & [T], f: fn(T) -> ~[U]) -> ~[U]

Apply a function to each element of a vector and return a concatenation of each result vector

Function foldl

pure fn foldl<T: copy, U>(z: T, v: & [U], p: fn(T, U) -> T) -> T

Reduce a vector from left to right

Function foldr

pure fn foldr<T, U: copy>(v: & [T], z: U, p: fn(T, U) -> U) -> U

Reduce a vector from right to left

Function from_elem

pure fn from_elem<T: copy>(n_elts: uint, t: T) -> ~[T]

Creates and initializes an immutable vector.

Creates an immutable vector of size n_elts and initializes the elements to the value t.

Function from_fn

pure fn from_fn<T>(n_elts: uint, op: init_op<T>) -> ~[T]

Creates and initializes an immutable vector.

Creates an immutable vector of size n_elts and initializes the elements to the value returned by the function op.

Function from_mut

fn from_mut<T>(+v: ~[mut T]) -> ~[T]

Produces an immutable vector from a mut vector.

Function grow

fn grow<T: copy>(&v: ~[const T], n: uint, initval: T)

Expands a vector in place, initializing the new elements to a given value

Arguments

Function grow_fn

fn grow_fn<T>(&v: ~[const T], n: uint, op: init_op<T>)

Expands a vector in place, initializing the new elements to the result of a function

Function init_op is called n times with the values [0..n)

Arguments

Function grow_set

fn grow_set<T: copy>(&v: ~[mut T], index: uint, initval: T, val: T)

Sets the value of a vector element at a given index, growing the vector as needed

Sets the element at position index to val. If index is past the end of the vector, expands the vector by replicating initval to fill the intervening space.

Function head

pure fn head<T: copy>(v: & [const T]) -> T

Returns the first element of a vector

Function init

pure fn init<T: copy>(v: & [const T]) -> ~[T]

Returns a vector containing all but the last element of a slice

Function is_empty

pure fn is_empty<T>(v: & [const T]) -> bool

Returns true if a vector contains no elements

Function is_not_empty

pure fn is_not_empty<T>(v: & [const T]) -> bool

Returns true if a vector contains some elements

Function iter

pure fn iter<T>(v: & [T], f: fn(T))

Iterates over a slice

Iterates over slice v and, for each element, calls function f with the element's value.

Function iter2

fn iter2<U, T>(v1: & [U], v2: & [T], f: fn(U, T))

Iterates over two vectors simultaneously

Failure

Both vectors must have the same length

Function iter_between

pure fn iter_between<T>(v: & [T], start: uint, end: uint, f: fn(T))

Function iteri

pure fn iteri<T>(v: & [T], f: fn(uint, T))

Iterates over a vector's elements and indexes

Iterates over vector v and, for each element, calls function f with the element's value and index.

Function last

pure fn last<T: copy>(v: & [const T]) -> T

Returns the last element of the slice v, failing if the slice is empty.

Function last_opt

pure fn last_opt<T: copy>(v: & [const T]) -> option<T>

Returns some(x) where x is the last element of the slice v, or none if the vector is empty.

Function len

pure fn len<T>(&&v: & [const T]) -> uint

Returns the length of a vector

Function map

pure fn map<T, U>(v: & [T], f: fn(T) -> U) -> ~[U]

Apply a function to each element of a vector and return the results

Function map2

pure fn map2<T: copy, U: copy, V>(v0: & [T], v1: & [U], f: fn(T, U) -> V) ->
        ~[V]

Apply a function to each pair of elements and return the results

Function map_consume

fn map_consume<T, U>(+v: ~[T], f: fn(+T) -> U) -> ~[U]

Function mapi

pure fn mapi<T, U>(v: & [T], f: fn(uint, T) -> U) -> ~[U]

Apply a function to each element of a vector and return the results

Function permute

pure fn permute<T: copy>(v: & [T], put: fn(~[T]))

Iterate over all permutations of vector v.

Permutations are produced in lexicographic order with respect to the order of elements in v (so if v is sorted then the permutations are lexicographically sorted).

The total number of permutations produced is len(v)!. If v contains repeated elements, then some permutations are repeated.

Function pop

fn pop<T>(&v: ~[const T]) -> T

Remove the last element from a vector and return it

Function position

pure fn position<T>(v: & [T], f: fn(T) -> bool) -> option<uint>

Find the first index matching some predicate

Apply function f to each element of v. When function f returns true then an option containing the index is returned. If f matches no elements then none is returned.

Function position_between

pure fn position_between<T>(v: & [T], start: uint, end: uint,
                            f: fn(T) -> bool) -> option<uint>

Find the first index matching some predicate within a range

Apply function f to each element of v between the range [start, end). When function f returns true then an option containing the index is returned. If f matches no elements then none is returned.

Function position_elem

pure fn position_elem<T>(v: & [T], x: T) -> option<uint>

Find the first index containing a matching value

Function push

fn push<T>(&v: ~[const T], +initval: T)

Append an element to a vector

Function push_all

fn push_all<T: copy>(&v: ~[const T], rhs: & [const T])

Function push_all_move

fn push_all_move<T>(&v: ~[const T], -rhs: ~[const T])

Function reserve

fn reserve<T>(&v: ~[const T], n: uint)

Reserves capacity for exactly n elements in the given vector.

If the capacity for v is already equal to or greater than the requested capacity, then no action is taken.

Arguments

Function reserve_at_least

fn reserve_at_least<T>(&v: ~[const T], n: uint)

Reserves capacity for at least n elements in the given vector.

This function will over-allocate in order to amortize the allocation costs in scenarios where the caller may need to repeatedly reserve additional space.

If the capacity for v is already equal to or greater than the requested capacity, then no action is taken.

Arguments

Function reverse

fn reverse<T>(v: ~[mut T])

Reverse the order of elements in a vector, in place

Function reversed

pure fn reversed<T: copy>(v: & [const T]) -> ~[T]

Returns a vector with the order of elements reversed

Function rfind

pure fn rfind<T: copy>(v: & [T], f: fn(T) -> bool) -> option<T>

Search for the last element that matches a given predicate

Apply function f to each element of v in reverse order. When function f returns true then an option containing the element is returned. If f matches no elements then none is returned.

Function rfind_between

pure fn rfind_between<T: copy>(v: & [T], start: uint, end: uint,
                               f: fn(T) -> bool) -> option<T>

Search for the last element that matches a given predicate within a range

Apply function f to each element of v in reverse order within the range [start, end). When function f returns true then an option containing the element is returned. If f matches no elements then none is returned.

Function riter

pure fn riter<T>(v: & [T], f: fn(T))

Iterates over a vector in reverse

Iterates over vector v and, for each element, calls function f with the element's value.

Function riteri

pure fn riteri<T>(v: & [T], f: fn(uint, T))

Iterates over a vector's elements and indexes in reverse

Iterates over vector v and, for each element, calls function f with the element's value and index.

Function rposition

pure fn rposition<T>(v: & [T], f: fn(T) -> bool) -> option<uint>

Find the last index matching some predicate

Apply function f to each element of v in reverse order. When function f returns true then an option containing the index is returned. If f matches no elements then none is returned.

Function rposition_between

pure fn rposition_between<T>(v: & [T], start: uint, end: uint,
                             f: fn(T) -> bool) -> option<uint>

Find the last index matching some predicate within a range

Apply function f to each element of v in reverse order between the range [start, end). When function f returns true then an option containing the index is returned. If f matches no elements then none is returned.

Function rsplit

fn rsplit<T: copy>(v: & [T], f: fn(T) -> bool) -> ~[~[T]]

Reverse split the vector v by applying each element against the predicate f.

Function rsplitn

fn rsplitn<T: copy>(v: & [T], n: uint, f: fn(T) -> bool) -> ~[~[T]]

Reverse split the vector v by applying each element against the predicate f up to `n times.

Function same_length

pure fn same_length<T, U>(xs: & [const T], ys: & [const U]) -> bool

Returns true if two vectors have the same length

Function shift

fn shift<T>(&v: ~[T]) -> T

Removes the first element from a vector and return it

Function slice

pure fn slice<T: copy>(v: & [const T], start: uint, end: uint) -> ~[T]

Returns a copy of the elements from [start..end) from v.

Function split

fn split<T: copy>(v: & [T], f: fn(T) -> bool) -> ~[~[T]]

Split the vector v by applying each element against the predicate f.

Function splitn

fn splitn<T: copy>(v: & [T], n: uint, f: fn(T) -> bool) -> ~[~[T]]

Split the vector v by applying each element against the predicate f up to n times.

Function swap

fn swap<T>(&&v: ~[mut T], a: uint, b: uint)

Swaps two elements in a vector

Arguments

Function tail

pure fn tail<T: copy>(v: & [const T]) -> ~[T]

Returns a vector containing all but the first element of a slice

Function tailn

pure fn tailn<T: copy>(v: & [const T], n: uint) -> ~[T]

Returns a vector containing all but the first n \ elements of a slice

Function to_mut

fn to_mut<T>(+v: ~[T]) -> ~[mut T]

Produces a mut vector from an immutable vector.

Function unpack_const_slice

pure fn unpack_const_slice<T, U>(s: & [const T], f: fn(*const T, uint) -> U)
        -> U

Work with the buffer and length of a slice.

Function unpack_slice

pure fn unpack_slice<T, U>(s: & [const T], f: fn(*T, uint) -> U) -> U

Work with the buffer and length of a slice.

Function unshift

fn unshift<T>(&v: ~[T], +x: T)

Prepend an element to the vector

Function unzip

pure fn unzip<T: copy, U: copy>(v: & [(T, U)]) -> (~[T], ~[U])

Convert a vector of pairs into a pair of vectors

Returns a tuple containing two vectors where the i-th element of the first vector contains the first element of the i-th tuple of the input vector, and the i-th element of the second vector contains the second element of the i-th tuple of the input vector.

Function view

pure fn view<T>(v: & [const T], start: uint, end: uint) -> &a [T]

Return a slice that points into another slice.

Function windowed

pure fn windowed<TT: copy>(nn: uint, xx: & [TT]) -> ~[~[TT]]

Function zip

pure fn zip<T: copy, U: copy>(v: & [const T], u: & [const U]) -> ~[(T, U)]

Convert two vectors to a vector of pairs

Returns a vector of tuples, where the i-th tuple contains contains the i-th elements from each of the input vectors.