Skip to content

Commit 4420617

Browse files
pongadalexcrichton
authored andcommitted
---
yaml --- r: 150590 b: refs/heads/try2 c: bf1ffaf h: refs/heads/master v: v3
1 parent 325d3a5 commit 4420617

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: d250ec0bddf7ba84e34e893621234af3ebcfbca8
8+
refs/heads/try2: bf1ffaf5f48f925bfb62f9dca3bc73cfcc246148
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/vec.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use cmp::{Ord, Eq, Ordering, TotalEq, TotalOrd};
1515
use container::{Container, Mutable};
1616
use default::Default;
1717
use fmt;
18-
use iter::{DoubleEndedIterator, FromIterator, Extendable, Iterator};
18+
use iter::{DoubleEndedIterator, FromIterator, Extendable, Iterator, range};
1919
use libc::{free, c_void};
2020
use mem::{size_of, move_val_init};
2121
use mem;
@@ -1135,6 +1135,56 @@ impl<T> Vec<T> {
11351135
pub fn as_mut_ptr(&mut self) -> *mut T {
11361136
self.as_mut_slice().as_mut_ptr()
11371137
}
1138+
1139+
/// Retains only the elements specified by the predicate.
1140+
///
1141+
/// In other words, remove all elements `e` such that `f(&e)` returns false.
1142+
/// This method operates in place and preserves the order the retained elements.
1143+
///
1144+
/// # Example
1145+
///
1146+
/// ```rust
1147+
/// let mut vec = vec!(1i, 2, 3, 4);
1148+
/// vec.retain(|x| x%2 == 0);
1149+
/// assert_eq!(vec, vec!(2, 4));
1150+
/// ```
1151+
pub fn retain(&mut self, f: |&T| -> bool) {
1152+
let len = self.len();
1153+
let mut del = 0u;
1154+
{
1155+
let v = self.as_mut_slice();
1156+
1157+
for i in range(0u, len) {
1158+
if !f(&v[i]) {
1159+
del += 1;
1160+
} else if del > 0 {
1161+
v.swap(i-del, i);
1162+
}
1163+
}
1164+
}
1165+
if del > 0 {
1166+
self.truncate(len - del);
1167+
}
1168+
}
1169+
1170+
/// Expands a vector in place, initializing the new elements to the result of a function.
1171+
///
1172+
/// The vector is grown by `n` elements. The i-th new element are initialized to the value
1173+
/// returned by `f(i)` where `i` is in the range [0, n).
1174+
///
1175+
/// # Example
1176+
///
1177+
/// ```rust
1178+
/// let mut vec = vec!(0u, 1);
1179+
/// vec.grow_fn(3, |i| i);
1180+
/// assert_eq!(vec, vec!(0, 1, 0, 1, 2));
1181+
/// ```
1182+
pub fn grow_fn(&mut self, n: uint, f: |uint| -> T) {
1183+
self.reserve_additional(n);
1184+
for i in range(0u, n) {
1185+
self.push(f(i));
1186+
}
1187+
}
11381188
}
11391189

11401190
impl<T:TotalOrd> Vec<T> {
@@ -1523,4 +1573,17 @@ mod tests {
15231573
v.clone_from(&three);
15241574
assert_eq!(v, three)
15251575
}
1576+
1577+
fn test_grow_fn() {
1578+
let mut v = Vec::from_slice([0u, 1]);
1579+
v.grow_fn(3, |i| i);
1580+
assert!(v == Vec::from_slice([0u, 1, 0, 1, 2]));
1581+
}
1582+
1583+
#[test]
1584+
fn test_retain() {
1585+
let mut vec = Vec::from_slice([1u, 2, 3, 4]);
1586+
vec.retain(|x| x%2 == 0);
1587+
assert!(vec == Vec::from_slice([2u, 4]));
1588+
}
15261589
}

0 commit comments

Comments
 (0)