Skip to content

require a total ordering for Eq and Ord #12435

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,7 @@ impl<T: Eq> Eq for Foo<T> {

Supported traits for `deriving` are:

* Comparison traits: `Eq`, `TotalEq`, `Ord`, `TotalOrd`.
* Comparison traits: `Eq`, `Ord`
* Serialization: `Encodable`, `Decodable`. These require `serialize`.
* `Clone` and `DeepClone`, to perform (deep) copies.
* `IterBytes`, to iterate over the bytes in a data type.
Expand Down
3 changes: 1 addition & 2 deletions src/doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2523,8 +2523,7 @@ struct Circle { radius: f64 }
enum ABC { A, B, C }
~~~

The full list of derivable traits is `Eq`, `TotalEq`, `Ord`,
`TotalOrd`, `Encodable` `Decodable`, `Clone`, `DeepClone`,
The full list of derivable traits is `Eq`, `Ord`, `Encodable` `Decodable`, `Clone`, `DeepClone`,
`IterBytes`, `Rand`, `Default`, `Zero`, `FromPrimitive` and `Show`.

# Crates and the module system
Expand Down
1 change: 0 additions & 1 deletion src/etc/generate-deriving-span-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def write_file(name, string):
for (trait, supers, errs) in [('Rand', [], 1),
('Clone', [], 1), ('DeepClone', ['Clone'], 1),
('Eq', [], 2), ('Ord', [], 8),
('TotalEq', [], 1), ('TotalOrd', ['TotalEq'], 1),
('Show', [], 1),
('Hash', [], 1)]:
traits[trait] = (ALL, supers, errs)
Expand Down
4 changes: 2 additions & 2 deletions src/etc/vim/syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ syn keyword rustTrait Bool
syn keyword rustTrait ToCStr
syn keyword rustTrait Char
syn keyword rustTrait Clone DeepClone
syn keyword rustTrait Eq Ord TotalEq TotalOrd Ordering Equiv
syn keyword rustTrait Eq Ord Ordering Equiv
syn keyword rustEnumVariant Less Equal Greater
syn keyword rustTrait Container Mutable Map MutableMap Set MutableSet
syn keyword rustTrait Default
Expand All @@ -98,7 +98,7 @@ syn keyword rustTrait ToStr IntoStr
syn keyword rustTrait Tuple1 Tuple2 Tuple3 Tuple4
syn keyword rustTrait Tuple5 Tuple6 Tuple7 Tuple8
syn keyword rustTrait Tuple9 Tuple10 Tuple11 Tuple12
syn keyword rustTrait ImmutableEqVector ImmutableTotalOrdVector ImmutableCloneableVector
syn keyword rustTrait ImmutableEqVector ImmutableOrdVector ImmutableCloneableVector
syn keyword rustTrait OwnedVector OwnedCloneableVector OwnedEqVector MutableVector
syn keyword rustTrait Vector VectorVector CloneableVector ImmutableVector

Expand Down
126 changes: 75 additions & 51 deletions src/libcollections/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct BTree<K, V> {
priv upper_bound: uint
}

impl<K: TotalOrd, V> BTree<K, V> {
impl<K: Ord, V> BTree<K, V> {

///Returns new BTree with root node (leaf) and user-supplied lower bound
///The lower bound applies to every node except the root node.
Expand Down Expand Up @@ -58,7 +58,7 @@ impl<K: TotalOrd, V> BTree<K, V> {
//We would probably want to remove the dependence on the Clone trait in the future.
//It is here as a crutch to ensure values can be passed around through the tree's nodes
//especially during insertions and deletions.
impl<K: Clone + TotalOrd, V: Clone> BTree<K, V> {
impl<K: Clone + Ord, V: Clone> BTree<K, V> {
///Returns the value of a given key, which may not exist in the tree.
///Calls the root node's get method.
pub fn get(self, k: K) -> Option<V> {
Expand All @@ -83,7 +83,7 @@ impl<K: Clone + TotalOrd, V: Clone> BTree<K, V> {
}
}

impl<K: Clone + TotalOrd, V: Clone> Clone for BTree<K, V> {
impl<K: Clone + Ord, V: Clone> Clone for BTree<K, V> {
///Implements the Clone trait for the BTree.
///Uses a helper function/constructor to produce a new BTree.
fn clone(&self) -> BTree<K, V> {
Expand All @@ -92,21 +92,25 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for BTree<K, V> {
}


impl<K: TotalOrd, V: TotalEq> TotalEq for BTree<K, V> {
impl<K: Ord, V: Eq> Eq for BTree<K, V> {
///Testing equality on BTrees by comparing the root.
fn equals(&self, other: &BTree<K, V>) -> bool {
fn eq(&self, other: &BTree<K, V>) -> bool {
self.root.cmp(&other.root) == Equal
}
}

impl<K: TotalOrd, V: TotalEq> TotalOrd for BTree<K, V> {
impl<K: Ord, V: Eq> Ord for BTree<K, V> {
fn lt(&self, other: &BTree<K, V>) -> bool {
self.cmp(other) == Less
}

///Returns an ordering based on the root nodes of each BTree.
fn cmp(&self, other: &BTree<K, V>) -> Ordering {
self.root.cmp(&other.root)
}
}

impl<K: ToStr + TotalOrd, V: ToStr> ToStr for BTree<K, V> {
impl<K: ToStr + Ord, V: ToStr> ToStr for BTree<K, V> {
///Returns a string representation of the BTree
fn to_str(&self) -> ~str {
let ret = self.root.to_str();
Expand All @@ -127,7 +131,7 @@ enum Node<K, V> {


//Node functions/methods
impl<K: TotalOrd, V> Node<K, V> {
impl<K: Ord, V> Node<K, V> {
///Creates a new leaf node given a vector of elements.
fn new_leaf(vec: ~[LeafElt<K, V>]) -> Node<K,V> {
LeafNode(Leaf::new(vec))
Expand Down Expand Up @@ -157,7 +161,7 @@ impl<K: TotalOrd, V> Node<K, V> {
}
}

impl<K: Clone + TotalOrd, V: Clone> Node<K, V> {
impl<K: Clone + Ord, V: Clone> Node<K, V> {
///Returns the corresponding value to the provided key.
///get() is called in different ways on a branch or a leaf.
fn get(&self, k: K) -> Option<V> {
Expand All @@ -176,7 +180,7 @@ impl<K: Clone + TotalOrd, V: Clone> Node<K, V> {
}
}

impl<K: Clone + TotalOrd, V: Clone> Clone for Node<K, V> {
impl<K: Clone + Ord, V: Clone> Clone for Node<K, V> {
///Returns a new node based on whether or not it is a branch or a leaf.
fn clone(&self) -> Node<K, V> {
match *self {
Expand All @@ -191,10 +195,10 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for Node<K, V> {
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for Node<K, V> {
impl<K: Ord, V: Eq> Eq for Node<K, V> {
///Returns whether two nodes are equal based on the keys of each element.
///Two nodes are equal if all of their keys are the same.
fn equals(&self, other: &Node<K, V>) -> bool{
fn eq(&self, other: &Node<K, V>) -> bool{
match *self{
BranchNode(ref branch) => {
if other.is_leaf() {
Expand All @@ -215,8 +219,12 @@ impl<K: TotalOrd, V: TotalEq> TotalEq for Node<K, V> {
}
}

impl<K: TotalOrd, V: TotalEq> TotalOrd for Node<K, V> {
///Implementation of TotalOrd for Nodes.
impl<K: Ord, V: Eq> Ord for Node<K, V> {
fn lt(&self, other: &Node<K, V>) -> bool {
self.cmp(other) == Less
}

///Implementation of Ord for Nodes.
fn cmp(&self, other: &Node<K, V>) -> Ordering {
match *self {
LeafNode(ref leaf) => {
Expand All @@ -235,7 +243,7 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for Node<K, V> {
}
}

impl<K: ToStr + TotalOrd, V: ToStr> ToStr for Node<K, V> {
impl<K: ToStr + Ord, V: ToStr> ToStr for Node<K, V> {
///Returns a string representation of a Node.
///Will iterate over the Node and show "Key: x, value: y, child: () // "
///for all elements in the Node. "Child" only exists if the Node contains
Expand All @@ -262,7 +270,7 @@ struct Branch<K, V> {
}


impl<K: TotalOrd, V> Leaf<K, V> {
impl<K: Ord, V> Leaf<K, V> {
///Creates a new Leaf from a vector of LeafElts.
fn new(vec: ~[LeafElt<K, V>]) -> Leaf<K, V> {
Leaf {
Expand Down Expand Up @@ -322,7 +330,7 @@ impl<K: TotalOrd, V> Leaf<K, V> {
}


impl<K: Clone + TotalOrd, V: Clone> Leaf<K, V> {
impl<K: Clone + Ord, V: Clone> Leaf<K, V> {
///Returns the corresponding value to the supplied key.
fn get(&self, k: K) -> Option<V> {
for s in self.elts.iter() {
Expand Down Expand Up @@ -373,21 +381,25 @@ impl<K: Clone + TotalOrd, V: Clone> Leaf<K, V> {
}
}

impl<K: Clone + TotalOrd, V: Clone> Clone for Leaf<K, V> {
impl<K: Clone + Ord, V: Clone> Clone for Leaf<K, V> {
///Returns a new Leaf with the same elts.
fn clone(&self) -> Leaf<K, V> {
Leaf::new(self.elts.clone())
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for Leaf<K, V> {
///Implementation of equals function for leaves that compares LeafElts.
fn equals(&self, other: &Leaf<K, V>) -> bool {
self.elts.equals(&other.elts)
impl<K: Ord, V: Eq> Eq for Leaf<K, V> {
///Implementation of eq function for leaves that compares LeafElts.
fn eq(&self, other: &Leaf<K, V>) -> bool {
self.elts.eq(&other.elts)
}
}

impl<K: TotalOrd, V: TotalEq> TotalOrd for Leaf<K, V> {
impl<K: Ord, V: Eq> Ord for Leaf<K, V> {
fn lt(&self, other: &Leaf<K, V>) -> bool {
self.cmp(other) == Less
}

///Returns an ordering based on the first element of each Leaf.
fn cmp(&self, other: &Leaf<K, V>) -> Ordering {
if self.elts.len() > other.elts.len() {
Expand All @@ -401,15 +413,15 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for Leaf<K, V> {
}


impl<K: ToStr + TotalOrd, V: ToStr> ToStr for Leaf<K, V> {
impl<K: ToStr + Ord, V: ToStr> ToStr for Leaf<K, V> {
///Returns a string representation of a Leaf.
fn to_str(&self) -> ~str {
self.elts.iter().map(|s| s.to_str()).to_owned_vec().connect(" // ")
}
}


impl<K: TotalOrd, V> Branch<K, V> {
impl<K: Ord, V> Branch<K, V> {
///Creates a new Branch from a vector of BranchElts and a rightmost child (a node).
fn new(vec: ~[BranchElt<K, V>], right: ~Node<K, V>) -> Branch<K, V> {
Branch {
Expand Down Expand Up @@ -467,7 +479,7 @@ impl<K: TotalOrd, V> Branch<K, V> {
}
}

impl<K: Clone + TotalOrd, V: Clone> Branch<K, V> {
impl<K: Clone + Ord, V: Clone> Branch<K, V> {
///Returns the corresponding value to the supplied key.
///If the key is not there, find the child that might hold it.
fn get(&self, k: K) -> Option<V> {
Expand Down Expand Up @@ -591,21 +603,25 @@ impl<K: Clone + TotalOrd, V: Clone> Branch<K, V> {
}
}

impl<K: Clone + TotalOrd, V: Clone> Clone for Branch<K, V> {
impl<K: Clone + Ord, V: Clone> Clone for Branch<K, V> {
///Returns a new branch using the clone methods of the Branch's internal variables.
fn clone(&self) -> Branch<K, V> {
Branch::new(self.elts.clone(), self.rightmost_child.clone())
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for Branch<K, V> {
///Equals function for Branches--compares all the elements in each branch
fn equals(&self, other: &Branch<K, V>) -> bool {
self.elts.equals(&other.elts)
impl<K: Ord, V: Eq> Eq for Branch<K, V> {
///eq function for Branches--compares all the elements in each branch
fn eq(&self, other: &Branch<K, V>) -> bool {
self.elts.eq(&other.elts)
}
}

impl<K: TotalOrd, V: TotalEq> TotalOrd for Branch<K, V> {
impl<K: Ord, V: Eq> Ord for Branch<K, V> {
fn lt(&self, other: &Branch<K, V>) -> bool {
self.cmp(other) == Less
}

///Compares the first elements of two branches to determine an ordering
fn cmp(&self, other: &Branch<K, V>) -> Ordering {
if self.elts.len() > other.elts.len() {
Expand All @@ -618,7 +634,7 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for Branch<K, V> {
}
}

impl<K: ToStr + TotalOrd, V: ToStr> ToStr for Branch<K, V> {
impl<K: ToStr + Ord, V: ToStr> ToStr for Branch<K, V> {
///Returns a string representation of a Branch.
fn to_str(&self) -> ~str {
let mut ret = self.elts.iter().map(|s| s.to_str()).to_owned_vec().connect(" // ");
Expand All @@ -641,7 +657,7 @@ struct BranchElt<K, V> {
value: V
}

impl<K: TotalOrd, V> LeafElt<K, V> {
impl<K: Ord, V> LeafElt<K, V> {
///Creates a new LeafElt from a supplied key-value pair.
fn new(k: K, v: V) -> LeafElt<K, V> {
LeafElt {
Expand All @@ -651,36 +667,40 @@ impl<K: TotalOrd, V> LeafElt<K, V> {
}
}

impl<K: Clone + TotalOrd, V: Clone> Clone for LeafElt<K, V> {
impl<K: Clone + Ord, V: Clone> Clone for LeafElt<K, V> {
///Returns a new LeafElt by cloning the key and value.
fn clone(&self) -> LeafElt<K, V> {
LeafElt::new(self.key.clone(), self.value.clone())
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for LeafElt<K, V> {
///TotalEq for LeafElts
fn equals(&self, other: &LeafElt<K, V>) -> bool {
self.key.equals(&other.key) && self.value.equals(&other.value)
impl<K: Ord, V: Eq> Eq for LeafElt<K, V> {
///Eq for LeafElts
fn eq(&self, other: &LeafElt<K, V>) -> bool {
self.key.eq(&other.key) && self.value.eq(&other.value)
}
}

impl<K: TotalOrd, V: TotalEq> TotalOrd for LeafElt<K, V> {
impl<K: Ord, V: Eq> Ord for LeafElt<K, V> {
fn lt(&self, other: &LeafElt<K, V>) -> bool {
self.cmp(other) == Less
}

///Returns an ordering based on the keys of the LeafElts.
fn cmp(&self, other: &LeafElt<K, V>) -> Ordering {
self.key.cmp(&other.key)
}
}

impl<K: ToStr + TotalOrd, V: ToStr> ToStr for LeafElt<K, V> {
impl<K: ToStr + Ord, V: ToStr> ToStr for LeafElt<K, V> {
///Returns a string representation of a LeafElt.
fn to_str(&self) -> ~str {
format!("Key: {}, value: {};",
self.key.to_str(), self.value.to_str())
}
}

impl<K: TotalOrd, V> BranchElt<K, V> {
impl<K: Ord, V> BranchElt<K, V> {
///Creates a new BranchElt from a supplied key, value, and left child.
fn new(k: K, v: V, n: ~Node<K, V>) -> BranchElt<K, V> {
BranchElt {
Expand All @@ -692,7 +712,7 @@ impl<K: TotalOrd, V> BranchElt<K, V> {
}


impl<K: Clone + TotalOrd, V: Clone> Clone for BranchElt<K, V> {
impl<K: Clone + Ord, V: Clone> Clone for BranchElt<K, V> {
///Returns a new BranchElt by cloning the key, value, and left child.
fn clone(&self) -> BranchElt<K, V> {
BranchElt::new(self.key.clone(),
Expand All @@ -701,21 +721,25 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for BranchElt<K, V> {
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for BranchElt<K, V>{
///TotalEq for BranchElts
fn equals(&self, other: &BranchElt<K, V>) -> bool {
self.key.equals(&other.key)&&self.value.equals(&other.value)
impl<K: Ord, V: Eq> Eq for BranchElt<K, V>{
///Eq for BranchElts
fn eq(&self, other: &BranchElt<K, V>) -> bool {
self.key.eq(&other.key)&&self.value.eq(&other.value)
}
}

impl<K: TotalOrd, V: TotalEq> TotalOrd for BranchElt<K, V> {
///Fulfills TotalOrd for BranchElts
impl<K: Ord, V: Eq> Ord for BranchElt<K, V> {
fn lt(&self, other: &BranchElt<K, V>) -> bool {
self.cmp(other) == Less
}

///Fulfills Ord for BranchElts
fn cmp(&self, other: &BranchElt<K, V>) -> Ordering {
self.key.cmp(&other.key)
}
}

impl<K: ToStr + TotalOrd, V: ToStr> ToStr for BranchElt<K, V> {
impl<K: ToStr + Ord, V: ToStr> ToStr for BranchElt<K, V> {
///Returns string containing key, value, and child (which should recur to a leaf)
///Consider changing in future to be more readable.
fn to_str(&self) -> ~str {
Expand Down Expand Up @@ -825,7 +849,7 @@ mod test_btree {
fn btree_clone_test() {
let b = BTree::new(1, ~"abc", 2);
let b2 = b.clone();
assert!(b.root.equals(&b2.root))
assert!(b.root.eq(&b2.root))
}

//Tests the BTree's cmp() method when one node is "less than" another.
Expand Down
Loading