From df40aeccdbfcfb0cc7851c6df24f28fbeccfe046 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Fri, 28 Feb 2014 12:55:51 -0800 Subject: libstd: Add some functionality to `Vec` --- src/libstd/hash/mod.rs | 8 ++++++++ src/libstd/vec_ng.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/hash/mod.rs b/src/libstd/hash/mod.rs index 027a1d9010e..0d319c5d74e 100644 --- a/src/libstd/hash/mod.rs +++ b/src/libstd/hash/mod.rs @@ -70,6 +70,7 @@ use option::{Option, Some, None}; 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 @@ impl> Hash for ~[T] { } } +impl> Hash for Vec { + #[inline] + fn hash(&self, state: &mut S) { + self.as_slice().hash(state); + } +} + impl<'a, S: Writer, T: Hash> Hash for &'a T { #[inline] fn hash(&self, state: &mut S) { diff --git a/src/libstd/vec_ng.rs b/src/libstd/vec_ng.rs index 52d3405f8c1..6cc3ccc3452 100644 --- a/src/libstd/vec_ng.rs +++ b/src/libstd/vec_ng.rs @@ -15,6 +15,7 @@ use cast::{forget, transmute}; 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::RawPtr; 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 { priv len: uint, @@ -340,6 +342,18 @@ impl Vec { 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 Vec { + /// 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(mut first: Vec, second: &[T]) -> Vec { first } +/// Appends one element to the vector provided. The vector itself is then +/// returned for use again. +#[inline] +pub fn append_one(mut lhs: Vec, x: T) -> Vec { + lhs.push(x); + lhs +} + #[unsafe_destructor] impl Drop for Vec { fn drop(&mut self) { @@ -360,6 +382,12 @@ impl Drop for Vec { } } +impl Default for Vec { + fn default() -> Vec { + Vec::new() + } +} + pub struct MoveItems { priv allocation: *mut c_void, // the block of memory allocated for the vector priv iter: Items<'static, T> -- cgit 1.4.1-3-g733a5