Comparisons!

This commit is contained in:
2018-03-24 12:03:29 -07:00
parent 9c4ea7ae26
commit 6b9783c69a
3 changed files with 24 additions and 13 deletions

View File

@@ -9,6 +9,7 @@ license-file = "LICENSE"
repository = "https://github.com/acw/simple_crypto" repository = "https://github.com/acw/simple_crypto"
[dependencies] [dependencies]
num = "^0.1.42"
rand = "^0.3" rand = "^0.3"
[dev-dependencies] [dev-dependencies]

View File

@@ -71,20 +71,23 @@ impl PartialOrd for UCN {
impl Ord for UCN { impl Ord for UCN {
fn cmp(&self, other: &UCN) -> Ordering { fn cmp(&self, other: &UCN) -> Ordering {
let mut iter_left = self.contents.iter(); match self.contents.len().cmp(&other.contents.len()) {
let mut iter_right = other.contents.iter(); Ordering::Equal => {
let mut me = self.contents.iter().rev();
let mut them = other.contents.iter().rev();
loop { for (m, t) in me.zip(them) {
match (iter_left.next(), iter_right.next()) { match m.cmp(t) {
(None, None) => return Ordering::Equal, Ordering::Equal =>
(Some(_), None) => return Ordering::Greater, continue,
(None, Some(_)) => return Ordering::Less, res =>
(Some(x), Some(y)) => return res
match x.cmp(y) {
Ordering::Equal => continue,
result => return result
} }
} }
Ordering::Equal
}
x => x
} }
} }
} }
@@ -161,5 +164,11 @@ mod test {
ucnx.cmp(&ucny) == Ordering::Greater ucnx.cmp(&ucny) == Ordering::Greater
} }
} }
fn self_is_equal(x: Vec<u64>) -> bool {
let val = UCN{ contents: x };
let copy = val.clone();
(&val == &copy) && (val.cmp(&copy) == Ordering::Equal)
}
} }
} }

View File

@@ -11,13 +11,14 @@
//! when they should use it, and examples. For now, it mostly just fowards //! when they should use it, and examples. For now, it mostly just fowards
//! off to more detailed modules. Help requested! //! off to more detailed modules. Help requested!
extern crate num;
#[cfg(test)] #[cfg(test)]
#[macro_use] #[macro_use]
extern crate quickcheck; extern crate quickcheck;
extern crate rand; extern crate rand;
/// The cryptonum module provides support for large numbers at fixed, /// The cryptonum module provides support for large numbers for use in various
/// cryptographically-relevant sizes. /// cryptographically-relevant algorithms.
pub mod cryptonum; pub mod cryptonum;
#[cfg(test)] #[cfg(test)]