Skip to content
Permalink
Browse files

Add Ord impl

  • Loading branch information...
Pratyush committed Sep 6, 2019
1 parent efa9590 commit 98c5b490abf78e8e5a4981ec78d767c9b73404b0
Showing with 19 additions and 18 deletions.
  1. +19 −18 r1cs-core/src/lib.rs
@@ -25,26 +25,9 @@ use std::cmp::Ordering;
type SmallVec<F> = StackVec<[(Variable, F); 16]>;

/// Represents a variable in a constraint system.
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[derive(PartialOrd, Ord, PartialEq, Eq, Copy, Clone, Debug)]
pub struct Variable(Index);

impl PartialOrd for Variable {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Variable {
fn cmp(&self, other: &Self) -> Ordering {
match (self.0, other.0) {
(Index::Input(ref idx1), Index::Input(ref idx2))
| (Index::Aux(ref idx1), Index::Aux(ref idx2)) => idx1.cmp(idx2),
(Index::Input(_), Index::Aux(_)) => Ordering::Less,
(Index::Aux(_), Index::Input(_)) => Ordering::Greater,
}
}
}

impl Variable {
/// This constructs a variable with an arbitrary index.
/// Circuit implementations are not recommended to use this.
@@ -68,6 +51,24 @@ pub enum Index {
Aux(usize),
}

impl PartialOrd for Index {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Index {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(Index::Input(ref idx1), Index::Input(ref idx2))
| (Index::Aux(ref idx1), Index::Aux(ref idx2)) => idx1.cmp(idx2),
(Index::Input(_), Index::Aux(_)) => Ordering::Less,
(Index::Aux(_), Index::Input(_)) => Ordering::Greater,
}
}
}


/// This represents a linear combination of some variables, with coefficients
/// in the field `F`.
/// The `(coeff, var)` pairs in a `LinearCombination` are kept sorted according

0 comments on commit 98c5b49

Please sign in to comment.
You can’t perform that action at this time.