Skip to content

Convert vector functions to methods, part 2 #7487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 1, 2013
4 changes: 2 additions & 2 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
fatal(~"process did not return an error status");
}

let prefixes = vec::map(expected_errors, |ee| {
let prefixes = expected_errors.iter().transform(|ee| {
fmt!("%s:%u:", testfile.to_str(), ee.line)
});
}).collect::<~[~str]>();

// Scan and extract our error/warning messages,
// which look like:
Expand Down
38 changes: 26 additions & 12 deletions src/etc/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ def ch_prefix(ix):

def emit_bsearch_range_table(f):
f.write("""
pure fn bsearch_range_table(c: char, r: &[(char,char)]) -> bool {
use cmp::{EQ, LT, GT};
use vec::bsearch;
fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
use cmp::{Equal, Less, Greater};
use vec::ImmutableVector;
use option::None;
(do bsearch(r) |&(lo,hi)| {
if lo <= c && c <= hi { EQ }
else if hi < c { LT }
else { GT }
(do r.bsearch |&(lo,hi)| {
if lo <= c && c <= hi { Equal }
else if hi < c { Less }
else { Greater }
}) != None
}\n\n
""");
Expand All @@ -140,15 +140,15 @@ def emit_property_module(f, mod, tbl):
keys.sort()
emit_bsearch_range_table(f);
for cat in keys:
f.write(" const %s_table : &[(char,char)] = &[\n" % cat)
f.write(" static %s_table : &'static [(char,char)] = &[\n" % cat)
ix = 0
for pair in tbl[cat]:
f.write(ch_prefix(ix))
f.write("(%s, %s)" % (escape_char(pair[0]), escape_char(pair[1])))
ix += 1
f.write("\n ];\n\n")

f.write(" pub pure fn %s(c: char) -> bool {\n" % cat)
f.write(" pub fn %s(c: char) -> bool {\n" % cat)
f.write(" bsearch_range_table(c, %s_table)\n" % cat)
f.write(" }\n\n")
f.write("}\n")
Expand All @@ -159,7 +159,7 @@ def emit_property_module_old(f, mod, tbl):
keys = tbl.keys()
keys.sort()
for cat in keys:
f.write(" pure fn %s(c: char) -> bool {\n" % cat)
f.write(" fn %s(c: char) -> bool {\n" % cat)
f.write(" ret alt c {\n")
prefix = ' '
for pair in tbl[cat]:
Expand Down Expand Up @@ -236,8 +236,22 @@ def emit_decomp_module(f, canon, compat):

(canon_decomp, compat_decomp, gencats) = load_unicode_data("UnicodeData.txt")

# Explain that the source code was generated by this script.
rf.write('// The following code was generated by "src/etc/unicode.py"\n\n')
# Preamble
rf.write('''// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// The following code was generated by "src/etc/unicode.py"

#[allow(missing_doc)];

''')

emit_property_module(rf, "general_category", gencats)

Expand Down
9 changes: 4 additions & 5 deletions src/libextra/fileinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ total line count).
use std::io::ReaderUtil;
use std::io;
use std::os;
use std::vec;

/**
A summary of the internal state of a `FileInput` object. `line_num`
Expand Down Expand Up @@ -353,13 +352,13 @@ a literal `-`.
*/
// XXX: stupid, unclear name
pub fn pathify(vec: &[~str], stdin_hyphen : bool) -> ~[Option<Path>] {
vec::map(vec, |&str : & ~str| {
if stdin_hyphen && str == ~"-" {
vec.iter().transform(|str| {
if stdin_hyphen && "-" == *str {
None
} else {
Some(Path(str))
Some(Path(*str))
}
})
}).collect()
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/libextra/getopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,9 @@ pub mod groups {
*/
pub fn usage(brief: &str, opts: &[OptGroup]) -> ~str {

let desc_sep = ~"\n" + " ".repeat(24);
let desc_sep = "\n" + " ".repeat(24);

let rows = vec::map(opts, |optref| {
let mut rows = opts.iter().transform(|optref| {
let OptGroup{short_name: short_name,
long_name: long_name,
hint: hint,
Expand Down Expand Up @@ -669,7 +669,7 @@ pub mod groups {

return str::to_owned(brief) +
"\n\nOptions:\n" +
rows.connect("\n") +
rows.collect::<~[~str]>().connect("\n") +
"\n\n";
}
} // end groups module
Expand Down
10 changes: 5 additions & 5 deletions src/libextra/num/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,13 @@ impl Mul<BigUint, BigUint> for BigUint {
if n == 1 { return copy *a; }

let mut carry = 0;
let prod = do vec::map(a.data) |ai| {
let prod = do a.data.iter().transform |ai| {
let (hi, lo) = BigDigit::from_uint(
(*ai as uint) * (n as uint) + (carry as uint)
);
carry = hi;
lo
};
}.collect::<~[BigDigit]>();
if carry == 0 { return BigUint::new(prod) };
return BigUint::new(prod + [carry]);
}
Expand Down Expand Up @@ -618,13 +618,13 @@ impl BigUint {
if n_bits == 0 || self.is_zero() { return copy *self; }

let mut carry = 0;
let shifted = do vec::map(self.data) |elem| {
let shifted = do self.data.iter().transform |elem| {
let (hi, lo) = BigDigit::from_uint(
(*elem as uint) << n_bits | (carry as uint)
);
carry = hi;
lo
};
}.collect::<~[BigDigit]>();
if carry == 0 { return BigUint::new(shifted); }
return BigUint::new(shifted + [carry]);
}
Expand Down Expand Up @@ -1172,7 +1172,7 @@ mod biguint_tests {

#[test]
fn test_cmp() {
let data = [ &[], &[1], &[2], &[-1], &[0, 1], &[2, 1], &[1, 1, 1] ]
let data: ~[BigUint] = [ &[], &[1], &[2], &[-1], &[0, 1], &[2, 1], &[1, 1, 1] ]
.map(|v| BigUint::from_slice(*v));
for data.iter().enumerate().advance |(i, ni)| {
for data.slice(i, data.len()).iter().enumerate().advance |(j0, nj)| {
Expand Down
6 changes: 3 additions & 3 deletions src/libextra/par.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn map<A:Copy + Send,B:Copy + Send>(
vec::concat(map_slices(xs, || {
let f = fn_factory();
let result: ~fn(uint, &[A]) -> ~[B] =
|_, slice| vec::map(slice, |x| f(x));
|_, slice| slice.iter().transform(|x| f(x)).collect();
result
}))
}
Expand All @@ -104,9 +104,9 @@ pub fn mapi<A:Copy + Send,B:Copy + Send>(
let slices = map_slices(xs, || {
let f = fn_factory();
let result: ~fn(uint, &[A]) -> ~[B] = |base, slice| {
vec::mapi(slice, |i, x| {
slice.iter().enumerate().transform(|(i, x)| {
f(i + base, x)
})
}).collect()
};
result
});
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<T:Ord> PriorityQueue<T> {
let mut end = q.len();
while end > 1 {
end -= 1;
vec::swap(q.data, 0, end);
q.data.swap(0, end);
q.siftdown_range(0, end)
}
q.to_vec()
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ impl ToStr for Version {
let s = if self.pre.is_empty() {
s
} else {
s + "-" + self.pre.map(|i| i.to_str()).connect(".")
fmt!("%s-%s", s, self.pre.map(|i| i.to_str()).connect("."))
};
if self.build.is_empty() {
s
} else {
s + "+" + self.build.map(|i| i.to_str()).connect(".")
fmt!("%s+%s", s, self.build.map(|i| i.to_str()).connect("."))
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/libextra/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::cmp;
use std::container::{Container, Mutable, Map, Set};
use std::uint;
use std::util::replace;
use std::vec;

#[allow(missing_doc)]
pub struct SmallIntMap<T> {
Expand Down Expand Up @@ -86,7 +85,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
let exists = self.contains_key(&key);
let len = self.v.len();
if len <= key {
vec::grow_fn(&mut self.v, key - len + 1, |_| None);
self.v.grow_fn(key - len + 1, |_| None);
}
self.v[key] = Some(value);
!exists
Expand Down Expand Up @@ -383,8 +382,6 @@ mod test_set {

use super::SmallIntSet;

use std::vec;

#[test]
fn test_disjoint() {
let mut xs = SmallIntSet::new();
Expand Down Expand Up @@ -456,7 +453,7 @@ mod test_set {
let mut i = 0;
let expected = [3, 5, 11, 77];
for a.intersection(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
Expand All @@ -479,7 +476,7 @@ mod test_set {
let mut i = 0;
let expected = [1, 5, 11];
for a.difference(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
Expand All @@ -504,7 +501,7 @@ mod test_set {
let mut i = 0;
let expected = [1, 5, 11, 14, 22];
for a.symmetric_difference(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
Expand Down Expand Up @@ -533,7 +530,7 @@ mod test_set {
let mut i = 0;
let expected = [1, 3, 5, 9, 11, 13, 16, 19, 24];
for a.union(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
Expand Down
Loading