6 /// Return the smaller of the two
7 fn min<'a>(&'a self, other: &'a Self) -> &'a Self;
10 /// Return a pointer to the minimal value of `v`.
11 pub fn vec_min<T: Minimum>(v: &Vec<T>) -> Option<&T> {
14 min = Some(match min {
23 data: Vec<u64>, // least significant digits first. The last block will *not* be 0.
26 // Add with carry, returning the sum and the carry
27 fn overflowing_add(a: u64, b: u64, carry: bool) -> (u64, bool) {
28 let sum = u64::wrapping_add(a, b);
29 let carry_n = if carry { 1 } else { 0 };
30 if sum >= a { // the first sum did not overflow
31 let sum_total = u64::wrapping_add(sum, carry_n);
32 let had_overflow = sum_total < sum;
33 (sum_total, had_overflow)
34 } else { // the first sum did overflow
35 // it is impossible for this to overflow again, as we are just adding 0 or 1
41 /// Construct a BigInt from a "small" one.
42 pub fn new(x: u64) -> Self {
43 if x == 0 { // take care of our invariant!
44 BigInt { data: vec![] }
46 BigInt { data: vec![x] }
50 fn test_invariant(&self) -> bool {
51 if self.data.len() == 0 {
54 self.data[self.data.len() - 1] != 0
58 /// Construct a BigInt from a vector of 64-bit "digits", with the last significant digit being first
59 pub fn from_vec(mut v: Vec<u64>) -> Self {
60 // remove trailing zeroes
61 while v.len() > 0 && v[v.len()-1] == 0 {
67 /// Increments the number by 1. Solution to 05.1.
68 pub fn inc1(&mut self) {
70 // This loop adds "(1 << idx)". If there is no more carry, we leave.
71 while idx < self.data.len() {
72 let cur = self.data[idx];
73 let sum = u64::wrapping_add(cur, 1);
76 // No overflow, we are done.
83 // If we came here, there is a last carry to add
87 /// Increments the number by "by".
88 pub fn inc(&mut self, mut by: u64) {
90 // This loop adds "by * (1 << idx)". Think of "by" as the carry from incrementing the last digit.
91 while idx < self.data.len() {
92 let cur = self.data[idx];
93 let sum = u64::wrapping_add(cur, by);
96 // No overflow, we are done.
99 // We need to add a carry.
104 // If we came here, there is a last carry to add
108 /// Return the nth power-of-2 as BigInt
109 pub fn power_of_2(mut power: u64) -> BigInt {
110 let mut v = Vec::new();
120 impl Clone for BigInt {
121 fn clone(&self) -> Self {
122 BigInt { data: self.data.clone() }
126 impl PartialEq for BigInt {
127 fn eq(&self, other: &BigInt) -> bool {
128 debug_assert!(self.test_invariant() && other.test_invariant());
129 self.data == other.data
133 impl Minimum for BigInt {
134 fn min<'a>(&'a self, other: &'a Self) -> &'a Self {
135 debug_assert!(self.test_invariant() && other.test_invariant());
136 if self.data.len() < other.data.len() {
138 } else if self.data.len() > other.data.len() {
141 // compare back-to-front, i.e., most significant digit first
142 let mut idx = self.data.len()-1;
144 if self.data[idx] < other.data[idx] {
146 } else if self.data[idx] > other.data[idx] {
159 impl fmt::Debug for BigInt {
160 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
165 impl<'a, 'b> ops::Add<&'a BigInt> for &'b BigInt {
166 type Output = BigInt;
167 fn add(self, rhs: &'a BigInt) -> Self::Output {
168 let mut result_vec:Vec<u64> = Vec::with_capacity(cmp::max(self.data.len(), rhs.data.len()));
169 let mut carry:bool = false; // the carry bit
170 for (i, val) in (&self.data).into_iter().enumerate() {
171 // compute next digit and carry
172 let rhs_val = if i < rhs.data.len() { rhs.data[i] } else { 0 };
173 let (sum, new_carry) = overflowing_add(*val, rhs_val, carry);
175 result_vec.push(sum);
181 // We know that the invariant holds: overflowing_add would only return (0, false) if
182 // the arguments are (0, 0, false), but we know that in the last iteration, `val` is the
183 // last digit of `self` and hence not 0.
184 BigInt { data: result_vec }
188 impl<'a> ops::Add<BigInt> for &'a BigInt {
189 type Output = BigInt;
191 fn add(self, rhs: BigInt) -> Self::Output {
196 impl<'a> ops::Add<&'a BigInt> for BigInt {
197 type Output = BigInt;
199 fn add(self, rhs: &'a BigInt) -> Self::Output {
204 impl ops::Add<BigInt> for BigInt {
205 type Output = BigInt;
207 fn add(self, rhs: BigInt) -> Self::Output {
215 use super::overflowing_add;
219 fn test_overflowing_add() {
220 assert_eq!(overflowing_add(10, 100, false), (110, false));
221 assert_eq!(overflowing_add(10, 100, true), (111, false));
222 assert_eq!(overflowing_add(1 << 63, 1 << 63, false), (0, true));
223 assert_eq!(overflowing_add(1 << 63, 1 << 63, true), (1, true));
224 assert_eq!(overflowing_add(1 << 63, (1 << 63) -1 , true), (0, true));
229 let mut b = BigInt::new(0);
231 assert_eq!(b, BigInt::new(1));
233 assert_eq!(b, BigInt::new(2));
235 b = BigInt::new(u64::MAX);
237 assert_eq!(b, BigInt::from_vec(vec![0, 1]));
239 assert_eq!(b, BigInt::from_vec(vec![1, 1]));
243 fn test_power_of_2() {
244 assert_eq!(BigInt::power_of_2(0), BigInt::new(1));
245 assert_eq!(BigInt::power_of_2(13), BigInt::new(1 << 13));
246 assert_eq!(BigInt::power_of_2(64), BigInt::from_vec(vec![0, 1]));
247 assert_eq!(BigInt::power_of_2(96), BigInt::from_vec(vec![0, 1 << 32]));
248 assert_eq!(BigInt::power_of_2(128), BigInt::from_vec(vec![0, 0, 1]));