Skip to content

Commit 205e930

Browse files
pcwaltonpnkfelix
authored andcommitted
---
yaml --- r: 149807 b: refs/heads/try2 c: f690cc2 h: refs/heads/master i: 149805: ef03113 149803: 44bb0ba 149799: 1c80022 149791: c42aa5c v: v3
1 parent c3ebd3b commit 205e930

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 96e8c00e95b1980c429c5cfa4aae33e3cc60f3c5
8+
refs/heads/try2: f690cc2c63d436434412cef56154627d94b6284e
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/vec_ng.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use fmt;
2020
use iter::{DoubleEndedIterator, FromIterator, Extendable, Iterator};
2121
use libc::{free, c_void};
2222
use mem::{size_of, move_val_init};
23+
use mem;
2324
use num;
2425
use num::{CheckedMul, CheckedAdd};
2526
use ops::Drop;
@@ -66,6 +67,14 @@ impl<T> Vec<T> {
6667
}
6768

6869
impl<T: Clone> Vec<T> {
70+
pub fn from_slice(values: &[T]) -> Vec<T> {
71+
let mut vector = Vec::new();
72+
for value in values.iter() {
73+
vector.push((*value).clone())
74+
}
75+
vector
76+
}
77+
6978
pub fn from_elem(length: uint, value: T) -> Vec<T> {
7079
unsafe {
7180
let mut xs = Vec::with_capacity(length);
@@ -282,6 +291,12 @@ impl<T> Vec<T> {
282291
}
283292
}
284293

294+
#[inline]
295+
pub fn move_rev_iter(mut self) -> MoveItems<T> {
296+
self.reverse();
297+
self.move_iter()
298+
}
299+
285300
#[inline]
286301
pub unsafe fn set_len(&mut self, len: uint) {
287302
self.len = len;
@@ -322,6 +337,11 @@ impl<T> Vec<T> {
322337
self.as_slice().tail()
323338
}
324339

340+
#[inline]
341+
pub fn tailn<'a>(&'a self, n: uint) -> &'a [T] {
342+
self.as_slice().tailn(n)
343+
}
344+
325345
#[inline]
326346
pub fn last<'a>(&'a self) -> Option<&'a T> {
327347
self.as_slice().last()
@@ -387,21 +407,132 @@ impl<T> Vec<T> {
387407
}
388408
}
389409

410+
#[inline]
411+
pub fn mut_slice<'a>(&'a mut self, start: uint, end: uint)
412+
-> &'a mut [T] {
413+
self.as_mut_slice().mut_slice(start, end)
414+
}
415+
416+
#[inline]
417+
pub fn reverse(&mut self) {
418+
self.as_mut_slice().reverse()
419+
}
420+
421+
#[inline]
390422
pub fn slice_from<'a>(&'a self, start: uint) -> &'a [T] {
391423
self.as_slice().slice_from(start)
392424
}
393425

426+
#[inline]
427+
pub fn slice_to<'a>(&'a self, end: uint) -> &'a [T] {
428+
self.as_slice().slice_to(end)
429+
}
430+
394431
#[inline]
395432
pub fn init<'a>(&'a self) -> &'a [T] {
396433
self.slice(0, self.len() - 1)
397434
}
435+
436+
#[inline]
437+
pub fn as_ptr(&self) -> *T {
438+
self.as_slice().as_ptr()
439+
}
440+
441+
#[inline]
442+
pub fn clear(&mut self) {
443+
self.truncate(0)
444+
}
398445
}
399446

400447
impl<T:Eq> Vec<T> {
401448
/// Return true if a vector contains an element with the given value
402449
pub fn contains(&self, x: &T) -> bool {
403450
self.as_slice().contains(x)
404451
}
452+
453+
pub fn dedup(&mut self) {
454+
unsafe {
455+
// Although we have a mutable reference to `self`, we cannot make
456+
// *arbitrary* changes. The `Eq` comparisons could fail, so we
457+
// must ensure that the vector is in a valid state at all time.
458+
//
459+
// The way that we handle this is by using swaps; we iterate
460+
// over all the elements, swapping as we go so that at the end
461+
// the elements we wish to keep are in the front, and those we
462+
// wish to reject are at the back. We can then truncate the
463+
// vector. This operation is still O(n).
464+
//
465+
// Example: We start in this state, where `r` represents "next
466+
// read" and `w` represents "next_write`.
467+
//
468+
// r
469+
// +---+---+---+---+---+---+
470+
// | 0 | 1 | 1 | 2 | 3 | 3 |
471+
// +---+---+---+---+---+---+
472+
// w
473+
//
474+
// Comparing self[r] against self[w-1], tis is not a duplicate, so
475+
// we swap self[r] and self[w] (no effect as r==w) and then increment both
476+
// r and w, leaving us with:
477+
//
478+
// r
479+
// +---+---+---+---+---+---+
480+
// | 0 | 1 | 1 | 2 | 3 | 3 |
481+
// +---+---+---+---+---+---+
482+
// w
483+
//
484+
// Comparing self[r] against self[w-1], this value is a duplicate,
485+
// so we increment `r` but leave everything else unchanged:
486+
//
487+
// r
488+
// +---+---+---+---+---+---+
489+
// | 0 | 1 | 1 | 2 | 3 | 3 |
490+
// +---+---+---+---+---+---+
491+
// w
492+
//
493+
// Comparing self[r] against self[w-1], this is not a duplicate,
494+
// so swap self[r] and self[w] and advance r and w:
495+
//
496+
// r
497+
// +---+---+---+---+---+---+
498+
// | 0 | 1 | 2 | 1 | 3 | 3 |
499+
// +---+---+---+---+---+---+
500+
// w
501+
//
502+
// Not a duplicate, repeat:
503+
//
504+
// r
505+
// +---+---+---+---+---+---+
506+
// | 0 | 1 | 2 | 3 | 1 | 3 |
507+
// +---+---+---+---+---+---+
508+
// w
509+
//
510+
// Duplicate, advance r. End of vec. Truncate to w.
511+
512+
let ln = self.len();
513+
if ln < 1 { return; }
514+
515+
// Avoid bounds checks by using unsafe pointers.
516+
let p = self.as_mut_slice().as_mut_ptr();
517+
let mut r = 1;
518+
let mut w = 1;
519+
520+
while r < ln {
521+
let p_r = p.offset(r as int);
522+
let p_wm1 = p.offset((w - 1) as int);
523+
if *p_r != *p_wm1 {
524+
if r != w {
525+
let p_w = p_wm1.offset(1);
526+
mem::swap(&mut *p_r, &mut *p_w);
527+
}
528+
w += 1;
529+
}
530+
r += 1;
531+
}
532+
533+
self.truncate(w);
534+
}
535+
}
405536
}
406537

407538
#[inline]

0 commit comments

Comments
 (0)