12
12
#[ doc( hidden) ] ;
13
13
14
14
use cast:: { forget, transmute} ;
15
+ use cast;
15
16
use clone:: Clone ;
16
- use cmp:: { Ord , Eq , Ordering , TotalEq , TotalOrd } ;
17
+ use cmp:: { Eq , Equiv , Ord , Ordering , TotalEq , TotalOrd } ;
17
18
use container:: { Container , Mutable } ;
18
19
use default:: Default ;
19
20
use fmt;
@@ -30,7 +31,7 @@ use ptr;
30
31
use rt:: global_heap:: { malloc_raw, realloc_raw} ;
31
32
use raw:: Slice ;
32
33
use vec:: { ImmutableEqVector , ImmutableVector , Items , MutItems , MutableVector } ;
33
- use vec:: { RevItems } ;
34
+ use vec:: { RevItems , Vector } ;
34
35
35
36
pub struct Vec < T > {
36
37
priv len: uint ,
@@ -184,6 +185,13 @@ impl<T: TotalOrd> TotalOrd for Vec<T> {
184
185
}
185
186
}
186
187
188
+ impl < ' a , T : Eq , V : Vector < T > > Equiv < V > for Vec < T > {
189
+ #[ inline]
190
+ fn equiv ( & self , other : & V ) -> bool {
191
+ self . as_slice ( ) == other. as_slice ( )
192
+ }
193
+ }
194
+
187
195
impl < T > Container for Vec < T > {
188
196
#[ inline]
189
197
fn len ( & self ) -> uint {
@@ -280,12 +288,6 @@ impl<T> Vec<T> {
280
288
self . len = len;
281
289
}
282
290
283
- #[ inline]
284
- pub fn as_slice < ' a > ( & ' a self ) -> & ' a [ T ] {
285
- let slice = Slice { data : self . ptr as * T , len : self . len } ;
286
- unsafe { transmute ( slice) }
287
- }
288
-
289
291
#[ inline]
290
292
pub fn as_mut_slice < ' a > ( & ' a mut self ) -> & ' a mut [ T ] {
291
293
let slice = Slice { data : self . ptr as * T , len : self . len } ;
@@ -455,6 +457,59 @@ impl<T> Mutable for Vec<T> {
455
457
fn clear ( & mut self ) {
456
458
self . truncate ( 0 )
457
459
}
460
+
461
+ #[ inline]
462
+ pub fn shift ( & mut self ) -> Option < T > {
463
+ self . remove ( 0 )
464
+ }
465
+
466
+ #[ inline]
467
+ pub fn partition ( self , f: |& T | -> bool) -> ( Vec < T > , Vec < T > ) {
468
+ let mut lefts = Vec :: new ( ) ;
469
+ let mut rights = Vec :: new ( ) ;
470
+
471
+ for elt in self . move_iter ( ) {
472
+ if f ( & elt) {
473
+ lefts. push ( elt) ;
474
+ } else {
475
+ rights. push ( elt) ;
476
+ }
477
+ }
478
+
479
+ ( lefts, rights)
480
+ }
481
+
482
+ pub fn remove ( & mut self , i : uint ) -> Option < T > {
483
+ let len = self . len ( ) ;
484
+ if i < len {
485
+ unsafe { // infallible
486
+ // the place we are taking from.
487
+ let ptr = self . as_mut_slice ( ) . as_mut_ptr ( ) . offset ( i as int ) ;
488
+ // copy it out, unsafely having a copy of the value on
489
+ // the stack and in the vector at the same time.
490
+ let ret = Some ( ptr:: read ( ptr as * T ) ) ;
491
+
492
+ // Shift everything down to fill in that spot.
493
+ ptr:: copy_memory ( ptr, & * ptr. offset ( 1 ) , len - i - 1 ) ;
494
+ self . set_len ( len - 1 ) ;
495
+
496
+ ret
497
+ }
498
+ } else {
499
+ None
500
+ }
501
+ }
502
+ }
503
+
504
+ impl < T > Vector < T > for Vec < T > {
505
+ /// Work with `self` as a slice.
506
+ #[ inline]
507
+ fn as_slice < ' a > ( & ' a self ) -> & ' a [ T ] {
508
+ let slice = Slice { data : self . ptr as * T , len : self . len } ;
509
+ unsafe {
510
+ cast:: transmute ( slice)
511
+ }
512
+ }
458
513
}
459
514
460
515
impl < T : Eq > Vec < T > {
0 commit comments