From 99f231f3477c276c84b1335490c3942453095dad Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 29 Feb 2012 18:13:29 -0800 Subject: [PATCH] core: Change a number of arguments in vec to const vecs --- src/libcore/vec.rs | 63 +++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index 5baebb60f16..4b0c83d7168 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -52,7 +52,7 @@ Predicate: same_length Returns true if two vectors have the same length */ -pure fn same_length(xs: [T], ys: [U]) -> bool { +pure fn same_length(xs: [const T], ys: [const U]) -> bool { vec::len(xs) == vec::len(ys) } @@ -278,7 +278,7 @@ Function: split Split the vector `v` by applying each element against the predicate `f`. */ -fn split(v: [T], f: fn(T) -> bool) -> [[T]] { +fn split(v: [const T], f: fn(T) -> bool) -> [[T]] { let ln = len(v); if (ln == 0u) { ret [] } @@ -303,7 +303,7 @@ Function: splitn Split the vector `v` by applying each element against the predicate `f` up to `n` times. */ -fn splitn(v: [T], n: uint, f: fn(T) -> bool) -> [[T]] { +fn splitn(v: [const T], n: uint, f: fn(T) -> bool) -> [[T]] { let ln = len(v); if (ln == 0u) { ret [] } @@ -331,7 +331,7 @@ Function: rsplit Reverse split the vector `v` by applying each element against the predicate `f`. */ -fn rsplit(v: [T], f: fn(T) -> bool) -> [[T]] { +fn rsplit(v: [const T], f: fn(T) -> bool) -> [[T]] { let ln = len(v); if (ln == 0u) { ret [] } @@ -356,7 +356,7 @@ Function: rsplitn Reverse split the vector `v` by applying each element against the predicate `f` up to `n times. */ -fn rsplitn(v: [T], n: uint, f: fn(T) -> bool) -> [[T]] { +fn rsplitn(v: [const T], n: uint, f: fn(T) -> bool) -> [[T]] { let ln = len(v); if (ln == 0u) { ret [] } @@ -412,7 +412,7 @@ Function: push Append an element to a vector */ -fn push(&v: [T], initval: T) { +fn push(&v: [const T], initval: T) { v += [initval]; } @@ -432,7 +432,7 @@ v - The vector to grow n - The number of elements to add initval - The value for the new elements */ -fn grow(&v: [T], n: uint, initval: T) { +fn grow(&v: [const T], n: uint, initval: T) { reserve(v, next_power_of_two(len(v) + n)); let i: uint = 0u; while i < n { v += [initval]; i += 1u; } @@ -471,7 +471,7 @@ v - The vector to grow n - The number of elements to add init_op - A function to call to retreive each appended element's value */ -fn grow_fn(&v: [T], n: uint, op: init_op) { +fn grow_fn(&v: [const T], n: uint, op: init_op) { reserve(v, next_power_of_two(len(v) + n)); let i: uint = 0u; while i < n { v += [op(i)]; i += 1u; } @@ -527,7 +527,8 @@ Function: map2 Apply a function to each pair of elements and return the results */ -fn map2(v0: [T], v1: [U], f: fn(T, U) -> V) -> [V] { +fn map2(v0: [const T], v1: [const U], + f: fn(T, U) -> V) -> [V] { let v0_len = len(v0); if v0_len != len(v1) { fail; } let u: [V] = []; @@ -645,7 +646,7 @@ Return true if a predicate matches any elements in both vectors. If the vectors contains no elements then false is returned. */ -fn any2(v0: [T], v1: [U], f: fn(T, U) -> bool) -> bool { +fn any2(v0: [const T], v1: [U], f: fn(T, U) -> bool) -> bool { let v0_len = len(v0); let v1_len = len(v1); let i = 0u; @@ -675,7 +676,7 @@ Return true if a predicate matches all elements in both vectors. If the vectors are not the same size then false is returned. */ -fn all2(v0: [T], v1: [U], f: fn(T, U) -> bool) -> bool { +fn all2(v0: [const T], v1: [const U], f: fn(T, U) -> bool) -> bool { let v0_len = len(v0); if v0_len != len(v1) { ret false; } let i = 0u; @@ -688,7 +689,7 @@ Function: contains Return true if a vector contains an element with the given value */ -fn contains(v: [T], x: T) -> bool { +fn contains(v: [const T], x: T) -> bool { for elt: T in v { if x == elt { ret true; } } ret false; } @@ -713,7 +714,7 @@ 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. */ -fn find(v: [T], f: fn(T) -> bool) -> option { +fn find(v: [const T], f: fn(T) -> bool) -> option { find_from(v, 0u, len(v), f) } @@ -726,8 +727,8 @@ 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. */ -fn find_from(v: [T], start: uint, end: uint, f: fn(T) -> bool) -> - option { +fn find_from(v: [const T], start: uint, end: uint, + f: fn(T) -> bool) -> option { option::map(position_from(v, start, end, f)) { |i| v[i] } } @@ -740,7 +741,7 @@ 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. */ -fn rfind(v: [T], f: fn(T) -> bool) -> option { +fn rfind(v: [const T], f: fn(T) -> bool) -> option { rfind_from(v, 0u, len(v), f) } @@ -753,8 +754,8 @@ 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. */ -fn rfind_from(v: [T], start: uint, end: uint, f: fn(T) -> bool) -> - option { +fn rfind_from(v: [const T], start: uint, end: uint, + f: fn(T) -> bool) -> option { option::map(rposition_from(v, start, end, f)) { |i| v[i] } } @@ -768,7 +769,7 @@ Returns: option::some(uint) - The first index containing a matching value option::none - No elements matched */ -fn position_elt(v: [T], x: T) -> option { +fn position_elt(v: [const T], x: T) -> option { position(v) { |y| x == y } } @@ -781,7 +782,7 @@ 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. */ -fn position(v: [T], f: fn(T) -> bool) -> option { +fn position(v: [const T], f: fn(T) -> bool) -> option { position_from(v, 0u, len(v), f) } @@ -794,8 +795,8 @@ 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. */ -fn position_from(v: [T], start: uint, end: uint, f: fn(T) -> bool) -> - option { +fn position_from(v: [const T], start: uint, end: uint, + f: fn(T) -> bool) -> option { assert start <= end; assert end <= len(v); let i = start; @@ -813,7 +814,7 @@ Returns: option::some(uint) - The last index containing a matching value option::none - No elements matched */ -fn rposition_elt(v: [T], x: T) -> option { +fn rposition_elt(v: [const T], x: T) -> option { rposition(v) { |y| x == y } } @@ -826,7 +827,7 @@ 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. */ -fn rposition(v: [T], f: fn(T) -> bool) -> option { +fn rposition(v: [const T], f: fn(T) -> bool) -> option { rposition_from(v, 0u, len(v), f) } @@ -839,8 +840,8 @@ 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. */ -fn rposition_from(v: [T], start: uint, end: uint, f: fn(T) -> bool) -> - option { +fn rposition_from(v: [const T], start: uint, end: uint, + f: fn(T) -> bool) -> option { assert start <= end; assert end <= len(v); let i = end; @@ -865,7 +866,7 @@ 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. */ -fn unzip(v: [(T, U)]) -> ([T], [U]) { +fn unzip(v: [const (T, U)]) -> ([T], [U]) { let as = [], bs = []; for (a, b) in v { as += [a]; bs += [b]; } ret (as, bs); @@ -883,7 +884,7 @@ Preconditions: (v, u) */ -fn zip(v: [T], u: [U]) -> [(T, U)] { +fn zip(v: [const T], u: [const U]) -> [(T, U)] { let zipped = []; let sz = len(v), i = 0u; assert sz == len(u); @@ -979,7 +980,7 @@ Function: iter2 Iterates over two vectors in parallel */ -fn iter2(v: [U], v2: [T], f: fn(U, T)) { +fn iter2(v: [ U], v2: [const T], f: fn(U, T)) { let i = 0; for elt in v { f(elt, v2[i]); i += 1; } } @@ -1036,7 +1037,7 @@ 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. */ -fn permute(v: [const T], put: fn([T])) { +fn permute(v: [T], put: fn([T])) { let ln = len(v); if ln == 0u { put([]); @@ -1051,7 +1052,7 @@ fn permute(v: [const T], put: fn([T])) { } } -fn windowed (nn: uint, xx: [TT]) -> [[TT]] { +fn windowed (nn: uint, xx: [const TT]) -> [[TT]] { let ww = []; assert 1u <= nn;