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. Solution to 05.1.
59 pub fn from_vec(mut v: Vec<u64>) -> Self {
60 // remove trailing zeros
61 while v.len() > 0 && v[v.len()-1] == 0 {
67 /// Increments the number by 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 // This is essentially the solution to 06.1.
135 fn min<'a>(&'a self, other: &'a Self) -> &'a Self {
136 debug_assert!(self.test_invariant() && other.test_invariant());
137 if self.data.len() < other.data.len() {
139 } else if self.data.len() > other.data.len() {
142 // compare back-to-front, i.e., most significant digit first
143 let mut idx = self.data.len()-1;
145 if self.data[idx] < other.data[idx] {
147 } else if self.data[idx] > other.data[idx] {
160 impl fmt::Debug for BigInt {
161 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
166 impl<'a, 'b> ops::Add<&'a BigInt> for &'b BigInt {
167 type Output = BigInt;
168 fn add(self, rhs: &'a BigInt) -> Self::Output {
169 let mut result_vec:Vec<u64> = Vec::with_capacity(cmp::max(self.data.len(), rhs.data.len()));
170 let mut carry:bool = false; // the carry bit
171 for (i, val) in (&self.data).into_iter().enumerate() {
172 // compute next digit and carry
173 let rhs_val = if i < rhs.data.len() { rhs.data[i] } else { 0 };
174 let (sum, new_carry) = overflowing_add(*val, rhs_val, carry);
176 result_vec.push(sum);
182 // We know that the invariant holds: overflowing_add would only return (0, false) if
183 // the arguments are (0, 0, false), but we know that in the last iteration, `val` is the
184 // last digit of `self` and hence not 0.
185 BigInt { data: result_vec }
189 impl<'a> ops::Add<BigInt> for &'a BigInt {
190 type Output = BigInt;
192 fn add(self, rhs: BigInt) -> Self::Output {
197 impl<'a> ops::Add<&'a BigInt> for BigInt {
198 type Output = BigInt;
200 fn add(self, rhs: &'a BigInt) -> Self::Output {
205 impl ops::Add<BigInt> for BigInt {
206 type Output = BigInt;
208 fn add(self, rhs: BigInt) -> Self::Output {
216 use super::overflowing_add;
220 fn test_overflowing_add() {
221 assert_eq!(overflowing_add(10, 100, false), (110, false));
222 assert_eq!(overflowing_add(10, 100, true), (111, false));
223 assert_eq!(overflowing_add(1 << 63, 1 << 63, false), (0, true));
224 assert_eq!(overflowing_add(1 << 63, 1 << 63, true), (1, true));
225 assert_eq!(overflowing_add(1 << 63, (1 << 63) -1 , true), (0, true));
230 let mut b = BigInt::new(0);
232 assert_eq!(b, BigInt::new(1));
234 assert_eq!(b, BigInt::new(2));
236 b = BigInt::new(u64::MAX);
238 assert_eq!(b, BigInt::from_vec(vec![0, 1]));
240 assert_eq!(b, BigInt::from_vec(vec![1, 1]));
244 fn test_power_of_2() {
245 assert_eq!(BigInt::power_of_2(0), BigInt::new(1));
246 assert_eq!(BigInt::power_of_2(13), BigInt::new(1 << 13));
247 assert_eq!(BigInt::power_of_2(64), BigInt::from_vec(vec![0, 1]));
248 assert_eq!(BigInt::power_of_2(96), BigInt::from_vec(vec![0, 1 << 32]));
249 assert_eq!(BigInt::power_of_2(128), BigInt::from_vec(vec![0, 0, 1]));