Skip to content

Commit a396e1e

Browse files
committed
Convert vec::{grow, grow_fn, grow_set} to methods.
1 parent 2eea642 commit a396e1e

File tree

3 files changed

+48
-68
lines changed

3 files changed

+48
-68
lines changed

src/libextra/smallintmap.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use std::cmp;
2020
use std::container::{Container, Mutable, Map, Set};
2121
use std::uint;
2222
use std::util::replace;
23-
use std::vec;
2423

2524
#[allow(missing_doc)]
2625
pub struct SmallIntMap<T> {
@@ -86,7 +85,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
8685
let exists = self.contains_key(&key);
8786
let len = self.v.len();
8887
if len <= key {
89-
vec::grow_fn(&mut self.v, key - len + 1, |_| None);
88+
self.v.grow_fn(key - len + 1, |_| None);
9089
}
9190
self.v[key] = Some(value);
9291
!exists

src/libstd/vec.rs

+46-63
Original file line numberDiff line numberDiff line change
@@ -363,63 +363,6 @@ pub fn append_one<T>(lhs: ~[T], x: T) -> ~[T] {
363363
v
364364
}
365365

366-
/**
367-
* Expands a vector in place, initializing the new elements to a given value
368-
*
369-
* # Arguments
370-
*
371-
* * v - The vector to grow
372-
* * n - The number of elements to add
373-
* * initval - The value for the new elements
374-
*/
375-
pub fn grow<T:Copy>(v: &mut ~[T], n: uint, initval: &T) {
376-
let new_len = v.len() + n;
377-
v.reserve_at_least(new_len);
378-
let mut i: uint = 0u;
379-
380-
while i < n {
381-
v.push(copy *initval);
382-
i += 1u;
383-
}
384-
}
385-
386-
/**
387-
* Expands a vector in place, initializing the new elements to the result of
388-
* a function
389-
*
390-
* Function `init_op` is called `n` times with the values [0..`n`)
391-
*
392-
* # Arguments
393-
*
394-
* * v - The vector to grow
395-
* * n - The number of elements to add
396-
* * init_op - A function to call to retreive each appended element's
397-
* value
398-
*/
399-
pub fn grow_fn<T>(v: &mut ~[T], n: uint, op: &fn(uint) -> T) {
400-
let new_len = v.len() + n;
401-
v.reserve_at_least(new_len);
402-
let mut i: uint = 0u;
403-
while i < n {
404-
v.push(op(i));
405-
i += 1u;
406-
}
407-
}
408-
409-
/**
410-
* Sets the value of a vector element at a given index, growing the vector as
411-
* needed
412-
*
413-
* Sets the element at position `index` to `val`. If `index` is past the end
414-
* of the vector, expands the vector by replicating `initval` to fill the
415-
* intervening space.
416-
*/
417-
pub fn grow_set<T:Copy>(v: &mut ~[T], index: uint, initval: &T, val: T) {
418-
let l = v.len();
419-
if index >= l { grow(&mut *v, index - l + 1u, initval); }
420-
v[index] = val;
421-
}
422-
423366
// Functional utilities
424367

425368
/// Apply a function to each element of a vector and return the results
@@ -1648,9 +1591,26 @@ impl<T> OwnedVector<T> for ~[T] {
16481591
(lefts, rights)
16491592
}
16501593

1651-
#[inline]
1594+
/**
1595+
* Expands a vector in place, initializing the new elements to the result of
1596+
* a function
1597+
*
1598+
* Function `init_op` is called `n` times with the values [0..`n`)
1599+
*
1600+
* # Arguments
1601+
*
1602+
* * n - The number of elements to add
1603+
* * init_op - A function to call to retreive each appended element's
1604+
* value
1605+
*/
16521606
fn grow_fn(&mut self, n: uint, op: &fn(uint) -> T) {
1653-
grow_fn(self, n, op);
1607+
let new_len = self.len() + n;
1608+
self.reserve_at_least(new_len);
1609+
let mut i: uint = 0u;
1610+
while i < n {
1611+
self.push(op(i));
1612+
i += 1u;
1613+
}
16541614
}
16551615
}
16561616

@@ -1687,14 +1647,37 @@ impl<T:Copy> OwnedCopyableVector<T> for ~[T] {
16871647
}
16881648
}
16891649

1690-
#[inline]
1650+
/**
1651+
* Expands a vector in place, initializing the new elements to a given value
1652+
*
1653+
* # Arguments
1654+
*
1655+
* * n - The number of elements to add
1656+
* * initval - The value for the new elements
1657+
*/
16911658
fn grow(&mut self, n: uint, initval: &T) {
1692-
grow(self, n, initval);
1659+
let new_len = self.len() + n;
1660+
self.reserve_at_least(new_len);
1661+
let mut i: uint = 0u;
1662+
1663+
while i < n {
1664+
self.push(copy *initval);
1665+
i += 1u;
1666+
}
16931667
}
16941668

1695-
#[inline]
1669+
/**
1670+
* Sets the value of a vector element at a given index, growing the vector as
1671+
* needed
1672+
*
1673+
* Sets the element at position `index` to `val`. If `index` is past the end
1674+
* of the vector, expands the vector by replicating `initval` to fill the
1675+
* intervening space.
1676+
*/
16961677
fn grow_set(&mut self, index: uint, initval: &T, val: T) {
1697-
grow_set(self, index, initval, val);
1678+
let l = self.len();
1679+
if index >= l { self.grow(index - l + 1u, initval); }
1680+
self[index] = val;
16981681
}
16991682
}
17001683

src/test/run-pass/issue-3563-3.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ fn AsciiArt(width: uint, height: uint, fill: char) -> AsciiArt {
6969
// blank characters for each position in our canvas.
7070
let mut lines = do vec::build_sized(height) |push| {
7171
for height.times {
72-
let mut line = ~[];
73-
vec::grow_set(&mut line, width-1, &'.', '.');
74-
push(line);
72+
push(vec::from_elem(width, '.'));
7573
}
7674
};
7775

0 commit comments

Comments
 (0)