@@ -23,8 +23,6 @@ use core::mem::replace;
23
23
24
24
use { vec, slice} ;
25
25
use vec:: Vec ;
26
- use hash;
27
- use hash:: Hash ;
28
26
29
27
// FIXME(conventions): capacity management???
30
28
@@ -62,8 +60,8 @@ use hash::Hash;
62
60
/// assert!(months.is_empty());
63
61
/// ```
64
62
#[ deriving( PartialEq , Eq ) ]
65
- pub struct VecMap < T > {
66
- v : Vec < Option < T > > ,
63
+ pub struct VecMap < V > {
64
+ v : Vec < Option < V > > ,
67
65
}
68
66
69
67
impl < V > Default for VecMap < V > {
@@ -83,12 +81,6 @@ impl<V:Clone> Clone for VecMap<V> {
83
81
}
84
82
}
85
83
86
- impl < S : hash:: Writer , T : Hash < S > > Hash < S > for VecMap < T > {
87
- fn hash ( & self , state : & mut S ) {
88
- self . v . hash ( state)
89
- }
90
- }
91
-
92
84
impl < V > VecMap < V > {
93
85
/// Creates an empty `VecMap`.
94
86
///
@@ -99,7 +91,7 @@ impl<V> VecMap<V> {
99
91
/// let mut map: VecMap<&str> = VecMap::new();
100
92
/// ```
101
93
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
102
- pub fn new ( ) -> VecMap < V > { VecMap { v : vec ! ( ) } }
94
+ pub fn new ( ) -> VecMap < V > { VecMap { v : vec ! [ ] } }
103
95
104
96
/// Creates an empty `VecMap` with space for at least `capacity`
105
97
/// elements before resizing.
@@ -207,10 +199,7 @@ impl<V> VecMap<V> {
207
199
/// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
208
200
/// ```
209
201
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
210
- pub fn into_iter ( & mut self )
211
- -> FilterMap < ( uint , Option < V > ) , ( uint , V ) ,
212
- Enumerate < vec:: MoveItems < Option < V > > > >
213
- {
202
+ pub fn into_iter ( & mut self ) -> MoveItems < V > {
214
203
let values = replace ( & mut self . v , vec ! ( ) ) ;
215
204
values. into_iter ( ) . enumerate ( ) . filter_map ( |( i, v) | {
216
205
v. map ( |v| ( i, v) )
@@ -523,16 +512,19 @@ impl<V> IndexMut<uint, V> for VecMap<V> {
523
512
524
513
macro_rules! iterator {
525
514
( impl $name: ident -> $elem: ty, $( $getter: ident) ,+) => {
526
- impl <' a, T > Iterator <$elem> for $name<' a, T > {
515
+ impl <' a, V > Iterator <$elem> for $name<' a, V > {
527
516
#[ inline]
528
517
fn next( & mut self ) -> Option <$elem> {
529
518
while self . front < self . back {
530
519
match self . iter. next( ) {
531
520
Some ( elem) => {
532
- if elem. is_some( ) {
533
- let index = self . front;
534
- self . front += 1 ;
535
- return Some ( ( index, elem $( . $getter ( ) ) +) ) ;
521
+ match elem$( . $getter ( ) ) + {
522
+ Some ( x) => {
523
+ let index = self . front;
524
+ self . front += 1 ;
525
+ return Some ( ( index, x) ) ;
526
+ } ,
527
+ None => { } ,
536
528
}
537
529
}
538
530
_ => ( )
@@ -552,15 +544,18 @@ macro_rules! iterator {
552
544
553
545
macro_rules! double_ended_iterator {
554
546
( impl $name: ident -> $elem: ty, $( $getter: ident) ,+) => {
555
- impl <' a, T > DoubleEndedIterator <$elem> for $name<' a, T > {
547
+ impl <' a, V > DoubleEndedIterator <$elem> for $name<' a, V > {
556
548
#[ inline]
557
549
fn next_back( & mut self ) -> Option <$elem> {
558
550
while self . front < self . back {
559
551
match self . iter. next_back( ) {
560
552
Some ( elem) => {
561
- if elem. is_some( ) {
562
- self . back -= 1 ;
563
- return Some ( ( self . back, elem$( . $getter ( ) ) +) ) ;
553
+ match elem$( . $getter ( ) ) + {
554
+ Some ( x) => {
555
+ self . back -= 1 ;
556
+ return Some ( ( self . back, x) ) ;
557
+ } ,
558
+ None => { } ,
564
559
}
565
560
}
566
561
_ => ( )
@@ -574,39 +569,42 @@ macro_rules! double_ended_iterator {
574
569
}
575
570
576
571
/// Forward iterator over a map.
577
- pub struct Entries < ' a , T : ' a > {
572
+ pub struct Entries < ' a , V : ' a > {
578
573
front : uint ,
579
574
back : uint ,
580
- iter : slice:: Items < ' a , Option < T > >
575
+ iter : slice:: Items < ' a , Option < V > >
581
576
}
582
577
583
- iterator ! ( impl Entries -> ( uint, & ' a T ) , as_ref, unwrap )
584
- double_ended_iterator ! ( impl Entries -> ( uint, & ' a T ) , as_ref, unwrap )
578
+ iterator ! ( impl Entries -> ( uint, & ' a V ) , as_ref)
579
+ double_ended_iterator ! ( impl Entries -> ( uint, & ' a V ) , as_ref)
585
580
586
581
/// Forward iterator over the key-value pairs of a map, with the
587
582
/// values being mutable.
588
- pub struct MutEntries < ' a , T : ' a > {
583
+ pub struct MutEntries < ' a , V : ' a > {
589
584
front : uint ,
590
585
back : uint ,
591
- iter : slice:: MutItems < ' a , Option < T > >
586
+ iter : slice:: MutItems < ' a , Option < V > >
592
587
}
593
588
594
- iterator ! ( impl MutEntries -> ( uint, & ' a mut T ) , as_mut, unwrap )
595
- double_ended_iterator ! ( impl MutEntries -> ( uint, & ' a mut T ) , as_mut, unwrap )
589
+ iterator ! ( impl MutEntries -> ( uint, & ' a mut V ) , as_mut)
590
+ double_ended_iterator ! ( impl MutEntries -> ( uint, & ' a mut V ) , as_mut)
596
591
597
592
/// Forward iterator over the keys of a map
598
- pub type Keys < ' a , T > =
599
- iter:: Map < ' static , ( uint , & ' a T ) , uint , Entries < ' a , T > > ;
593
+ pub type Keys < ' a , V > =
594
+ iter:: Map < ' static , ( uint , & ' a V ) , uint , Entries < ' a , V > > ;
600
595
601
596
/// Forward iterator over the values of a map
602
- pub type Values < ' a , T > =
603
- iter:: Map < ' static , ( uint , & ' a T ) , & ' a T , Entries < ' a , T > > ;
597
+ pub type Values < ' a , V > =
598
+ iter:: Map < ' static , ( uint , & ' a V ) , & ' a V , Entries < ' a , V > > ;
599
+
600
+ /// Iterator over the key-value pairs of a map, the iterator consumes the map
601
+ pub type MoveItems < V > =
602
+ FilterMap < ' static , ( uint , Option < V > ) , ( uint , V ) , Enumerate < vec:: MoveItems < Option < V > > > > ;
604
603
605
604
#[ cfg( test) ]
606
605
mod test_map {
607
606
use std:: prelude:: * ;
608
607
use vec:: Vec ;
609
- use hash;
610
608
611
609
use super :: VecMap ;
612
610
@@ -920,23 +918,6 @@ mod test_map {
920
918
assert ! ( a < b && a <= b) ;
921
919
}
922
920
923
- #[ test]
924
- fn test_hash ( ) {
925
- let mut x = VecMap :: new ( ) ;
926
- let mut y = VecMap :: new ( ) ;
927
-
928
- assert ! ( hash:: hash( & x) == hash:: hash( & y) ) ;
929
- x. insert ( 1 , 'a' ) ;
930
- x. insert ( 2 , 'b' ) ;
931
- x. insert ( 3 , 'c' ) ;
932
-
933
- y. insert ( 3 , 'c' ) ;
934
- y. insert ( 2 , 'b' ) ;
935
- y. insert ( 1 , 'a' ) ;
936
-
937
- assert ! ( hash:: hash( & x) == hash:: hash( & y) ) ;
938
- }
939
-
940
921
#[ test]
941
922
fn test_from_iter ( ) {
942
923
let xs: Vec < ( uint , char ) > = vec ! [ ( 1 u, 'a' ) , ( 2 , 'b' ) , ( 3 , 'c' ) , ( 4 , 'd' ) , ( 5 , 'e' ) ] ;
0 commit comments