about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-04-04 16:01:44 -0700
committerbors <bors@rust-lang.org>2014-04-04 16:01:44 -0700
commit4cf8d8ce69c1d1d10e90b04230d4c4e8dbb67bcc (patch)
tree61f1237167d3663ba874e2b802cd9f5dd3c57041 /src/libstd
parente5f1b9f6dc9418325f83d9766c7cfab30cb48018 (diff)
parent6d43138b75f009bfa6f0774953ef55fd91e9760a (diff)
downloadrust-4cf8d8ce69c1d1d10e90b04230d4c4e8dbb67bcc.tar.gz
rust-4cf8d8ce69c1d1d10e90b04230d4c4e8dbb67bcc.zip
auto merge of #13326 : alexcrichton/rust/rollup, r=alexcrichton
Closes #13313 (Fix typo in README.md)
Closes #13311 (Fix inner attribute syntax from `#[foo];` to `#![foo]`)
Closes #13309 (Add stdlib docs to the Linux binary tarball.)
Closes #13308 (syntax: remove obsolete mutability from ExprVec and ExprRepeat.)
Closes #13306 (TrieSet should impl Set/MutableSet; add with_capacity to PriorityQueue/SmallIntMap)
Closes #13303 (Register new snapshots)
Closes #13274 (Added grow_fn and retain to Vec)

*Issues Closed*

Closes #13249
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/lib.rs4
-rw-r--r--src/libstd/vec.rs65
2 files changed, 65 insertions, 4 deletions
diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs
index f4f5be4d37a..42d35b60896 100644
--- a/src/libstd/lib.rs
+++ b/src/libstd/lib.rs
@@ -57,9 +57,7 @@
 // Don't link to std. We are std.
 #![no_std]
 
-// #![deny(missing_doc)] // NOTE: uncomment after a stage0 snap
-#![allow(missing_doc)] // NOTE: remove after a stage0 snap
-#![allow(visible_private_types)] // NOTE: remove after a stage0 snap
+#![deny(missing_doc)]
 #![allow(unknown_features)] // NOTE: remove after a stage0 snap
 
 // When testing libstd, bring in libuv as the I/O backend so tests can print
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index e414ff25d43..30416b28241 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -15,7 +15,7 @@ use cmp::{Ord, Eq, Ordering, TotalEq, TotalOrd};
 use container::{Container, Mutable};
 use default::Default;
 use fmt;
-use iter::{DoubleEndedIterator, FromIterator, Extendable, Iterator};
+use iter::{DoubleEndedIterator, FromIterator, Extendable, Iterator, range};
 use libc::{free, c_void};
 use mem::{size_of, move_val_init};
 use mem;
@@ -1135,6 +1135,56 @@ impl<T> Vec<T> {
     pub fn as_mut_ptr(&mut self) -> *mut T {
         self.as_mut_slice().as_mut_ptr()
     }
+
+    /// Retains only the elements specified by the predicate.
+    ///
+    /// In other words, remove all elements `e` such that `f(&e)` returns false.
+    /// This method operates in place and preserves the order the retained elements.
+    ///
+    /// # Example
+    ///
+    /// ```rust
+    /// let mut vec = vec!(1i, 2, 3, 4);
+    /// vec.retain(|x| x%2 == 0);
+    /// assert_eq!(vec, vec!(2, 4));
+    /// ```
+    pub fn retain(&mut self, f: |&T| -> bool) {
+        let len = self.len();
+        let mut del = 0u;
+        {
+            let v = self.as_mut_slice();
+
+            for i in range(0u, len) {
+                if !f(&v[i]) {
+                    del += 1;
+                } else if del > 0 {
+                    v.swap(i-del, i);
+                }
+            }
+        }
+        if del > 0 {
+            self.truncate(len - del);
+        }
+    }
+
+    /// Expands a vector in place, initializing the new elements to the result of a function.
+    ///
+    /// The vector is grown by `n` elements. The i-th new element are initialized to the value
+    /// returned by `f(i)` where `i` is in the range [0, n).
+    ///
+    /// # Example
+    ///
+    /// ```rust
+    /// let mut vec = vec!(0u, 1);
+    /// vec.grow_fn(3, |i| i);
+    /// assert_eq!(vec, vec!(0, 1, 0, 1, 2));
+    /// ```
+    pub fn grow_fn(&mut self, n: uint, f: |uint| -> T) {
+        self.reserve_additional(n);
+        for i in range(0u, n) {
+            self.push(f(i));
+        }
+    }
 }
 
 impl<T:TotalOrd> Vec<T> {
@@ -1523,4 +1573,17 @@ mod tests {
         v.clone_from(&three);
         assert_eq!(v, three)
     }
+
+    fn test_grow_fn() {
+        let mut v = Vec::from_slice([0u, 1]);
+        v.grow_fn(3, |i| i);
+        assert!(v == Vec::from_slice([0u, 1, 0, 1, 2]));
+    }
+
+    #[test]
+    fn test_retain() {
+        let mut vec = Vec::from_slice([1u, 2, 3, 4]);
+        vec.retain(|x| x%2 == 0);
+        assert!(vec == Vec::from_slice([2u, 4]));
+    }
 }