Permalink
Browse files

De-mode vec::map, vec::eachi, vec::rev_each, vec::rev_eachi

  • Loading branch information...
1 parent f3c31a0 commit 3d59ac3a1989c2d233b04cc8adc9b058690c2544 @nikomatsakis nikomatsakis committed Sep 22, 2012
Showing with 408 additions and 427 deletions.
  1. +3 −3 doc/tutorial.md
  2. +3 −3 src/cargo/cargo.rs
  3. +3 −4 src/libcore/dlist.rs
  4. +22 −8 src/libcore/dvec.rs
  5. +4 −5 src/libcore/io.rs
  6. +2 −2 src/libcore/iter-trait.rs
  7. +6 −6 src/libcore/iter.rs
  8. +2 −3 src/libcore/os.rs
  9. +2 −4 src/libcore/result.rs
  10. +6 −9 src/libcore/str.rs
  11. +93 −117 src/libcore/vec.rs
  12. +1 −2 src/libstd/base64.rs
  13. +6 −6 src/libstd/deque.rs
  14. +5 −7 src/libstd/map.rs
  15. +1 −1 src/libstd/net_ip.rs
  16. +3 −3 src/libstd/par.rs
  17. +1 −1 src/libstd/rope.rs
  18. +1 −1 src/libstd/serialization.rs
  19. +1 −2 src/libstd/sort.rs
  20. +2 −2 src/libstd/sync.rs
  21. +1 −1 src/libsyntax/ast_map.rs
  22. +1 −1 src/libsyntax/ast_util.rs
  23. +2 −2 src/libsyntax/attr.rs
  24. +5 −5 src/libsyntax/ext/auto_serialize.rs
  25. +1 −1 src/libsyntax/ext/base.rs
  26. +1 −1 src/libsyntax/ext/pipes/ast_builder.rs
  27. +2 −2 src/libsyntax/ext/pipes/pipec.rs
  28. +2 −2 src/libsyntax/ext/pipes/proto.rs
  29. +1 −1 src/libsyntax/ext/qquote.rs
  30. +3 −3 src/libsyntax/ext/simplext.rs
  31. +4 −3 src/libsyntax/ext/source_util.rs
  32. +1 −1 src/libsyntax/ext/tt/macro_rules.rs
  33. +3 −2 src/libsyntax/ext/tt/transcribe.rs
  34. +54 −52 src/libsyntax/fold.rs
  35. +1 −1 src/libsyntax/parse/comments.rs
  36. +2 −2 src/rustc/back/rpath.rs
  37. +1 −1 src/rustc/driver/driver.rs
  38. +3 −3 src/rustc/front/config.rs
  39. +2 −2 src/rustc/metadata/creader.rs
  40. +1 −1 src/rustc/metadata/cstore.rs
  41. +1 −3 src/rustc/metadata/decoder.rs
  42. +1 −1 src/rustc/metadata/encoder.rs
  43. +1 −1 src/rustc/middle/check_alt.rs
  44. +1 −1 src/rustc/middle/const_eval.rs
  45. +10 −10 src/rustc/middle/resolve.rs
  46. +8 −8 src/rustc/middle/trans/alt.rs
  47. +6 −6 src/rustc/middle/trans/base.rs
  48. +2 −2 src/rustc/middle/trans/build.rs
  49. +2 −2 src/rustc/middle/trans/callee.rs
  50. +5 −5 src/rustc/middle/trans/common.rs
  51. +2 −2 src/rustc/middle/trans/consts.rs
  52. +2 −2 src/rustc/middle/trans/expr.rs
  53. +2 −2 src/rustc/middle/trans/foreign.rs
  54. +4 −4 src/rustc/middle/trans/meth.rs
  55. +11 −7 src/rustc/middle/trans/monomorphize.rs
  56. +3 −3 src/rustc/middle/trans/reflect.rs
  57. +1 −1 src/rustc/middle/trans/shape.rs
  58. +1 −1 src/rustc/middle/trans/tvec.rs
  59. +1 −1 src/rustc/middle/trans/type_of.rs
  60. +6 −6 src/rustc/middle/ty.rs
  61. +2 −2 src/rustc/middle/typeck.rs
  62. +3 −3 src/rustc/middle/typeck/astconv.rs
  63. +11 −14 src/rustc/middle/typeck/check.rs
  64. +1 −1 src/rustc/middle/typeck/check/alt.rs
  65. +1 −1 src/rustc/middle/typeck/check/regionmanip.rs
  66. +6 −6 src/rustc/middle/typeck/collect.rs
  67. +1 −2 src/rustc/middle/typeck/infer/region_var_bindings.rs
  68. +1 −4 src/rustc/middle/typeck/infer/sub.rs
  69. +2 −2 src/rustc/util/ppaux.rs
  70. +1 −1 src/rustdoc/attr_pass.rs
  71. +2 −2 src/rustdoc/extract.rs
  72. +3 −3 src/rustdoc/fold.rs
  73. +6 −6 src/rustdoc/markdown_pass.rs
  74. +2 −2 src/test/bench/core-vec-append.rs
  75. +10 −10 src/test/bench/graph500-bfs.rs
  76. +11 −11 src/test/bench/shootout-chameneos-redux.rs
  77. +1 −0 src/test/bench/shootout-k-nucleotide-pipes.rs
  78. +6 −5 src/test/bench/shootout-k-nucleotide.rs
  79. +4 −2 src/test/bench/task-perf-word-count-generic.rs
  80. +1 −1 src/test/run-pass/block-arg.rs
  81. +1 −1 src/test/run-pass/dvec-test.rs
  82. +1 −1 src/test/run-pass/issue-2804.rs
  83. +2 −2 src/test/run-pass/iter-eachi.rs
  84. +1 −1 src/test/run-pass/iter-map-to-vec.rs
View
@@ -426,7 +426,7 @@ annotation:
~~~~
// The type of this vector will be inferred based on its use.
let x = [];
-# vec::map(x, fn&(&&_y:int) -> int { _y });
+# vec::map(x, fn&(_y: &int) -> int { *_y });
// Explicitly say this is a vector of zero integers.
let y: [int * 0] = [];
~~~~
@@ -1242,7 +1242,7 @@ for crayons.each |crayon| {
}
// Map vector elements
-let crayon_names = crayons.map(|v| crayon_to_str(v));
+let crayon_names = crayons.map(|v| crayon_to_str(*v));
let favorite_crayon_name = crayon_names[0];
// Remove whitespace from before and after the string
@@ -1298,7 +1298,7 @@ access local variables in the enclosing scope.
~~~~
let mut max = 0;
-(~[1, 2, 3]).map(|x| if x > max { max = x });
+(~[1, 2, 3]).map(|x| if *x > max { max = *x });
~~~~
Stack closures are very efficient because their environment is
View
@@ -195,17 +195,17 @@ fn is_uuid(id: ~str) -> bool {
match i {
0u => {
- if str::len(part) == 8u {
+ if part.len() == 8u {
correct += 1u;
}
}
1u | 2u | 3u => {
- if str::len(part) == 4u {
+ if part.len() == 4u {
correct += 1u;
}
}
4u => {
- if str::len(part) == 12u {
+ if part.len() == 12u {
correct += 1u;
}
}
View
@@ -437,13 +437,12 @@ impl<T: Copy> DList<T> {
/// Get data at the list's tail, failing if empty. O(1).
pure fn tail() -> T { self.tail_n().data }
/// Get the elements of the list as a vector. O(n).
- pure fn to_vec() -> ~[mut T] {
- let mut v = ~[mut];
+ pure fn to_vec() -> ~[T] {
+ let mut v = vec::with_capacity(self.size);
unsafe {
- vec::reserve(v, self.size);
// Take this out of the unchecked when iter's functions are pure
for self.eachi |index,data| {
- v[index] = data;
+ v[index] = *data;
}
}
move v
View
@@ -117,7 +117,7 @@ priv impl<A> DVec<A> {
impl<A> DVec<A> {
/// Reserves space for N elements
fn reserve(count: uint) {
- vec::reserve(self.data, count)
+ vec::reserve(&mut self.data, count)
}
/**
@@ -243,7 +243,7 @@ impl<A: Copy> DVec<A> {
do self.swap |v| {
let mut v <- v;
let new_len = vec::len(v) + to_idx - from_idx;
- vec::reserve(v, new_len);
+ vec::reserve(&mut v, new_len);
let mut i = from_idx;
while i < to_idx {
vec::push(v, ts[i]);
@@ -313,9 +313,9 @@ impl<A: Copy> DVec<A> {
*/
fn grow_set_elt(idx: uint, initval: A, val: A) {
do self.swap |v| {
- let mut v = vec::to_mut(move v);
+ let mut v = move v;
vec::grow_set(v, idx, initval, val);
- move vec::from_mut(v)
+ move v
}
}
@@ -334,14 +334,28 @@ impl<A: Copy> DVec<A> {
/// Iterates over the elements in reverse order
#[inline(always)]
- fn reach(f: fn(A) -> bool) {
- do self.swap |v| { vec::reach(v, f); move v }
+ fn rev_each(f: fn(v: &A) -> bool) {
+ do self.swap |v| {
+ // FIXME(#2263)---we should be able to write
+ // `vec::rev_each(v, f);` but we cannot write now
+ for vec::rev_each(v) |e| {
+ if !f(e) { break; }
+ }
+ move v
+ }
}
/// Iterates over the elements and indices in reverse order
#[inline(always)]
- fn reachi(f: fn(uint, A) -> bool) {
- do self.swap |v| { vec::reachi(v, f); move v }
+ fn rev_eachi(f: fn(uint, v: &A) -> bool) {
+ do self.swap |v| {
+ // FIXME(#2263)---we should be able to write
+ // `vec::rev_eachi(v, f);` but we cannot write now
+ for vec::rev_eachi(v) |i, e| {
+ if !f(i, e) { break; }
+ }
+ move v
+ }
}
}
View
@@ -52,14 +52,13 @@ trait ReaderUtil {
impl<T: Reader> T : ReaderUtil {
fn read_bytes(len: uint) -> ~[u8] {
- let mut buf = ~[mut];
- vec::reserve(buf, len);
+ let mut buf = vec::with_capacity(len);
unsafe { vec::raw::set_len(buf, len); }
let count = self.read(buf, len);
unsafe { vec::raw::set_len(buf, count); }
- vec::from_mut(move buf)
+ move buf
}
fn read_line() -> ~str {
let mut buf = ~[];
@@ -696,7 +695,7 @@ impl BytesWriter: Writer {
let buf_len = buf.len();
let count = uint::max(buf_len, self.pos + v_len);
- vec::reserve(buf, count);
+ vec::reserve(&mut buf, count);
unsafe { vec::raw::set_len(buf, count); }
let view = vec::mut_view(buf, self.pos, count);
@@ -910,7 +909,7 @@ mod tests {
assert(vec::len(res) == len);
}
assert(vec::slice(ivals, 0u, vec::len(res)) ==
- vec::map(res, |x| x as int));
+ vec::map(res, |x| *x as int));
}
}
let mut i = 0u;
@@ -12,7 +12,7 @@ impl<A> IMPL_T<A>: iter::BaseIter<A> {
}
impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
- pure fn eachi(blk: fn(uint, A) -> bool) { iter::eachi(self, blk) }
+ pure fn eachi(blk: fn(uint, v: &A) -> bool) { iter::eachi(self, blk) }
pure fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) }
pure fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) }
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
@@ -32,7 +32,7 @@ impl<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
pure fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] {
iter::filter_to_vec(self, pred)
}
- pure fn map_to_vec<B>(op: fn(A) -> B) -> ~[B] {
+ pure fn map_to_vec<B>(op: fn(v: &A) -> B) -> ~[B] {
iter::map_to_vec(self, op)
}
pure fn to_vec() -> ~[A] { iter::to_vec(self) }
View
@@ -15,7 +15,7 @@ trait BaseIter<A> {
}
trait ExtendedIter<A> {
- pure fn eachi(blk: fn(uint, A) -> bool);
+ pure fn eachi(blk: fn(uint, v: &A) -> bool);
pure fn all(blk: fn(A) -> bool) -> bool;
pure fn any(blk: fn(A) -> bool) -> bool;
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B;
@@ -36,7 +36,7 @@ trait TimesIx{
trait CopyableIter<A:Copy> {
pure fn filter_to_vec(pred: fn(A) -> bool) -> ~[A];
- pure fn map_to_vec<B>(op: fn(A) -> B) -> ~[B];
+ pure fn map_to_vec<B>(op: fn(v: &A) -> B) -> ~[B];
pure fn to_vec() -> ~[A];
pure fn find(p: fn(A) -> bool) -> Option<A>;
}
@@ -66,10 +66,10 @@ trait Buildable<A> {
builder: fn(push: pure fn(+A))) -> self;
}
-pure fn eachi<A,IA:BaseIter<A>>(self: IA, blk: fn(uint, A) -> bool) {
+pure fn eachi<A,IA:BaseIter<A>>(self: IA, blk: fn(uint, v: &A) -> bool) {
let mut i = 0u;
for self.each |a| {
- if !blk(i, *a) { break; }
+ if !blk(i, a) { break; }
i += 1u;
}
}
@@ -97,11 +97,11 @@ pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(self: IA,
}
}
-pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: IA, op: fn(A) -> B)
+pure fn map_to_vec<A:Copy,B,IA:BaseIter<A>>(self: IA, op: fn(v: &A) -> B)
-> ~[B] {
do vec::build_sized_opt(self.size_hint()) |push| {
for self.each |a| {
- push(op(*a));
+ push(op(a));
}
}
}
View
@@ -637,7 +637,7 @@ fn list_dir(p: &Path) -> ~[~str] {
* This version prepends each entry with the directory.
*/
fn list_dir_path(p: &Path) -> ~[~Path] {
- os::list_dir(p).map(|f| ~p.push(f))
+ os::list_dir(p).map(|f| ~p.push(*f))
}
/// Removes a directory at the specified path
@@ -721,9 +721,8 @@ fn copy_file(from: &Path, to: &Path) -> bool {
fclose(istream);
return false;
}
- let mut buf : ~[mut u8] = ~[mut];
let bufsize = 8192u;
- vec::reserve(buf, bufsize);
+ let mut buf = vec::with_capacity::<u8>(bufsize);
let mut done = false;
let mut ok = true;
while !done {
View
@@ -269,8 +269,7 @@ impl<T: Copy, E: Copy> Result<T, E> {
fn map_vec<T,U:Copy,V:Copy>(
ts: &[T], op: fn((&T)) -> Result<V,U>) -> Result<~[V],U> {
- let mut vs: ~[V] = ~[];
- vec::reserve(vs, vec::len(ts));
+ let mut vs: ~[V] = vec::with_capacity(vec::len(ts));
for vec::each(ts) |t| {
match op(t) {
Ok(v) => vec::push(vs, v),
@@ -306,8 +305,7 @@ fn map_vec2<S,T,U:Copy,V:Copy>(ss: &[S], ts: &[T],
assert vec::same_length(ss, ts);
let n = vec::len(ts);
- let mut vs = ~[];
- vec::reserve(vs, n);
+ let mut vs = vec::with_capacity(n);
let mut i = 0u;
while i < n {
match op(ss[i],ts[i]) {
View
@@ -669,8 +669,8 @@ pure fn lines(s: &str) -> ~[~str] { split_char(s, '\n') }
*/
pure fn lines_any(s: &str) -> ~[~str] {
vec::map(lines(s), |s| {
- let l = len(s);
- let mut cp = copy s;
+ let l = len(*s);
+ let mut cp = copy *s;
if l > 0u && s[l - 1u] == '\r' as u8 {
unsafe { raw::set_len(&mut cp, l - 1u); }
}
@@ -1984,7 +1984,7 @@ pure fn as_buf<T>(s: &str, f: fn(*u8, uint) -> T) -> T {
fn reserve(s: &const ~str, n: uint) {
unsafe {
let v: *mut ~[u8] = cast::transmute(copy s);
- vec::reserve(*v, n + 1);
+ vec::reserve(&mut *v, n + 1);
}
}
@@ -2077,10 +2077,8 @@ mod raw {
/// Create a Rust string from a *u8 buffer of the given length
unsafe fn from_buf_len(buf: *const u8, len: uint) -> ~str {
- let mut v: ~[mut u8] = ~[mut];
- vec::reserve(v, len + 1u);
- vec::as_imm_buf(v, |vbuf, _len| {
- let vbuf = ::cast::transmute_mut_unsafe(vbuf);
+ let mut v: ~[u8] = vec::with_capacity(len + 1);
+ vec::as_mut_buf(v, |vbuf, _len| {
ptr::memcpy(vbuf, buf as *u8, len)
});
vec::raw::set_len(v, len);
@@ -2132,8 +2130,7 @@ mod raw {
assert (begin <= end);
assert (end <= n);
- let mut v = ~[];
- vec::reserve(v, end - begin + 1u);
+ let mut v = vec::with_capacity(end - begin + 1u);
unsafe {
do vec::as_imm_buf(v) |vbuf, _vlen| {
let vbuf = ::cast::transmute_mut_unsafe(vbuf);
Oops, something went wrong.

0 comments on commit 3d59ac3

Please sign in to comment.