@@ -25,17 +25,20 @@ impl <'a, K: KeyType, V: ValueType> MultiMap<K,V> {
2525 }
2626
2727 pub fn insert ( & mut self , key : K , value : V ) -> usize {
28- self . count += 1 ;
2928
3029 if let Some ( set) = self . multi_map . get_mut ( & key) {
31- set. insert ( value) ;
30+ if set. insert ( value) {
31+ self . count +=1 ;
32+ }
3233 return self . count ;
3334 }
34-
35+
3536 let mut set = BTreeSet :: < V > :: new ( ) ;
3637
3738 set. insert ( value) ;
3839
40+ self . count += 1 ;
41+
3942 self . multi_map . insert ( key, set) ;
4043
4144 return self . count ;
@@ -66,7 +69,7 @@ impl <'a, K: KeyType, V: ValueType> MultiMap<K,V> {
6669 if let Occupied ( mut entry) = self . multi_map . entry ( key) {
6770
6871 if entry. get_mut ( ) . remove ( & value) {
69- self . count -= 1 ;
72+ self . count -= 1 ;
7073 }
7174
7275 if entry. get ( ) . is_empty ( ) {
@@ -153,11 +156,11 @@ mod tests {
153156 let e1 = it. next ( ) . unwrap ( ) ;
154157 assert ! ( 12 == e1. key) ;
155158 assert ! ( String :: from( "abc" ) == e1. value) ;
156-
159+
157160 let e2 = it. next ( ) . unwrap ( ) ;
158161 assert ! ( 23 == e2. key) ;
159162 assert ! ( String :: from( "abc" ) == e2. value) ;
160-
163+
161164 let e3 = it. next ( ) . unwrap ( ) ;
162165 assert ! ( 23 == e3. key) ;
163166 assert ! ( String :: from( "def" ) == e3. value) ;
@@ -166,7 +169,7 @@ mod tests {
166169 #[ test]
167170 fn test_get ( ) {
168171 let mut mmap = MultiMap :: < i32 , String > :: new ( ) ;
169-
172+
170173 assert ! ( mmap. insert( 12 , String :: from( "abc" ) ) == 1 ) ;
171174 assert ! ( mmap. insert( 23 , String :: from( "abc" ) ) == 2 ) ;
172175 assert ! ( mmap. insert( 23 , String :: from( "def" ) ) == 3 ) ;
@@ -204,6 +207,12 @@ mod tests {
204207
205208 assert ! ( it. next( ) == None ) ;
206209 }
207- }
208-
209-
210+ #[ test]
211+ fn test_size ( ) {
212+ let mut mmap = MultiMap :: < i32 , String > :: new ( ) ;
213+ mmap. insert ( 1 , "abc" . into ( ) ) ;
214+ mmap. insert ( 1 , "abc" . into ( ) ) ;
215+ mmap. delete ( 1 , "abc" . into ( ) ) ;
216+ assert_eq ! ( mmap. size( ) , 0 )
217+ }
218+ }
0 commit comments