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

Implement Hash and PartialOrd for RingBuf #16020

Merged
merged 3 commits into from
Jul 27, 2014
Merged
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
49 changes: 49 additions & 0 deletions src/libcollections/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use core::cmp;
use core::default::Default;
use core::fmt;
use core::iter::RandomAccessIterator;
use core::iter;
use std::hash::{Writer, Hash};

use {Deque, Collection, Mutable, MutableSeq};
use vec::Vec;
Expand Down Expand Up @@ -450,6 +452,21 @@ impl<A: PartialEq> PartialEq for RingBuf<A> {
}
}

impl<A: PartialOrd> PartialOrd for RingBuf<A> {
fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter())
}
}

impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
fn hash(&self, state: &mut S) {
self.len().hash(state);
for elt in self.iter() {
elt.hash(state);
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in general collections need to hash their length first because otherwise this would have the same hash:

  • (vec![1], vec![2, 3])
  • (vec![1, 2], vec![3])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I didn't' think of that. That means that some of the other Hash implementations I've done are wrong. TreeMap at least seems to be wrong. Though I'm wondering if that could be fixed at the tuple-level. (Tuples could hash the index before each value?)

I've fixed it for RingBuf here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I think it's ok, they can be amended in the future. Hashing to the same value isn't really the end of the world, so long as the comparisons are correct!


impl<A> FromIterator<A> for RingBuf<A> {
fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> {
let (lower, _) = iterator.size_hint();
Expand Down Expand Up @@ -485,6 +502,7 @@ mod tests {
use std::fmt::Show;
use std::prelude::*;
use std::gc::{GC, Gc};
use std::hash;
use test::Bencher;
use test;

Expand Down Expand Up @@ -912,6 +930,37 @@ mod tests {
assert!(e == RingBuf::new());
}

#[test]
fn test_hash() {
let mut x = RingBuf::new();
let mut y = RingBuf::new();

x.push(1i);
x.push(2);
x.push(3);

y.push(0i);
y.push(1i);
y.pop_front();
y.push(2);
y.push(3);

assert!(hash::hash(&x) == hash::hash(&y));
}

#[test]
fn test_ord() {
let x = RingBuf::new();
let mut y = RingBuf::new();
y.push(1i);
y.push(2);
y.push(3);
assert!(x < y);
assert!(y > x);
assert!(x <= x);
assert!(x >= x);
}

#[test]
fn test_show() {
let ringbuf: RingBuf<int> = range(0i, 10).collect();
Expand Down