|
16 | 16 | //! # Example
|
17 | 17 | //!
|
18 | 18 | //! ```rust
|
19 |
| -//! use std::hashmap::HashMap; |
| 19 | +//! use collections::HashMap; |
20 | 20 | //!
|
21 | 21 | //! // type inference lets us omit an explicit type signature (which
|
22 | 22 | //! // would be `HashMap<&str, &str>` in this example).
|
|
52 | 52 | //! }
|
53 | 53 | //! ```
|
54 | 54 |
|
55 |
| -use container::{Container, Mutable, Map, MutableMap, Set, MutableSet}; |
56 |
| -use clone::Clone; |
57 |
| -use cmp::{Eq, Equiv, max}; |
58 |
| -use default::Default; |
59 |
| -use fmt; |
60 |
| -use hash_old::Hash; |
61 |
| -use iter; |
62 |
| -use iter::{Iterator, FromIterator, Extendable}; |
63 |
| -use iter::{FilterMap, Chain, Repeat, Zip}; |
64 |
| -use mem::replace; |
65 |
| -use num; |
66 |
| -use option::{None, Option, Some}; |
67 |
| -use rand::Rng; |
68 |
| -use rand; |
69 |
| -use result::{Ok, Err}; |
70 |
| -use vec::{ImmutableVector, MutableVector, OwnedVector, Items, MutItems}; |
71 |
| -use vec_ng; |
72 |
| -use vec_ng::Vec; |
| 55 | +use std::cmp::max; |
| 56 | +use std::fmt; |
| 57 | +use std::hash_old::Hash; |
| 58 | +use std::iter::{FilterMap, Chain, Repeat, Zip}; |
| 59 | +use std::iter; |
| 60 | +use std::mem::replace; |
| 61 | +use std::num; |
| 62 | +use std::rand::Rng; |
| 63 | +use std::rand; |
| 64 | +use std::vec::{Items, MutItems}; |
| 65 | +use std::vec_ng::Vec; |
| 66 | +use std::vec_ng; |
| 67 | + |
| 68 | +use serialize::{Encodable, Decodable, Encoder, Decoder}; |
73 | 69 |
|
74 | 70 | static INITIAL_CAPACITY: uint = 32u; // 2^5
|
75 | 71 |
|
@@ -404,7 +400,7 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
|
404 | 400 | /// # Example
|
405 | 401 | ///
|
406 | 402 | /// ```rust
|
407 |
| - /// use std::hashmap::HashMap; |
| 403 | + /// use collections::HashMap; |
408 | 404 | ///
|
409 | 405 | /// // map some strings to vectors of strings
|
410 | 406 | /// let mut map = HashMap::<~str, ~[~str]>::new();
|
@@ -613,6 +609,10 @@ impl<A: fmt::Show + Hash + Eq, B: fmt::Show> fmt::Show for HashMap<A, B> {
|
613 | 609 | }
|
614 | 610 | }
|
615 | 611 |
|
| 612 | +impl<K: fmt::Show + Hash + Eq, V: fmt::Show> ToStr for HashMap<K, V> { |
| 613 | + fn to_str(&self) -> ~str { format!("{}", *self) } |
| 614 | +} |
| 615 | + |
616 | 616 | /// HashMap iterator
|
617 | 617 | #[deriving(Clone)]
|
618 | 618 | pub struct Entries<'a, K, V> {
|
@@ -891,6 +891,10 @@ impl<A: fmt::Show + Hash + Eq> fmt::Show for HashSet<A> {
|
891 | 891 | }
|
892 | 892 | }
|
893 | 893 |
|
| 894 | +impl<A: fmt::Show + Hash + Eq> ToStr for HashSet<A> { |
| 895 | + fn to_str(&self) -> ~str { format!("{}", *self) } |
| 896 | +} |
| 897 | + |
894 | 898 | impl<K: Eq + Hash> FromIterator<K> for HashSet<K> {
|
895 | 899 | fn from_iterator<T: Iterator<K>>(iter: &mut T) -> HashSet<K> {
|
896 | 900 | let (lower, _) = iter.size_hint();
|
@@ -919,12 +923,75 @@ pub type SetAlgebraItems<'a, T> =
|
919 | 923 | FilterMap<'static,(&'a HashSet<T>, &'a T), &'a T,
|
920 | 924 | Zip<Repeat<&'a HashSet<T>>,SetItems<'a,T>>>;
|
921 | 925 |
|
| 926 | +impl< |
| 927 | + E: Encoder, |
| 928 | + K: Encodable<E> + Hash + IterBytes + Eq, |
| 929 | + V: Encodable<E> |
| 930 | +> Encodable<E> for HashMap<K, V> { |
| 931 | + fn encode(&self, e: &mut E) { |
| 932 | + e.emit_map(self.len(), |e| { |
| 933 | + let mut i = 0; |
| 934 | + for (key, val) in self.iter() { |
| 935 | + e.emit_map_elt_key(i, |e| key.encode(e)); |
| 936 | + e.emit_map_elt_val(i, |e| val.encode(e)); |
| 937 | + i += 1; |
| 938 | + } |
| 939 | + }) |
| 940 | + } |
| 941 | +} |
| 942 | + |
| 943 | +impl< |
| 944 | + D: Decoder, |
| 945 | + K: Decodable<D> + Hash + IterBytes + Eq, |
| 946 | + V: Decodable<D> |
| 947 | +> Decodable<D> for HashMap<K, V> { |
| 948 | + fn decode(d: &mut D) -> HashMap<K, V> { |
| 949 | + d.read_map(|d, len| { |
| 950 | + let mut map = HashMap::with_capacity(len); |
| 951 | + for i in range(0u, len) { |
| 952 | + let key = d.read_map_elt_key(i, |d| Decodable::decode(d)); |
| 953 | + let val = d.read_map_elt_val(i, |d| Decodable::decode(d)); |
| 954 | + map.insert(key, val); |
| 955 | + } |
| 956 | + map |
| 957 | + }) |
| 958 | + } |
| 959 | +} |
| 960 | + |
| 961 | +impl< |
| 962 | + S: Encoder, |
| 963 | + T: Encodable<S> + Hash + IterBytes + Eq |
| 964 | +> Encodable<S> for HashSet<T> { |
| 965 | + fn encode(&self, s: &mut S) { |
| 966 | + s.emit_seq(self.len(), |s| { |
| 967 | + let mut i = 0; |
| 968 | + for e in self.iter() { |
| 969 | + s.emit_seq_elt(i, |s| e.encode(s)); |
| 970 | + i += 1; |
| 971 | + } |
| 972 | + }) |
| 973 | + } |
| 974 | +} |
| 975 | + |
| 976 | +impl< |
| 977 | + D: Decoder, |
| 978 | + T: Decodable<D> + Hash + IterBytes + Eq |
| 979 | +> Decodable<D> for HashSet<T> { |
| 980 | + fn decode(d: &mut D) -> HashSet<T> { |
| 981 | + d.read_seq(|d, len| { |
| 982 | + let mut set = HashSet::with_capacity(len); |
| 983 | + for i in range(0u, len) { |
| 984 | + set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))); |
| 985 | + } |
| 986 | + set |
| 987 | + }) |
| 988 | + } |
| 989 | +} |
922 | 990 |
|
923 | 991 | #[cfg(test)]
|
924 | 992 | mod test_map {
|
925 |
| - use prelude::*; |
926 |
| - use super::*; |
927 |
| - use fmt; |
| 993 | + use super::{HashMap, HashSet}; |
| 994 | + use std::fmt; |
928 | 995 |
|
929 | 996 | #[test]
|
930 | 997 | fn test_create_capacity_zero() {
|
@@ -1180,14 +1247,49 @@ mod test_map {
|
1180 | 1247 | assert!(table_str == ~"{1: s2, 3: s4}" || table_str == ~"{3: s4, 1: s2}");
|
1181 | 1248 | assert_eq!(format!("{}", empty), ~"{}");
|
1182 | 1249 | }
|
| 1250 | +
|
| 1251 | + struct StructWithToStrWithoutEqOrHash { |
| 1252 | + value: int |
| 1253 | + } |
| 1254 | +
|
| 1255 | + impl fmt::Show for StructWithToStrWithoutEqOrHash { |
| 1256 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 1257 | + write!(f.buf, "s{}", self.value) |
| 1258 | + } |
| 1259 | + } |
| 1260 | +
|
| 1261 | + #[test] |
| 1262 | + fn test_hashset() { |
| 1263 | + let mut set: HashSet<int> = HashSet::new(); |
| 1264 | + let empty_set: HashSet<int> = HashSet::new(); |
| 1265 | +
|
| 1266 | + set.insert(1); |
| 1267 | + set.insert(2); |
| 1268 | +
|
| 1269 | + let set_str = set.to_str(); |
| 1270 | +
|
| 1271 | + assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}"); |
| 1272 | + assert_eq!(empty_set.to_str(), ~"{}"); |
| 1273 | + } |
| 1274 | +
|
| 1275 | + #[test] |
| 1276 | + fn test_hashmap() { |
| 1277 | + let mut table: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new(); |
| 1278 | + let empty: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new(); |
| 1279 | +
|
| 1280 | + table.insert(3, StructWithToStrWithoutEqOrHash { value: 4 }); |
| 1281 | + table.insert(1, StructWithToStrWithoutEqOrHash { value: 2 }); |
| 1282 | +
|
| 1283 | + let table_str = table.to_str(); |
| 1284 | +
|
| 1285 | + assert!(table_str == ~"{1: s2, 3: s4}" || table_str == ~"{3: s4, 1: s2}"); |
| 1286 | + assert_eq!(empty.to_str(), ~"{}"); |
| 1287 | + } |
1183 | 1288 | }
|
1184 | 1289 |
|
1185 | 1290 | #[cfg(test)]
|
1186 | 1291 | mod test_set {
|
1187 |
| - use super::*; |
1188 |
| - use prelude::*; |
1189 |
| - use container::Container; |
1190 |
| - use vec::ImmutableEqVector; |
| 1292 | + use super::HashSet; |
1191 | 1293 |
|
1192 | 1294 | #[test]
|
1193 | 1295 | fn test_disjoint() {
|
|
0 commit comments