diff options
| author | Glenn Willen <gwillen@nerdnet.org> | 2012-07-13 03:01:26 -0400 |
|---|---|---|
| committer | Graydon Hoare <graydon@mozilla.com> | 2012-07-23 17:30:02 -0700 |
| commit | 28519c8ef64dc37ceab23d6a26a4ccea7f396a00 (patch) | |
| tree | b34ea659924d488d6f88b0a842106e8c6b0388b4 /src/libstd | |
| parent | 06ac0c2b1da2592c53462e1d3675bf1198175aed (diff) | |
Add to_str for hashmap.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/map.rs | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/src/libstd/map.rs b/src/libstd/map.rs index c285bc06629..afb5f28ae08 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; @@ -98,6 +100,7 @@ mod chained { hasher: hashfn<K>, eqer: eqfn<K> }; + type t<K, V> = @hashmap_<K, V>; enum hashmap_<K, V> { hashmap_(@hashmap__<K, V>) @@ -111,7 +114,7 @@ mod chained { found_after(@entry<K,V>, @entry<K,V>) } - impl private_methods<K, V: copy> for t<K, V> { + impl private_methods<K, V: copy> for hashmap_<K, V> { fn search_rem(k: K, h: uint, idx: uint, e_root: @entry<K,V>) -> search_result<K,V> { let mut e0 = e_root; @@ -285,6 +288,33 @@ mod chained { fn each_value(blk: fn(V) -> bool) { self.each(|_k, v| blk(v)) } } + impl hashmap<K: to_str, V: to_str copy> of to_str for hashmap_<K, V> { + 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<K,V>(nchains: uint) -> ~[mut chain<K,V>] { ret vec::to_mut(vec::from_elem(nchains, absent)); } |
