Skip to content

Commit 7097283

Browse files
committed
auto merge of #16020 : nham/rust/ringbuf_hash_ord, r=alexcrichton
cc #15294
2 parents 769dae0 + 9fa4424 commit 7097283

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/libcollections/ringbuf.rs

+49
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use core::cmp;
1919
use core::default::Default;
2020
use core::fmt;
2121
use core::iter::RandomAccessIterator;
22+
use core::iter;
23+
use std::hash::{Writer, Hash};
2224

2325
use {Deque, Collection, Mutable, MutableSeq};
2426
use vec::Vec;
@@ -450,6 +452,21 @@ impl<A: PartialEq> PartialEq for RingBuf<A> {
450452
}
451453
}
452454

455+
impl<A: PartialOrd> PartialOrd for RingBuf<A> {
456+
fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering> {
457+
iter::order::partial_cmp(self.iter(), other.iter())
458+
}
459+
}
460+
461+
impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
462+
fn hash(&self, state: &mut S) {
463+
self.len().hash(state);
464+
for elt in self.iter() {
465+
elt.hash(state);
466+
}
467+
}
468+
}
469+
453470
impl<A> FromIterator<A> for RingBuf<A> {
454471
fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> {
455472
let (lower, _) = iterator.size_hint();
@@ -485,6 +502,7 @@ mod tests {
485502
use std::fmt::Show;
486503
use std::prelude::*;
487504
use std::gc::{GC, Gc};
505+
use std::hash;
488506
use test::Bencher;
489507
use test;
490508

@@ -912,6 +930,37 @@ mod tests {
912930
assert!(e == RingBuf::new());
913931
}
914932

933+
#[test]
934+
fn test_hash() {
935+
let mut x = RingBuf::new();
936+
let mut y = RingBuf::new();
937+
938+
x.push(1i);
939+
x.push(2);
940+
x.push(3);
941+
942+
y.push(0i);
943+
y.push(1i);
944+
y.pop_front();
945+
y.push(2);
946+
y.push(3);
947+
948+
assert!(hash::hash(&x) == hash::hash(&y));
949+
}
950+
951+
#[test]
952+
fn test_ord() {
953+
let x = RingBuf::new();
954+
let mut y = RingBuf::new();
955+
y.push(1i);
956+
y.push(2);
957+
y.push(3);
958+
assert!(x < y);
959+
assert!(y > x);
960+
assert!(x <= x);
961+
assert!(x >= x);
962+
}
963+
915964
#[test]
916965
fn test_show() {
917966
let ringbuf: RingBuf<int> = range(0i, 10).collect();

0 commit comments

Comments
 (0)