ソースを参照

Fixed Ordering implementation.

Apparently, from the documentation, if you are going to implement Ord
manually you need to do the same for the rest of the traits.
AvariceLHubris 1 年間 前
コミット
baa0d87c9b
1 ファイル変更19 行追加2 行削除
  1. 19 2
      src/hufftree/canonical.rs

+ 19 - 2
src/hufftree/canonical.rs

@@ -7,7 +7,7 @@ pub struct CanonicalHufftree {
     characters_and_codes: BiMap<char, BitVec>,
 }
 
-#[derive(PartialEq, PartialOrd, Eq, Debug)]
+#[derive(Debug)]
 struct CharTempCode {
     character: char,
     code: BitVec,
@@ -16,10 +16,25 @@ struct CharTempCode {
 
 impl Ord for CharTempCode {
     fn cmp(&self, other: &Self) -> std::cmp::Ordering {
-        return self.code_length.cmp(&other.code_length);
+        let ordering = self.code_length.cmp(&other.code_length);
+        ordering
+    }
+}
+
+impl PartialOrd for CharTempCode {
+    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
+        Some(self.cmp(other))
     }
 }
 
+impl PartialEq for CharTempCode {
+    fn eq(&self, other: &Self) -> bool {
+        return self.code_length == other.code_length;
+    }
+}
+
+impl Eq for CharTempCode {}
+
 impl CanonicalHufftree {
     pub fn from_tree(base_tree: Hufftree) -> Self {
         let characters = base_tree.get_characters();
@@ -37,8 +52,10 @@ impl CanonicalHufftree {
         }
 
         character_and_codes.sort();
+        // println!("Ordered characters: {:?}", character_and_codes);
         let mut character_and_codes: Vec<CharTempCode> =
             character_and_codes.into_iter().rev().collect();
+        // println!("Ordered characters: {:?}", character_and_codes);
 
         let mut first = true;
         let mut prev_length = 0;