@@ -60,6 +60,9 @@ iface map<K, V: copy> {
60
60
*/
61
61
fn remove ( K ) -> option < V > ;
62
62
63
+ /// Clear the map, removing all key/value pairs.
64
+ fn clear ( ) ;
65
+
63
66
/// Iterate over all the key/value pairs in the map
64
67
fn each ( fn ( K , V ) -> bool ) ;
65
68
@@ -75,6 +78,8 @@ iface map<K, V: copy> {
75
78
mod chained {
76
79
export t, mk, hashmap;
77
80
81
+ const initial_capacity: uint = 32 u; // 2^5
82
+
78
83
type entry < K , V > = {
79
84
hash : uint ,
80
85
key : K ,
@@ -255,6 +260,11 @@ mod chained {
255
260
}
256
261
}
257
262
263
+ fn clear ( ) {
264
+ self . count = 0 u;
265
+ self . chains = chains ( initial_capacity) ;
266
+ }
267
+
258
268
fn each ( blk : fn ( K , V ) -> bool ) {
259
269
for self . each_entry |entry| {
260
270
if !blk ( entry. key , copy entry. value ) { break ; }
@@ -271,7 +281,6 @@ mod chained {
271
281
}
272
282
273
283
fn mk < K , V : copy > ( hasher : hashfn < K > , eqer : eqfn < K > ) -> t < K , V > {
274
- let initial_capacity: uint = 32 u; // 2^5
275
284
let slf: t < K , V > = @{ mut count: 0 u,
276
285
mut chains: chains ( initial_capacity) ,
277
286
hasher: hasher,
@@ -609,6 +618,18 @@ mod tests {
609
618
assert ( option:: get ( map. find ( key) ) == "val" ) ;
610
619
}
611
620
621
+ #[ test]
622
+ fn test_clear ( ) {
623
+ let key = "k" ;
624
+ let map = map:: hashmap :: < str , str > ( str:: hash, str:: eq) ;
625
+ map. insert ( key, "val" ) ;
626
+ assert ( map. size ( ) == 1 ) ;
627
+ assert ( map. contains_key ( key) ) ;
628
+ map. clear ( ) ;
629
+ assert ( map. size ( ) == 0 ) ;
630
+ assert ( !map. contains_key ( key) ) ;
631
+ }
632
+
612
633
#[ test]
613
634
fn test_hash_from_vec ( ) {
614
635
let map = map:: hash_from_strs ( ~[
0 commit comments