Using const vector slices for more vec functions.

This commit is contained in:
Eric Holk 2012-05-18 10:40:54 -07:00
parent 0f20cae37f
commit 0eed37da29
5 changed files with 39 additions and 24 deletions

View file

@ -652,15 +652,16 @@ fn install_named(c: cargo, wd: str, name: str) {
fn install_uuid_specific(c: cargo, wd: str, src: str, uuid: str) { fn install_uuid_specific(c: cargo, wd: str, src: str, uuid: str) {
alt c.sources.find(src) { alt c.sources.find(src) {
some(s) { some(s) {
if vec::any(copy s.packages, { |p| let packages = copy s.packages;
if p.uuid == uuid { if vec::any(packages, { |p|
install_package(c, wd, p); if p.uuid == uuid {
true install_package(c, wd, p);
} else { false } true
}) { ret; } } else { false }
} }) { ret; }
_ { } }
_ { }
} }
error("Can't find package " + src + "/" + uuid); error("Can't find package " + src + "/" + uuid);
} }
@ -668,7 +669,8 @@ fn install_uuid_specific(c: cargo, wd: str, src: str, uuid: str) {
fn install_named_specific(c: cargo, wd: str, src: str, name: str) { fn install_named_specific(c: cargo, wd: str, src: str, name: str) {
alt c.sources.find(src) { alt c.sources.find(src) {
some(s) { some(s) {
if vec::any(copy s.packages, { |p| let packages = copy s.packages;
if vec::any(packages, { |p|
if p.name == name { if p.name == name {
install_package(c, wd, p); install_package(c, wd, p);
true true

View file

@ -33,6 +33,7 @@ export grow;
export grow_fn; export grow_fn;
export grow_set; export grow_set;
export map; export map;
export mapi;
export map2; export map2;
export flat_map; export flat_map;
export filter_map; export filter_map;
@ -440,13 +441,23 @@ fn grow_set<T: copy>(&v: [mut T], index: uint, initval: T, val: T) {
#[doc = " #[doc = "
Apply a function to each element of a vector and return the results Apply a function to each element of a vector and return the results
"] "]
fn map<T, U>(v: [T], f: fn(T) -> U) -> [U] { fn map<T, U>(v: [const T]/&, f: fn(T) -> U) -> [U] {
let mut result = []; let mut result = [];
reserve(result, len(v)); reserve(result, len(v));
for each(v) {|elem| result += [f(elem)]; } for each(v) {|elem| result += [f(elem)]; }
ret result; ret result;
} }
#[doc = "
Apply a function to each element of a vector and return the results
"]
fn mapi<T, U>(v: [const T]/&, f: fn(uint, T) -> U) -> [U] {
let mut result = [];
reserve(result, len(v));
for eachi(v) {|i, elem| result += [f(i, elem)]; }
ret result;
}
#[doc = " #[doc = "
Apply a function to each element of a vector and return a concatenation Apply a function to each element of a vector and return a concatenation
of each result vector of each result vector
@ -537,7 +548,7 @@ fn foldl<T: copy, U>(z: T, v: [const U], p: fn(T, U) -> T) -> T {
} }
#[doc = "Reduce a vector from right to left"] #[doc = "Reduce a vector from right to left"]
fn foldr<T, U: copy>(v: [const T], z: U, p: fn(T, U) -> U) -> U { fn foldr<T, U: copy>(v: [const T]/&, z: U, p: fn(T, U) -> U) -> U {
let mut accum = z; let mut accum = z;
riter(v) { |elt| riter(v) { |elt|
accum = p(elt, accum); accum = p(elt, accum);
@ -550,7 +561,7 @@ Return true if a predicate matches any elements
If the vector contains no elements then false is returned. If the vector contains no elements then false is returned.
"] "]
fn any<T>(v: [T], f: fn(T) -> bool) -> bool { fn any<T>(v: [const T]/&, f: fn(T) -> bool) -> bool {
for each(v) {|elem| if f(elem) { ret true; } } for each(v) {|elem| if f(elem) { ret true; } }
ret false; ret false;
} }
@ -560,7 +571,7 @@ Return true if a predicate matches any elements in both vectors.
If the vectors contains no elements then false is returned. If the vectors contains no elements then false is returned.
"] "]
fn any2<T, U>(v0: [const T], v1: [U], f: fn(T, U) -> bool) -> bool { fn any2<T, U>(v0: [const T]/&, v1: [const U]/&, f: fn(T, U) -> bool) -> bool {
let v0_len = len(v0); let v0_len = len(v0);
let v1_len = len(v1); let v1_len = len(v1);
let mut i = 0u; let mut i = 0u;
@ -576,7 +587,7 @@ Return true if a predicate matches all elements
If the vector contains no elements then true is returned. If the vector contains no elements then true is returned.
"] "]
fn all<T>(v: [T], f: fn(T) -> bool) -> bool { fn all<T>(v: [const T]/&, f: fn(T) -> bool) -> bool {
for each(v) {|elem| if !f(elem) { ret false; } } for each(v) {|elem| if !f(elem) { ret false; } }
ret true; ret true;
} }
@ -586,7 +597,7 @@ Return true if a predicate matches all elements
If the vector contains no elements then true is returned. If the vector contains no elements then true is returned.
"] "]
fn alli<T>(v: [T], f: fn(uint, T) -> bool) -> bool { fn alli<T>(v: [const T]/&, f: fn(uint, T) -> bool) -> bool {
for eachi(v) {|i, elem| if !f(i, elem) { ret false; } } for eachi(v) {|i, elem| if !f(i, elem) { ret false; } }
ret true; ret true;
} }
@ -596,7 +607,7 @@ Return true if a predicate matches all elements in both vectors.
If the vectors are not the same size then false is returned. If the vectors are not the same size then false is returned.
"] "]
fn all2<T, U>(v0: [const T], v1: [const U], f: fn(T, U) -> bool) -> bool { fn all2<T, U>(v0: [const T]/&, v1: [const U]/&, f: fn(T, U) -> bool) -> bool {
let v0_len = len(v0); let v0_len = len(v0);
if v0_len != len(v1) { ret false; } if v0_len != len(v1) { ret false; }
let mut i = 0u; let mut i = 0u;
@ -899,7 +910,7 @@ Iterates over a vector in reverse
Iterates over vector `v` and, for each element, calls function `f` with the Iterates over vector `v` and, for each element, calls function `f` with the
element's value. element's value.
"] "]
fn riter<T>(v: [const T], f: fn(T)) { fn riter<T>(v: [const T]/&, f: fn(T)) {
riteri(v) { |_i, v| f(v) } riteri(v) { |_i, v| f(v) }
} }
@ -909,7 +920,7 @@ Iterates over a vector's elements and indexes in reverse
Iterates over vector `v` and, for each element, calls function `f` with the Iterates over vector `v` and, for each element, calls function `f` with the
element's value and index. element's value and index.
"] "]
fn riteri<T>(v: [const T], f: fn(uint, T)) { fn riteri<T>(v: [const T]/&, f: fn(uint, T)) {
let mut i = len(v); let mut i = len(v);
while 0u < i { while 0u < i {
i -= 1u; i -= 1u;
@ -1115,8 +1126,7 @@ impl extensions<T> for [T] {
and return the results and return the results
"] "]
fn mapi<U>(f: fn(uint, T) -> U) -> [U] { fn mapi<U>(f: fn(uint, T) -> U) -> [U] {
let mut i = 0u; mapi(self, f)
self.map { |e| i += 1u; f(i - 1u, e) }
} }
#[doc = "Returns true if the function returns true for all elements. #[doc = "Returns true if the function returns true for all elements.

View file

@ -257,10 +257,11 @@ fn finish<T: qq_helper>
); );
let mut rcall = pcall; let mut rcall = pcall;
if (g_len > 0u) { if (g_len > 0u) {
let gather = copy qcx.gather;
rcall = mk_call(cx,sp, rcall = mk_call(cx,sp,
["syntax", "ext", "qquote", "replace"], ["syntax", "ext", "qquote", "replace"],
[pcall, [pcall,
mk_vec_e(cx,sp, vec::map(copy qcx.gather) {|g| mk_vec_e(cx,sp, vec::map(gather) {|g|
mk_call(cx,sp, mk_call(cx,sp,
["syntax", "ext", "qquote", g.constr], ["syntax", "ext", "qquote", g.constr],
[g.e])}), [g.e])}),

View file

@ -213,7 +213,8 @@ fn run_tests_console(opts: test_opts,
fn print_failures(st: console_test_state) { fn print_failures(st: console_test_state) {
st.out.write_line("\nfailures:"); st.out.write_line("\nfailures:");
let failures = vec::map(copy st.failures) {|test| test.name}; let failures = copy st.failures;
let failures = vec::map(failures) {|test| test.name};
let failures = sort::merge_sort(str::le, failures); let failures = sort::merge_sort(str::le, failures);
for vec::each(failures) {|name| for vec::each(failures) {|name|
st.out.write_line(#fmt[" %s", name]); st.out.write_line(#fmt[" %s", name]);

View file

@ -4012,7 +4012,8 @@ fn trans_block_cleanups_(bcx: block, cleanup_cx: block, is_lpad: bool) ->
let mut bcx = bcx; let mut bcx = bcx;
alt check cleanup_cx.kind { alt check cleanup_cx.kind {
block_scope({cleanups, _}) { block_scope({cleanups, _}) {
vec::riter(copy cleanups) {|cu| let cleanups = copy cleanups;
vec::riter(cleanups) {|cu|
alt cu { alt cu {
clean(cfn, cleanup_type) | clean_temp(_, cfn, cleanup_type) { clean(cfn, cleanup_type) | clean_temp(_, cfn, cleanup_type) {
// Some types don't need to be cleaned up during // Some types don't need to be cleaned up during