diff options
| author | bors <bors@rust-lang.org> | 2014-03-02 08:31:33 -0800 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-03-02 08:31:33 -0800 |
| commit | 910012aabae3dfd4b7190f46e88cde75804b5cb0 (patch) | |
| tree | ac07696b5bb7a8ba6dacd1b2abd3926b59621058 /src/libstd | |
| parent | baf79083aedb8ae64efddbcf28b358841cfd1157 (diff) | |
| parent | 355932407ba324d33cd9353a69203f7f76c059a6 (diff) | |
| download | rust-910012aabae3dfd4b7190f46e88cde75804b5cb0.tar.gz rust-910012aabae3dfd4b7190f46e88cde75804b5cb0.zip | |
auto merge of #12637 : pcwalton/rust/devecing, r=alexcrichton
r? @alexcrichton
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/hash/mod.rs | 8 | ||||
| -rw-r--r-- | src/libstd/vec_ng.rs | 57 |
2 files changed, 64 insertions, 1 deletions
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<S: Writer, T: Hash<S>> Hash<S> for ~[T] { } } +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) { diff --git a/src/libstd/vec_ng.rs b/src/libstd/vec_ng.rs index 52d3405f8c1..9b6acdd9b9e 100644 --- a/src/libstd/vec_ng.rs +++ b/src/libstd/vec_ng.rs @@ -15,6 +15,8 @@ use cast::{forget, transmute}; use clone::Clone; use cmp::{Eq, Ordering, TotalEq, TotalOrd}; use container::Container; +use default::Default; +use fmt; use iter::{DoubleEndedIterator, FromIterator, Iterator}; use libc::{free, c_void}; use mem::{size_of, move_val_init}; @@ -26,7 +28,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<T> { priv len: uint, @@ -80,6 +83,26 @@ impl<T: Clone> Vec<T> { self.push((*element).clone()) } } + + + pub fn grow(&mut self, n: uint, initval: &T) { + let new_len = self.len() + n; + self.reserve(new_len); + let mut i: uint = 0u; + + while i < n { + self.push((*initval).clone()); + i += 1u; + } + } + + pub fn grow_set(&mut self, index: uint, initval: &T, val: T) { + let l = self.len(); + if index >= l { + self.grow(index - l + 1u, initval); + } + *self.get_mut(index) = val; + } } impl<T:Clone> Clone for Vec<T> { @@ -340,6 +363,18 @@ impl<T> 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 +383,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 +403,18 @@ impl<T> Drop for Vec<T> { } } +impl<T> Default for Vec<T> { + fn default() -> Vec<T> { + Vec::new() + } +} + +impl<T:fmt::Show> fmt::Show for Vec<T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.as_slice().fmt(f) + } +} + pub struct MoveItems<T> { priv allocation: *mut c_void, // the block of memory allocated for the vector priv iter: Items<'static, T> |
