Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to_str for hashmap. #2899

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/libstd/map.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -92,20 +94,21 @@ mod chained {
absent
}

type t<K, V> = @{
type inner<K, V> = {
mut count: uint,
mut chains: ~[mut chain<K,V>],
hasher: hashfn<K>,
eqer: eqfn<K>
};
type t<K, V> = @inner<K, V>;

enum search_result<K, V> {
not_found,
found_first(uint, @entry<K,V>),
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 inner<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;
Expand Down Expand Up @@ -276,6 +279,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 inner<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));
}
Expand Down