1
1
//! A map type
2
2
3
3
import chained:: hashmap;
4
+ import io:: writer_util;
5
+ import to_str:: to_str;
4
6
export hashmap, hashfn, eqfn, set, map, chained, hashmap, str_hash;
5
7
export box_str_hash;
6
8
export bytes_hash, int_hash, uint_hash, set_add;
@@ -98,6 +100,7 @@ mod chained {
98
100
hasher : hashfn < K > ,
99
101
eqer : eqfn < K >
100
102
} ;
103
+ type t < K , V > = @hashmap_ < K , V > ;
101
104
102
105
enum hashmap_ < K , V > {
103
106
hashmap_( @hashmap__ < K , V > )
@@ -111,7 +114,7 @@ mod chained {
111
114
found_after( @entry < K , V > , @entry < K , V > )
112
115
}
113
116
114
- impl private_methods < K , V : copy > for t < K , V > {
117
+ impl private_methods < K , V : copy > for hashmap_ < K , V > {
115
118
fn search_rem ( k : K , h : uint , idx : uint ,
116
119
e_root : @entry < K , V > ) -> search_result < K , V > {
117
120
let mut e0 = e_root;
@@ -285,6 +288,33 @@ mod chained {
285
288
fn each_value ( blk : fn ( V ) -> bool ) { self . each ( |_k, v| blk ( v) ) }
286
289
}
287
290
291
+ impl hashmap < K : to_str , V : to_str copy> of to_str for hashmap_ < K , V > {
292
+ fn to_writer ( wr : io:: writer ) {
293
+ if self . count == 0 u {
294
+ wr. write_str ( "{}" ) ;
295
+ ret;
296
+ }
297
+
298
+ wr. write_str ( "{ " ) ;
299
+ let mut first = true ;
300
+ for self . each_entry |entry| {
301
+ if !first {
302
+ wr. write_str ( ", " ) ;
303
+ }
304
+ first = false ;
305
+ wr. write_str ( entry. key . to_str ( ) ) ;
306
+ wr. write_str ( ": " ) ;
307
+ wr. write_str ( ( copy entry. value ) . to_str ( ) ) ;
308
+ } ;
309
+ wr. write_str ( " }" ) ;
310
+ }
311
+
312
+ fn to_str ( ) -> ~str {
313
+ do io:: with_str_writer |wr| { self . to_writer ( wr) }
314
+ }
315
+ }
316
+
317
+
288
318
fn chains < K , V > ( nchains : uint ) -> ~[ mut chain < K , V > ] {
289
319
ret vec:: to_mut ( vec:: from_elem ( nchains, absent) ) ;
290
320
}
0 commit comments