Skip to content

Hash VecDeque in its slice parts #31224

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

Merged
merged 1 commit into from
Jan 27, 2016
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/libcollections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1994,9 +1994,9 @@ impl<A: Ord> Ord for VecDeque<A> {
impl<A: Hash> Hash for VecDeque<A> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.len().hash(state);
for elt in self {
elt.hash(state);
}
let (a, b) = self.as_slices();
Hash::hash_slice(a, state);
Hash::hash_slice(b, state);
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/libcollectionstest/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,25 @@ fn test_hash() {
assert!(::hash(&x) == ::hash(&y));
}

#[test]
fn test_hash_after_rotation() {
// test that two deques hash equal even if elements are laid out differently
let len = 28;
let mut ring: VecDeque<i32> = (0..len as i32).collect();
let orig = ring.clone();
for _ in 0..ring.capacity() {
// shift values 1 step to the right by pop, sub one, push
ring.pop_front();
for elt in &mut ring {
*elt -= 1;
}
ring.push_back(len - 1);
assert_eq!(::hash(&orig), ::hash(&ring));
assert_eq!(orig, ring);
assert_eq!(ring, orig);
}
}

#[test]
fn test_ord() {
let x = VecDeque::new();
Expand Down