diff --git a/src/libstd/map.rs b/src/libstd/map.rs index 8a2089c6f2b3a..2595325dc642c 100644 --- a/src/libstd/map.rs +++ b/src/libstd/map.rs @@ -1,6 +1,8 @@ //! A map type import chained::hashmap; +import io::writer_util; +import to_str::to_str; export hashmap, hashfn, eqfn, set, map, chained, hashmap, str_hash; export box_str_hash; export bytes_hash, int_hash, uint_hash, set_add; @@ -92,12 +94,13 @@ mod chained { absent } - type t = @{ + type inner = { mut count: uint, mut chains: ~[mut chain], hasher: hashfn, eqer: eqfn }; + type t = @inner; enum search_result { not_found, @@ -105,7 +108,7 @@ mod chained { found_after(@entry, @entry) } - impl private_methods for t { + impl private_methods for inner { fn search_rem(k: K, h: uint, idx: uint, e_root: @entry) -> search_result { let mut e0 = e_root; @@ -276,6 +279,33 @@ mod chained { fn each_value(blk: fn(V) -> bool) { self.each(|_k, v| blk(v)) } } + impl hashmap of to_str for inner { + fn to_writer(wr: io::writer) { + if self.count == 0u { + wr.write_str("{}"); + ret; + } + + wr.write_str("{ "); + let mut first = true; + for self.each_entry |entry| { + if !first { + wr.write_str(", "); + } + first = false; + wr.write_str(entry.key.to_str()); + wr.write_str(": "); + wr.write_str((copy entry.value).to_str()); + }; + wr.write_str(" }"); + } + + fn to_str() -> str { + do io::with_str_writer |wr| { self.to_writer(wr) } + } + } + + fn chains(nchains: uint) -> ~[mut chain] { ret vec::to_mut(vec::from_elem(nchains, absent)); }