Advertisement
mwchase

LC:NN upload 3 - node_ordering.rs

Mar 19th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.52 KB | None | 0 0
  1. use std::cmp::Ordering;
  2.  
  3. use node_defs::{Container, NodeData, Sum, Product, Power, Primitive};
  4.  
  5. #[derive(Eq, Ord, PartialEq, PartialOrd)]
  6. enum PrimitiveType {
  7.     Input,
  8.     SystemVariable,
  9.     Parameter,
  10.     Sigmoid,
  11. }
  12.  
  13. impl<T: NodeData> Primitive<T> {
  14.     fn simplify(&self) -> PrimitiveType {
  15.         match self {
  16.             &Primitive::Input(_) => PrimitiveType::Input,
  17.             &Primitive::SystemVariable(_) => PrimitiveType::SystemVariable,
  18.             &Primitive::Parameter(_) => PrimitiveType::Parameter,
  19.             &Primitive::Sigmoid(_, _) => PrimitiveType::Sigmoid,
  20.         }
  21.     }
  22. }
  23.  
  24. impl<T: NodeData> Ord for Sum<T> {
  25.     fn cmp(&self, other: &Sum<T>) -> Ordering {
  26.         let cmp_result = self.constant.cmp(&other.constant);
  27.         match cmp_result {
  28.             Ordering::Equal => {
  29.                 for (self_term, other_term) in
  30.                     self.terms
  31.                         .clone()
  32.                         .into_iter()
  33.                         .zip(other.terms.clone()) {
  34.                     let cmp_result = self_term.cmp(&other_term);
  35.                     match cmp_result {
  36.                         Ordering::Equal => (),
  37.                         _ => return cmp_result,
  38.                     }
  39.                 }
  40.                 let cmp_result = self.terms.len().cmp(&other.terms.len());
  41.                 match cmp_result {
  42.                     Ordering::Equal => self.minus.cmp(&other.minus),
  43.                     _ => cmp_result,
  44.                 }
  45.             }
  46.             _ => cmp_result,
  47.         }
  48.     }
  49. }
  50.  
  51. impl<T: NodeData> Ord for Product<T> {
  52.     fn cmp(&self, other: &Product<T>) -> Ordering {
  53.         let cmp_result = self.fuzzy_cmp(other);
  54.         match cmp_result {
  55.             Ordering::Equal => self.coefficient.cmp(&other.coefficient),
  56.             _ => cmp_result,
  57.         }
  58.     }
  59. }
  60.  
  61. impl<T: NodeData> Product<T> {
  62.     pub fn fuzzy_cmp(&self, other: &Product<T>) -> Ordering {
  63.         for (self_power, other_power) in
  64.             self.powers
  65.                 .clone()
  66.                 .into_iter()
  67.                 .zip(other.powers.clone()) {
  68.             let cmp_result = self_power.cmp(&other_power);
  69.             match cmp_result {
  70.                 Ordering::Equal => (),
  71.                 _ => return cmp_result,
  72.             }
  73.         }
  74.         self.powers.len().cmp(&other.powers.len())
  75.     }
  76. }
  77.  
  78. impl<T: NodeData> Ord for Power<T> {
  79.     fn cmp(&self, other: &Power<T>) -> Ordering {
  80.         match self.fuzzy_cmp(&other) {
  81.             Ordering::Equal => self.exponent.cmp(&other.exponent),
  82.             cmp_result => cmp_result,
  83.         }
  84.     }
  85. }
  86.  
  87. impl<T: NodeData> Power<T> {
  88.     pub fn fuzzy_cmp(&self, other: &Power<T>) -> Ordering {
  89.         self.primitive.cmp(&other.primitive)
  90.     }
  91. }
  92.  
  93. impl<T: NodeData> Ord for Primitive<T> {
  94.     fn cmp(&self, other: &Primitive<T>) -> Ordering {
  95.         match (self, other) {
  96.             (&Primitive::Input(ref a), &Primitive::Input(ref b)) => a.cmp(b),
  97.             (&Primitive::SystemVariable(ref a), &Primitive::SystemVariable(ref b)) => a.cmp(b),
  98.             (&Primitive::Parameter(ref a), &Primitive::Parameter(ref b)) => a.cmp(b),
  99.             (&Primitive::Sigmoid(minus_a, ref a), &Primitive::Sigmoid(minus_b, ref b)) => {
  100.                 let cmp_result = a.cmp(b);
  101.                 match cmp_result {
  102.                     Ordering::Equal => minus_a.cmp(&minus_b),
  103.                     _ => cmp_result,
  104.                 }
  105.             }
  106.             _ => self.simplify().cmp(&other.simplify()),
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement