hashmap

How to implement Eq and Hash for my own structs to use them as a HashMap key?

一世执手 提交于 2020-08-19 05:54:11
问题 I have two structs, A and B , and I want to use a HashMap<A, B> . I have a piece of code like this: use std::collections::HashMap; pub struct A { x: i32, y: i32, title: String, } pub struct B { a: u32, b: u32, } fn main() { let map = HashMap::new(); map.insert( A { x: 10, y: 20, title: "test".to_string(), }, B { a: 1, b: 2 }, ); } But the compiler gives me these errors: error[E0277]: the trait bound `A: std::cmp::Eq` is not satisfied --> src/main.rs:16:9 | 16 | map.insert( | ^^^^^^ the trait

Why is the initial capacity in HashMap 16 (power of two) and the initial capacity of Hashtable 11(prime number)?

核能气质少年 提交于 2020-08-19 04:20:10
问题 Please describe the reason if you know. I Googled it, but didn't find well explained answers. Is it for making index of bucket positive when your hashCode is negative? 回答1: For HashMap , the index in the array that stores the entries of the Map is calculated this way (where h is calculated from the hashCode of the key): static int indexFor(int h, int length) { return h & (length-1); } Where length is the length of the array. This only works when length is a power of 2. If length wasn't power

Do you need to synchronized reading from HashMap?

一曲冷凌霜 提交于 2020-08-10 08:44:11
问题 I have a java.util.HashMap object. I guarantee that writing to HashMap is done by single dedicated thread. However, reading from the same HashMap object can be done from more that one thread at the time. Can I run in any troubles with such implementation? 回答1: Yes, you can run into big troubles with such an implementation! Adding a value to the HashMap is not an atomic operation. So if you read the map from another thread you might see an inconsistent state when another thread is adding a

Do you need to synchronized reading from HashMap?

点点圈 提交于 2020-08-10 08:37:08
问题 I have a java.util.HashMap object. I guarantee that writing to HashMap is done by single dedicated thread. However, reading from the same HashMap object can be done from more that one thread at the time. Can I run in any troubles with such implementation? 回答1: Yes, you can run into big troubles with such an implementation! Adding a value to the HashMap is not an atomic operation. So if you read the map from another thread you might see an inconsistent state when another thread is adding a

Are there any HashMap implementations with consistent ordering between program runs?

生来就可爱ヽ(ⅴ<●) 提交于 2020-08-07 04:58:21
问题 I've observed that HashMap has a different order of elements even with the same data on the next program start. It looks like HashMap uses some absolute addresses to sort elements. Is there any other HashMap implementation, which has the same behaviour if the same data was inserted? 回答1: I believe linked-hash-map is the de facto crate for this. 回答2: I've observed that HashMap has a different order of elements even with the same data on the next program start. You don't have to observe

Are there any HashMap implementations with consistent ordering between program runs?

孤人 提交于 2020-08-07 04:56:54
问题 I've observed that HashMap has a different order of elements even with the same data on the next program start. It looks like HashMap uses some absolute addresses to sort elements. Is there any other HashMap implementation, which has the same behaviour if the same data was inserted? 回答1: I believe linked-hash-map is the de facto crate for this. 回答2: I've observed that HashMap has a different order of elements even with the same data on the next program start. You don't have to observe

Are there any HashMap implementations with consistent ordering between program runs?

匆匆过客 提交于 2020-08-07 04:56:14
问题 I've observed that HashMap has a different order of elements even with the same data on the next program start. It looks like HashMap uses some absolute addresses to sort elements. Is there any other HashMap implementation, which has the same behaviour if the same data was inserted? 回答1: I believe linked-hash-map is the de facto crate for this. 回答2: I've observed that HashMap has a different order of elements even with the same data on the next program start. You don't have to observe

can we have a nested map as key within other map?

时光怂恿深爱的人放手 提交于 2020-08-05 08:02:46
问题 I have just started implementing data structures in Java and was wondering can we have a situation like this. Map<HashMap<String,String>,String> map = new HashMap<HashMap<String,String>,String>(); And if yes ,, please give a small example. If you don't found question relevant ,, please mention in comments, 回答1: You can do this, but you should not do so in most cases. The key to a map needs to be constant and its equals and hashcode need to be set up to give the correct behavior. If you modify

How do I build a Cacher in Rust without relying on the Copy trait?

假装没事ソ 提交于 2020-08-04 16:48:29
问题 I am trying to implement a Cacher as mentioned in Chapter 13 of the Rust book and running into trouble. My Cacher code looks like: use std::collections::HashMap; use std::hash::Hash; pub struct Cacher<T, K, V> where T: Fn(K) -> V, { calculation: T, values: HashMap<K, V>, } impl<T, K: Eq + Hash, V> Cacher<T, K, V> where T: Fn(K) -> V, { pub fn new(calculation: T) -> Cacher<T, K, V> { Cacher { calculation, values: HashMap::new(), } } pub fn value(&mut self, k: K) -> &V { let result = self

How do I build a Cacher in Rust without relying on the Copy trait?

偶尔善良 提交于 2020-08-04 16:48:07
问题 I am trying to implement a Cacher as mentioned in Chapter 13 of the Rust book and running into trouble. My Cacher code looks like: use std::collections::HashMap; use std::hash::Hash; pub struct Cacher<T, K, V> where T: Fn(K) -> V, { calculation: T, values: HashMap<K, V>, } impl<T, K: Eq + Hash, V> Cacher<T, K, V> where T: Fn(K) -> V, { pub fn new(calculation: T) -> Cacher<T, K, V> { Cacher { calculation, values: HashMap::new(), } } pub fn value(&mut self, k: K) -> &V { let result = self