Skip to content

Commit

Permalink
scupt-util, fix MTSet/MTMap hash compute
Browse files Browse the repository at this point in the history
  • Loading branch information
ybbh committed Jun 28, 2024
1 parent aefca16 commit fbe174e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/cmp_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::hash::{Hash, Hasher, SipHasher};
pub fn cmp_hash<K:Hash + Eq>(x:&K, y:&K) -> Ordering{
let mut ord = Ordering::Equal;
let mut n = 0u64;
while ord.is_eq() {
while !x.eq(y) && ord.is_eq() {
let mut h1 = SipHasher::new_with_keys(0, n);
let mut h2 = SipHasher::new_with_keys(0, n);
x.hash(&mut h1);
Expand Down
33 changes: 19 additions & 14 deletions src/test_mt_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod tests {
)]
struct K {
x:i32,
y:i32,
}

impl MsgTrait for K {
Expand All @@ -40,20 +41,24 @@ mod tests {
#[test]
fn test_mt_map_hash() {
let mut vec = vec![];
for i in 0..20 {
let k = K {
x:i
};
let v = K {
x:i + 1
};
vec.push((k, v));
for j in 0..10 {
for i in 0..20 {
let k = K {
x: i,
y:j
};
let v = K {
x: i + 1,
y:j + 1
};
vec.push((k, v));
}
let map1 = MTMap::from_vec(vec.clone());
let map2 = MTMap::from_vec(vec.clone());
assert_eq!(map1, map2);
let mut hash_set = HashSet::new();
hash_set.insert(map1);
assert!(hash_set.contains(&map2));
}
let map1 = MTMap::from_vec(vec.clone());
let map2 = MTMap::from_vec(vec.clone());
assert_eq!(map1, map2);
let mut hash_set = HashSet::new();
hash_set.insert(map1);
assert!(hash_set.contains(&map2));
}
}
27 changes: 16 additions & 11 deletions src/test_mt_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod tests {
)]
struct V {
x : i32,
y: i32,
}

impl MsgTrait for V {
Expand All @@ -38,17 +39,21 @@ mod tests {
#[test]
fn test_mt_set_hash() {
let mut vec = vec![];
for i in 0..20 {
let v = V {
x:i
};
vec.push(v);
for j in 0..10 {
for i in 0..20 {
let v = V {
x: i,
y: j
};
vec.push(v);
}
let set1 = MTSet::from_vec(vec.clone());
let set2 = MTSet::from_vec(vec.clone());
assert_eq!(set1, set2);
let mut hash_set = HashSet::new();
hash_set.insert(set1);
assert!(!hash_set.insert(set2.clone()));
assert!(hash_set.contains(&set2));
}
let set1 = MTSet::from_vec(vec.clone());
let set2 = MTSet::from_vec(vec.clone());
assert_eq!(set1, set2);
let mut hash_set = HashSet::new();
hash_set.insert(set1);
assert!(hash_set.contains(&set2));
}
}

0 comments on commit fbe174e

Please sign in to comment.