Skip to content

Commit df40aec

Browse files
committed
libstd: Add some functionality to Vec<T>
1 parent c81b3fb commit df40aec

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/libstd/hash/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ use option::{Option, Some, None};
7070
use rc::Rc;
7171
use str::{Str, StrSlice};
7272
use vec::{Vector, ImmutableVector};
73+
use vec_ng::Vec;
7374

7475
/// Reexport the `sip::hash` function as our default hasher.
7576
pub use hash = self::sip::hash;
@@ -207,6 +208,13 @@ impl<S: Writer, T: Hash<S>> Hash<S> for ~[T] {
207208
}
208209
}
209210

211+
impl<S: Writer, T: Hash<S>> Hash<S> for Vec<T> {
212+
#[inline]
213+
fn hash(&self, state: &mut S) {
214+
self.as_slice().hash(state);
215+
}
216+
}
217+
210218
impl<'a, S: Writer, T: Hash<S>> Hash<S> for &'a T {
211219
#[inline]
212220
fn hash(&self, state: &mut S) {

src/libstd/vec_ng.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use cast::{forget, transmute};
1515
use clone::Clone;
1616
use cmp::{Eq, Ordering, TotalEq, TotalOrd};
1717
use container::Container;
18+
use default::Default;
1819
use iter::{DoubleEndedIterator, FromIterator, Iterator};
1920
use libc::{free, c_void};
2021
use mem::{size_of, move_val_init};
@@ -26,7 +27,8 @@ use ptr::RawPtr;
2627
use ptr;
2728
use rt::global_heap::{malloc_raw, realloc_raw};
2829
use raw::Slice;
29-
use vec::{ImmutableVector, Items, MutItems, MutableVector, RevItems};
30+
use vec::{ImmutableEqVector, ImmutableVector, Items, MutItems, MutableVector};
31+
use vec::{RevItems};
3032

3133
pub struct Vec<T> {
3234
priv len: uint,
@@ -340,6 +342,18 @@ impl<T> Vec<T> {
340342
pub fn slice_from<'a>(&'a self, start: uint) -> &'a [T] {
341343
self.as_slice().slice_from(start)
342344
}
345+
346+
#[inline]
347+
pub fn init<'a>(&'a self) -> &'a [T] {
348+
self.slice(0, self.len() - 1)
349+
}
350+
}
351+
352+
impl<T:Eq> Vec<T> {
353+
/// Return true if a vector contains an element with the given value
354+
pub fn contains(&self, x: &T) -> bool {
355+
self.as_slice().contains(x)
356+
}
343357
}
344358

345359
#[inline]
@@ -348,6 +362,14 @@ pub fn append<T:Clone>(mut first: Vec<T>, second: &[T]) -> Vec<T> {
348362
first
349363
}
350364

365+
/// Appends one element to the vector provided. The vector itself is then
366+
/// returned for use again.
367+
#[inline]
368+
pub fn append_one<T>(mut lhs: Vec<T>, x: T) -> Vec<T> {
369+
lhs.push(x);
370+
lhs
371+
}
372+
351373
#[unsafe_destructor]
352374
impl<T> Drop for Vec<T> {
353375
fn drop(&mut self) {
@@ -360,6 +382,12 @@ impl<T> Drop for Vec<T> {
360382
}
361383
}
362384

385+
impl<T> Default for Vec<T> {
386+
fn default() -> Vec<T> {
387+
Vec::new()
388+
}
389+
}
390+
363391
pub struct MoveItems<T> {
364392
priv allocation: *mut c_void, // the block of memory allocated for the vector
365393
priv iter: Items<'static, T>

0 commit comments

Comments
 (0)