|
11 | 11 | // Migrate documentation over from `std::vec` when it is removed.
|
12 | 12 | #[doc(hidden)];
|
13 | 13 |
|
14 |
| -use ops::Drop; |
15 |
| -use option::{None, Option, Some}; |
| 14 | +use cast::{forget, transmute}; |
16 | 15 | use clone::Clone;
|
17 |
| -use iter::{DoubleEndedIterator, Iterator}; |
18 |
| -use num::CheckedMul; |
| 16 | +use cmp::{Eq, Ordering, TotalEq, TotalOrd}; |
19 | 17 | use container::Container;
|
| 18 | +use iter::{DoubleEndedIterator, FromIterator, Iterator}; |
| 19 | +use libc::{free, c_void}; |
20 | 20 | use mem::{size_of, move_val_init};
|
21 |
| -use cast::{forget, transmute}; |
| 21 | +use num::CheckedMul; |
| 22 | +use ops::Drop; |
| 23 | +use option::{None, Option, Some}; |
| 24 | +use ptr::RawPtr; |
| 25 | +use ptr; |
22 | 26 | use rt::global_heap::{malloc_raw, realloc_raw};
|
23 |
| -use vec::{ImmutableVector, Items, MutableVector}; |
24 | 27 | use unstable::raw::Slice;
|
25 |
| -use ptr; |
26 |
| -use ptr::RawPtr; |
27 |
| -use libc::{free, c_void}; |
| 28 | +use vec::{ImmutableVector, Items, MutItems, MutableVector, RevItems}; |
28 | 29 |
|
29 | 30 | pub struct Vec<T> {
|
30 | 31 | priv len: uint,
|
@@ -71,6 +72,55 @@ impl<T: Clone> Vec<T> {
|
71 | 72 | xs
|
72 | 73 | }
|
73 | 74 | }
|
| 75 | + |
| 76 | + #[inline] |
| 77 | + pub fn push_all(&mut self, other: &[T]) { |
| 78 | + for element in other.iter() { |
| 79 | + self.push((*element).clone()) |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl<T:Clone> Clone for Vec<T> { |
| 85 | + fn clone(&self) -> Vec<T> { |
| 86 | + let mut vector = Vec::with_capacity(self.len()); |
| 87 | + for element in self.iter() { |
| 88 | + vector.push((*element).clone()) |
| 89 | + } |
| 90 | + vector |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl<T> FromIterator<T> for Vec<T> { |
| 95 | + fn from_iterator<I:Iterator<T>>(iterator: &mut I) -> Vec<T> { |
| 96 | + let (lower, _) = iterator.size_hint(); |
| 97 | + let mut vector = Vec::with_capacity(lower); |
| 98 | + for element in *iterator { |
| 99 | + vector.push(element) |
| 100 | + } |
| 101 | + vector |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl<T:Eq> Eq for Vec<T> { |
| 106 | + #[inline] |
| 107 | + fn eq(&self, other: &Vec<T>) -> bool { |
| 108 | + self.as_slice() == other.as_slice() |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl<T:TotalEq> TotalEq for Vec<T> { |
| 113 | + #[inline] |
| 114 | + fn equals(&self, other: &Vec<T>) -> bool { |
| 115 | + self.as_slice().equals(&other.as_slice()) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl<T:TotalOrd> TotalOrd for Vec<T> { |
| 120 | + #[inline] |
| 121 | + fn cmp(&self, other: &Vec<T>) -> Ordering { |
| 122 | + self.as_slice().cmp(&other.as_slice()) |
| 123 | + } |
74 | 124 | }
|
75 | 125 |
|
76 | 126 | impl<T> Container for Vec<T> {
|
@@ -180,8 +230,117 @@ impl<T> Vec<T> {
|
180 | 230 | pub unsafe fn set_len(&mut self, len: uint) {
|
181 | 231 | self.len = len;
|
182 | 232 | }
|
| 233 | + |
| 234 | + #[inline] |
| 235 | + pub fn get<'a>(&'a self, index: uint) -> &'a T { |
| 236 | + &self.as_slice()[index] |
| 237 | + } |
| 238 | + |
| 239 | + #[inline] |
| 240 | + pub fn get_mut<'a>(&'a mut self, index: uint) -> &'a mut T { |
| 241 | + &mut self.as_mut_slice()[index] |
| 242 | + } |
| 243 | + |
| 244 | + #[inline] |
| 245 | + pub fn iter<'a>(&'a self) -> Items<'a,T> { |
| 246 | + self.as_slice().iter() |
| 247 | + } |
| 248 | + |
| 249 | + #[inline] |
| 250 | + pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a,T> { |
| 251 | + self.as_mut_slice().mut_iter() |
| 252 | + } |
| 253 | + |
| 254 | + #[inline] |
| 255 | + pub fn sort_by(&mut self, compare: |&T, &T| -> Ordering) { |
| 256 | + self.as_mut_slice().sort_by(compare) |
| 257 | + } |
| 258 | + |
| 259 | + #[inline] |
| 260 | + pub fn slice<'a>(&'a self, start: uint, end: uint) -> &'a [T] { |
| 261 | + self.as_slice().slice(start, end) |
| 262 | + } |
| 263 | + |
| 264 | + #[inline] |
| 265 | + pub fn tail<'a>(&'a self) -> &'a [T] { |
| 266 | + self.as_slice().tail() |
| 267 | + } |
| 268 | + |
| 269 | + #[inline] |
| 270 | + pub fn last<'a>(&'a self) -> Option<&'a T> { |
| 271 | + self.as_slice().last() |
| 272 | + } |
| 273 | + |
| 274 | + #[inline] |
| 275 | + pub fn mut_last<'a>(&'a mut self) -> Option<&'a mut T> { |
| 276 | + self.as_mut_slice().mut_last() |
| 277 | + } |
| 278 | + |
| 279 | + #[inline] |
| 280 | + pub fn swap_remove(&mut self, index: uint) -> T { |
| 281 | + let length = self.len(); |
| 282 | + if index >= length { |
| 283 | + fail!("Vec::swap_remove - index {} >= length {}", index, length); |
| 284 | + } |
| 285 | + if index < length - 1 { |
| 286 | + self.as_mut_slice().swap(index, length - 1); |
| 287 | + } |
| 288 | + self.pop().unwrap() |
| 289 | + } |
| 290 | + |
| 291 | + #[inline] |
| 292 | + pub fn unshift(&mut self, element: T) { |
| 293 | + self.insert(0, element) |
| 294 | + } |
| 295 | + |
| 296 | + pub fn insert(&mut self, index: uint, element: T) { |
| 297 | + let len = self.len(); |
| 298 | + assert!(index <= len); |
| 299 | + // space for the new element |
| 300 | + self.reserve_exact(len + 1); |
| 301 | + |
| 302 | + unsafe { // infallible |
| 303 | + // The spot to put the new value |
| 304 | + { |
| 305 | + let slice = self.as_mut_slice(); |
| 306 | + let p = slice.as_mut_ptr().offset(index as int); |
| 307 | + // Shift everything over to make space. (Duplicating the |
| 308 | + // `index`th element into two consecutive places.) |
| 309 | + ptr::copy_memory(p.offset(1), &*p, len - index); |
| 310 | + // Write it in, overwriting the first copy of the `index`th |
| 311 | + // element. |
| 312 | + move_val_init(&mut *p, element); |
| 313 | + } |
| 314 | + self.set_len(len + 1); |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + #[inline] |
| 319 | + pub fn rev_iter<'a>(&'a self) -> RevItems<'a,T> { |
| 320 | + self.as_slice().rev_iter() |
| 321 | + } |
| 322 | + |
| 323 | + #[inline] |
| 324 | + pub fn map<U>(&self, f: |t: &T| -> U) -> Vec<U> { |
| 325 | + self.iter().map(f).collect() |
| 326 | + } |
| 327 | + |
| 328 | + pub fn push_all_move(&mut self, other: Vec<T>) { |
| 329 | + for element in other.move_iter() { |
| 330 | + self.push(element) |
| 331 | + } |
| 332 | + } |
| 333 | + |
| 334 | + pub fn slice_from<'a>(&'a self, start: uint) -> &'a [T] { |
| 335 | + self.as_slice().slice_from(start) |
| 336 | + } |
183 | 337 | }
|
184 | 338 |
|
| 339 | +#[inline] |
| 340 | +pub fn append<T:Clone>(mut first: Vec<T>, second: &[T]) -> Vec<T> { |
| 341 | + first.push_all(second); |
| 342 | + first |
| 343 | +} |
185 | 344 |
|
186 | 345 | #[unsafe_destructor]
|
187 | 346 | impl<T> Drop for Vec<T> {
|
@@ -233,3 +392,4 @@ impl<T> Drop for MoveItems<T> {
|
233 | 392 | }
|
234 | 393 | }
|
235 | 394 | }
|
| 395 | + |
0 commit comments