@@ -20,6 +20,7 @@ use fmt;
20
20
use iter:: { DoubleEndedIterator , FromIterator , Extendable , Iterator } ;
21
21
use libc:: { free, c_void} ;
22
22
use mem:: { size_of, move_val_init} ;
23
+ use mem;
23
24
use num;
24
25
use num:: { CheckedMul , CheckedAdd } ;
25
26
use ops:: Drop ;
@@ -66,6 +67,14 @@ impl<T> Vec<T> {
66
67
}
67
68
68
69
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
+
69
78
pub fn from_elem ( length : uint , value : T ) -> Vec < T > {
70
79
unsafe {
71
80
let mut xs = Vec :: with_capacity ( length) ;
@@ -282,6 +291,12 @@ impl<T> Vec<T> {
282
291
}
283
292
}
284
293
294
+ #[ inline]
295
+ pub fn move_rev_iter ( mut self ) -> MoveItems < T > {
296
+ self . reverse ( ) ;
297
+ self . move_iter ( )
298
+ }
299
+
285
300
#[ inline]
286
301
pub unsafe fn set_len ( & mut self , len : uint ) {
287
302
self . len = len;
@@ -322,6 +337,11 @@ impl<T> Vec<T> {
322
337
self . as_slice ( ) . tail ( )
323
338
}
324
339
340
+ #[ inline]
341
+ pub fn tailn < ' a > ( & ' a self , n : uint ) -> & ' a [ T ] {
342
+ self . as_slice ( ) . tailn ( n)
343
+ }
344
+
325
345
#[ inline]
326
346
pub fn last < ' a > ( & ' a self ) -> Option < & ' a T > {
327
347
self . as_slice ( ) . last ( )
@@ -387,21 +407,132 @@ impl<T> Vec<T> {
387
407
}
388
408
}
389
409
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]
390
422
pub fn slice_from < ' a > ( & ' a self , start : uint ) -> & ' a [ T ] {
391
423
self . as_slice ( ) . slice_from ( start)
392
424
}
393
425
426
+ #[ inline]
427
+ pub fn slice_to < ' a > ( & ' a self , end : uint ) -> & ' a [ T ] {
428
+ self . as_slice ( ) . slice_to ( end)
429
+ }
430
+
394
431
#[ inline]
395
432
pub fn init < ' a > ( & ' a self ) -> & ' a [ T ] {
396
433
self . slice ( 0 , self . len ( ) - 1 )
397
434
}
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
+ }
398
445
}
399
446
400
447
impl < T : Eq > Vec < T > {
401
448
/// Return true if a vector contains an element with the given value
402
449
pub fn contains ( & self , x : & T ) -> bool {
403
450
self . as_slice ( ) . contains ( x)
404
451
}
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
+ }
405
536
}
406
537
407
538
#[ inline]
0 commit comments