From e152bbcb4664203ed711368a656102fa2a3eec49 Mon Sep 17 00:00:00 2001 From: Oliver Middleton Date: Mon, 14 Dec 2015 12:41:18 +0000 Subject: [PATCH] bigint: simplify Hash There cannot be any leading zeros in a BigUint so just derive Hash which will just hash the Vec directly. Add Hash to Sign so we can derive it for BigInt as well. --- src/bigint.rs | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/src/bigint.rs b/src/bigint.rs index afb7df4..c3262b4 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -66,7 +66,7 @@ use std::iter::repeat; use std::num::ParseIntError; use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub}; use std::str::{self, FromStr}; -use std::{fmt, hash}; +use std::fmt; use std::cmp::Ordering::{self, Less, Greater, Equal}; use std::{i64, u64}; @@ -175,7 +175,7 @@ fn div_wide(hi: BigDigit, lo: BigDigit, divisor: BigDigit) -> (BigDigit, BigDigi /// /// A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number /// `(a + b * big_digit::BASE + c * big_digit::BASE^2)`. -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash)] pub struct BigUint { data: Vec } @@ -222,22 +222,6 @@ impl Default for BigUint { fn default() -> BigUint { Zero::zero() } } -impl hash::Hash for BigUint { - fn hash(&self, state: &mut H) where H: hash::Hasher { - // hash 0 in case it's all 0's - 0u32.hash(state); - - let mut found_first_value = false; - for elem in self.data.iter().rev() { - // don't hash any leading 0's, they shouldn't affect the hash - if found_first_value || *elem != 0 { - found_first_value = true; - elem.hash(state); - } - } - } -} - impl fmt::Display for BigUint { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.to_str_radix(10)) @@ -1637,7 +1621,7 @@ fn get_radix_base(radix: u32) -> (DoubleBigDigit, usize) { } /// A Sign is a `BigInt`'s composing element. -#[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Debug, RustcEncodable, RustcDecodable, Hash)] pub enum Sign { Minus, NoSign, Plus } impl Neg for Sign { @@ -1668,7 +1652,7 @@ impl Mul for Sign { } /// A big signed integer type. -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Hash)] pub struct BigInt { sign: Sign, data: BigUint @@ -1715,13 +1699,6 @@ impl fmt::Display for BigInt { } } -impl hash::Hash for BigInt { - fn hash(&self, state: &mut H) where H: hash::Hasher { - (self.sign == Plus).hash(state); - self.data.hash(state); - } -} - impl FromStr for BigInt { type Err = ParseBigIntError;