瀏覽代碼

Scalar subtraction of a BigDigit from a BigUint

A BigDigit can be subtracted from a BigUint - this is one of several
operations being implemented to allow scalar operations on BigInt and
BigUint across the board.
Sam Cappleman-Lynes 7 年之前
父節點
當前提交
7b7799eab7
共有 2 個文件被更改,包括 31 次插入0 次删除
  1. 10 0
      bigint/src/biguint.rs
  2. 21 0
      bigint/src/tests/biguint.rs

+ 10 - 0
bigint/src/biguint.rs

@@ -437,6 +437,16 @@ impl<'a> Sub<BigUint> for &'a BigUint {
     }
     }
 }
 }
 
 
+impl Sub<BigDigit> for BigUint {
+    type Output = BigUint;
+
+    #[inline]
+    fn sub(mut self, other: BigDigit) -> BigUint {
+        sub2(&mut self.data[..], &[other]);
+        self.normalize()
+    }
+}
+
 forward_all_binop_to_ref_ref!(impl Mul for BigUint, mul);
 forward_all_binop_to_ref_ref!(impl Mul for BigUint, mul);
 
 
 impl<'a, 'b> Mul<&'b BigUint> for &'a BigUint {
 impl<'a, 'b> Mul<&'b BigUint> for &'a BigUint {

+ 21 - 0
bigint/src/tests/biguint.rs

@@ -723,6 +723,27 @@ fn test_sub() {
     }
     }
 }
 }
 
 
+#[test]
+fn test_scalar_sub() {
+    for elm in SUM_TRIPLES.iter() {
+        let (a_vec, b_vec, c_vec) = *elm;
+
+        if a_vec.len() == 1 {
+            let a = a_vec[0];
+            let b = BigUint::from_slice(b_vec);
+            let c = BigUint::from_slice(c_vec);
+            assert!(c - a == b);
+        }
+
+        if b_vec.len() == 1 {
+            let a = BigUint::from_slice(a_vec);
+            let b = b_vec[0];
+            let c = BigUint::from_slice(c_vec);
+            assert!(c - b == a);
+        }
+    }
+}
+
 #[test]
 #[test]
 #[should_panic]
 #[should_panic]
 fn test_sub_fail_on_underflow() {
 fn test_sub_fail_on_underflow() {