Skip to content

Commit

Permalink
convert @record to more standard enum syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Jenson committed Dec 8, 2012
1 parent 27189e6 commit 6e1b582
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions pairing_heap.rs
Expand Up @@ -13,10 +13,10 @@ use std::list::{List, Cons, Nil};
*/ */
pub enum PairingHeap<E: Copy Eq Ord> { pub enum PairingHeap<E: Copy Eq Ord> {
Empty_, Empty_,
PairingHeap_(@{ PairingHeap_(
head: E, E,
rest: @List<PairingHeap<E>> @List<PairingHeap<E>>
}) )
} }


pub trait Heap<E: Copy Eq Ord> { pub trait Heap<E: Copy Eq Ord> {
Expand All @@ -39,18 +39,18 @@ pure fn Empty<E: Copy Eq Ord>() -> PairingHeap<E> {
} }


pure fn PairingHeap<E: Copy Eq Ord>(initial_value: E) -> PairingHeap<E> { pure fn PairingHeap<E: Copy Eq Ord>(initial_value: E) -> PairingHeap<E> {
PairingHeap_(@{ PairingHeap_(
head: initial_value, initial_value,
rest: @Nil @Nil
}) )
} }


impl<E: Copy Eq Ord> PairingHeap<E> : Eq { impl<E: Copy Eq Ord> PairingHeap<E> : Eq {
pure fn eq(other: &PairingHeap<E>) -> bool { pure fn eq(other: &PairingHeap<E>) -> bool {
match (self, *other) { match (self, *other) {
(Empty_, Empty_) => {true} (Empty_, Empty_) => {true}
(PairingHeap_(heapA), PairingHeap_(heapB)) => { (PairingHeap_(headA, restA), PairingHeap_(headB, restB)) => {
(heapA.head == heapB.head) && (heapA.rest == heapB.rest) (headA == headB) && (restA == restB)
} }
//(Empty_, PairingHeap_(_)) => {false} // why are these unreachable? //(Empty_, PairingHeap_(_)) => {false} // why are these unreachable?
//(PairingHeap_(_), Empty) => {false} //(PairingHeap_(_), Empty) => {false}
Expand All @@ -73,32 +73,32 @@ impl<E: Copy Eq Ord> PairingHeap<E> : Heap<E> {
pure fn find_min() -> Option<E> { pure fn find_min() -> Option<E> {
match self { match self {
Empty_ => { None } Empty_ => { None }
PairingHeap_(heap) => { Some(heap.head) } PairingHeap_(head, _) => { Some(head) }
} }
} }


pure fn delete_min() -> (Option<E>, PairingHeap<E>) { pure fn delete_min() -> (Option<E>, PairingHeap<E>) {
match self { match self {
Empty_ => {(None, self)} Empty_ => {(None, self)}
PairingHeap_(heap) => {(Some(heap.head), self.merge_pairs(heap.rest))} PairingHeap_(head, rest) => {(Some(head), self.merge_pairs(rest))}
} }
} }


pure fn merge(other: PairingHeap<E>) -> PairingHeap<E> { pure fn merge(other: PairingHeap<E>) -> PairingHeap<E> {
match (self, other) { match (self, other) {
(Empty_, b) => { b } (Empty_, b) => { b }
(a, Empty_) => { a } (a, Empty_) => { a }
(x@PairingHeap_(heapA), y@PairingHeap_(heapB)) => { (x@PairingHeap_(headA, restA), y@PairingHeap_(headB, restB)) => {
if (heapA.head.le(&heapB.head)) { if (headA.le(&headB)) {
PairingHeap_(@{ PairingHeap_(
head: heapA.head, headA,
rest: @Cons(y, heapA.rest) @Cons(y, restA)
}) )
} else { } else {
PairingHeap_(@{ PairingHeap_(
head: heapB.head, headB,
rest: @Cons(x, heapB.rest) @Cons(x, restB)
}) )
} }
} }
} }
Expand Down

0 comments on commit 6e1b582

Please sign in to comment.