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 max_len = cmp::max(self.data.len(), rhs.data.len());
170 let mut result_vec:Vec<u64> = Vec::with_capacity(max_len);
171 let mut carry:bool = false; // the carry bit
172 for i in 0..max_len {
173 // compute next digit and carry
174 let lhs_val = if i < self.data.len() { self.data[i] } else { 0 };
175 let rhs_val = if i < rhs.data.len() { rhs.data[i] } else { 0 };
176 let (sum, new_carry) = overflowing_add(lhs_val, rhs_val, carry);
178 result_vec.push(sum);
184 // We know that the invariant holds: overflowing_add would only return (0, false) if
185 // the arguments are (0, 0, false), but we know that in the last iteration, one od the two digits
186 // is the last of its number and hence not 0.
187 BigInt { data: result_vec }
191 impl<'a> ops::Add<BigInt> for &'a BigInt {
192 type Output = BigInt;
194 fn add(self, rhs: BigInt) -> Self::Output {
199 impl<'a> ops::Add<&'a BigInt> for BigInt {
200 type Output = BigInt;
202 fn add(self, rhs: &'a BigInt) -> Self::Output {
207 impl ops::Add<BigInt> for BigInt {
208 type Output = BigInt;
210 fn add(self, rhs: BigInt) -> Self::Output {
218 use super::overflowing_add;
222 fn test_overflowing_add() {
223 assert_eq!(overflowing_add(10, 100, false), (110, false));
224 assert_eq!(overflowing_add(10, 100, true), (111, false));
225 assert_eq!(overflowing_add(1 << 63, 1 << 63, false), (0, true));
226 assert_eq!(overflowing_add(1 << 63, 1 << 63, true), (1, true));
227 assert_eq!(overflowing_add(1 << 63, (1 << 63) -1 , true), (0, true));
232 let b1 = BigInt::new(1 << 32);
233 let b2 = BigInt::from_vec(vec![0, 1]);
235 assert_eq!(&b1 + &b2, BigInt::from_vec(vec![1 << 32, 1]));
240 let mut b = BigInt::new(0);
242 assert_eq!(b, BigInt::new(1));
244 assert_eq!(b, BigInt::new(2));
246 b = BigInt::new(u64::MAX);
248 assert_eq!(b, BigInt::from_vec(vec![0, 1]));
250 assert_eq!(b, BigInt::from_vec(vec![1, 1]));
254 fn test_power_of_2() {
255 assert_eq!(BigInt::power_of_2(0), BigInt::new(1));
256 assert_eq!(BigInt::power_of_2(13), BigInt::new(1 << 13));
257 assert_eq!(BigInt::power_of_2(64), BigInt::from_vec(vec![0, 1]));
258 assert_eq!(BigInt::power_of_2(96), BigInt::from_vec(vec![0, 1 << 32]));
259 assert_eq!(BigInt::power_of_2(128), BigInt::from_vec(vec![0, 0, 1]));