14
14
*/
15
15
#[ forbid( deprecated_mode) ] ;
16
16
17
- use map;
18
17
use map:: StdMap ;
19
18
19
+ use core:: container:: { Container , Mutable , Map , Set } ;
20
20
use core:: dvec:: DVec ;
21
21
use core:: ops;
22
22
use core:: option:: { Some , None } ;
@@ -80,9 +80,9 @@ pub pure fn contains_key<T: Copy>(self: SmallIntMap<T>, key: uint) -> bool {
80
80
return !find ( self , key) . is_none ( ) ;
81
81
}
82
82
83
- /// Implements the map::map interface for smallintmap
84
- impl < V : Copy > SmallIntMap < V > : map :: StdMap < uint , V > {
85
- pure fn size ( ) -> uint {
83
+ impl < V > SmallIntMap < V > : Container {
84
+ /// Return the number of elements in the map
85
+ pure fn len ( & self ) -> uint {
86
86
let mut sz = 0 u;
87
87
for self . v. each |item| {
88
88
match * item {
@@ -92,6 +92,14 @@ impl<V: Copy> SmallIntMap<V>: map::StdMap<uint, V> {
92
92
}
93
93
sz
94
94
}
95
+
96
+ /// Return true if the map contains no elements
97
+ pure fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
98
+ }
99
+
100
+ /// Implements the map::map interface for smallintmap
101
+ impl < V : Copy > SmallIntMap < V > : StdMap < uint , V > {
102
+ pure fn size ( ) -> uint { self . len ( ) }
95
103
#[ inline( always) ]
96
104
fn insert ( key : uint , value : V ) -> bool {
97
105
let exists = contains_key ( self , key) ;
@@ -165,8 +173,8 @@ impl<V: Copy> SmallIntMap<V>: ops::Index<uint, V> {
165
173
}
166
174
167
175
/// Cast the given smallintmap to a map::map
168
- pub fn as_map < V : Copy > ( s : SmallIntMap < V > ) -> map :: StdMap < uint , V > {
169
- s as map :: StdMap :: < uint , V >
176
+ pub fn as_map < V : Copy > ( s : SmallIntMap < V > ) -> StdMap < uint , V > {
177
+ s as StdMap :: < uint , V >
170
178
}
171
179
172
180
#[ cfg( test) ]
@@ -176,6 +184,22 @@ mod tests {
176
184
use core:: option:: None ;
177
185
use core:: option;
178
186
187
+ #[ test]
188
+ fn test_len ( ) {
189
+ let mut map = mk ( ) ;
190
+ assert map. len ( ) == 0 ;
191
+ assert map. is_empty ( ) ;
192
+ map. insert ( 5 , 20 ) ;
193
+ assert map. len ( ) == 1 ;
194
+ assert !map. is_empty ( ) ;
195
+ map. insert ( 11 , 12 ) ;
196
+ assert map. len ( ) == 2 ;
197
+ assert !map. is_empty ( ) ;
198
+ map. insert ( 14 , 22 ) ;
199
+ assert map. len ( ) == 3 ;
200
+ assert !map. is_empty ( ) ;
201
+ }
202
+
179
203
#[ test]
180
204
fn test_insert_with_key ( ) {
181
205
let map: SmallIntMap < uint > = mk ( ) ;
0 commit comments