]> git.lizzy.rs Git - rust.git/commitdiff
libstd: Add some functionality to `Vec<T>`
authorPatrick Walton <pcwalton@mimiga.net>
Fri, 28 Feb 2014 20:55:51 +0000 (12:55 -0800)
committerPatrick Walton <pcwalton@mimiga.net>
Sun, 2 Mar 2014 06:40:52 +0000 (22:40 -0800)
src/libstd/hash/mod.rs
src/libstd/vec_ng.rs

index 027a1d9010e3e6287ecbfc30f29569984fc478e0..0d319c5d74ef996957ec67943fccb21e87c2059b 100644 (file)
@@ -70,6 +70,7 @@
 use rc::Rc;
 use str::{Str, StrSlice};
 use vec::{Vector, ImmutableVector};
+use vec_ng::Vec;
 
 /// Reexport the `sip::hash` function as our default hasher.
 pub use hash = self::sip::hash;
@@ -207,6 +208,13 @@ fn hash(&self, state: &mut S) {
     }
 }
 
+impl<S: Writer, T: Hash<S>> Hash<S> for Vec<T> {
+    #[inline]
+    fn hash(&self, state: &mut S) {
+        self.as_slice().hash(state);
+    }
+}
+
 impl<'a, S: Writer, T: Hash<S>> Hash<S> for &'a T {
     #[inline]
     fn hash(&self, state: &mut S) {
index 52d3405f8c1480f1eec2165350fb774c5c5b2a45..6cc3ccc345237f800f63a06ae38bf0f0f9585a4c 100644 (file)
@@ -15,6 +15,7 @@
 use clone::Clone;
 use cmp::{Eq, Ordering, TotalEq, TotalOrd};
 use container::Container;
+use default::Default;
 use iter::{DoubleEndedIterator, FromIterator, Iterator};
 use libc::{free, c_void};
 use mem::{size_of, move_val_init};
@@ -26,7 +27,8 @@
 use ptr;
 use rt::global_heap::{malloc_raw, realloc_raw};
 use raw::Slice;
-use vec::{ImmutableVector, Items, MutItems, MutableVector, RevItems};
+use vec::{ImmutableEqVector, ImmutableVector, Items, MutItems, MutableVector};
+use vec::{RevItems};
 
 pub struct Vec<T> {
     priv len: uint,
@@ -340,6 +342,18 @@ pub fn push_all_move(&mut self, other: Vec<T>) {
     pub fn slice_from<'a>(&'a self, start: uint) -> &'a [T] {
         self.as_slice().slice_from(start)
     }
+
+    #[inline]
+    pub fn init<'a>(&'a self) -> &'a [T] {
+        self.slice(0, self.len() - 1)
+    }
+}
+
+impl<T:Eq> Vec<T> {
+    /// Return true if a vector contains an element with the given value
+    pub fn contains(&self, x: &T) -> bool {
+        self.as_slice().contains(x)
+    }
 }
 
 #[inline]
@@ -348,6 +362,14 @@ pub fn append<T:Clone>(mut first: Vec<T>, second: &[T]) -> Vec<T> {
     first
 }
 
+/// Appends one element to the vector provided. The vector itself is then
+/// returned for use again.
+#[inline]
+pub fn append_one<T>(mut lhs: Vec<T>, x: T) -> Vec<T> {
+    lhs.push(x);
+    lhs
+}
+
 #[unsafe_destructor]
 impl<T> Drop for Vec<T> {
     fn drop(&mut self) {
@@ -360,6 +382,12 @@ fn drop(&mut self) {
     }
 }
 
+impl<T> Default for Vec<T> {
+    fn default() -> Vec<T> {
+        Vec::new()
+    }
+}
+
 pub struct MoveItems<T> {
     priv allocation: *mut c_void, // the block of memory allocated for the vector
     priv iter: Items<'static, T>