about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNODA, Kai <nodakai@gmail.com>2014-11-17 22:12:54 +0800
committerNODA, Kai <nodakai@gmail.com>2014-11-18 10:42:27 +0800
commit3fcf2840a484159c9e27601dc9480f9636d2f2e5 (patch)
treed5533bf566c33b4c7eac0517c69bba186f05433a
parent803aacd5aef78f90fdd06ae7653fc20eec224992 (diff)
downloadrust-3fcf2840a484159c9e27601dc9480f9636d2f2e5.tar.gz
rust-3fcf2840a484159c9e27601dc9480f9636d2f2e5.zip
libcore: add num::Int::pow() and deprecate num::pow().
Signed-off-by: NODA, Kai <nodakai@gmail.com>
-rw-r--r--src/libcore/num/mod.rs47
-rw-r--r--src/libstd/num/mod.rs6
-rw-r--r--src/test/bench/shootout-binarytrees.rs4
-rw-r--r--src/test/compile-fail/lint-dead-code-4.rs4
4 files changed, 33 insertions, 28 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs
index f5505ff8e76..df8538fe3f2 100644
--- a/src/libcore/num/mod.rs
+++ b/src/libcore/num/mod.rs
@@ -37,28 +37,10 @@ pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
 }
 
 /// Raises a `base` to the power of `exp`, using exponentiation by squaring.
-///
-/// # Example
-///
-/// ```rust
-/// use std::num;
-///
-/// assert_eq!(num::pow(2i, 4), 16);
-/// ```
 #[inline]
-pub fn pow<T: Int>(mut base: T, mut exp: uint) -> T {
-    if exp == 1 { base }
-    else {
-        let mut acc: T = Int::one();
-        while exp > 0 {
-            if (exp & 1) == 1 {
-                acc = acc * base;
-            }
-            base = base * base;
-            exp = exp >> 1;
-        }
-        acc
-    }
+#[deprecated = "Use Int::pow() instead, as in 2i.pow(4)"]
+pub fn pow<T: Int>(base: T, exp: uint) -> T {
+    base.pow(exp)
 }
 
 /// A built-in signed or unsigned integer.
@@ -359,6 +341,29 @@ pub trait Int
             None                         => Int::max_value(),
         }
     }
+
+    /// Raises self to the power of `exp`, using exponentiation by squaring.
+    ///
+    /// # Example
+    ///
+    /// ```rust
+    /// use std::num::Int;
+    ///
+    /// assert_eq!(2i.pow(4), 16);
+    /// ```
+    #[inline]
+    fn pow(self, mut exp: uint) -> Self {
+        let mut base = self;
+        let mut acc: Self = Int::one();
+        while exp > 0 {
+            if (exp & 1) == 1 {
+                acc = acc * base;
+            }
+            base = base * base;
+            exp /= 2;
+        }
+        acc
+    }
 }
 
 macro_rules! checked_op {
diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs
index 2ce6c0e6e71..f49b2b0deed 100644
--- a/src/libstd/num/mod.rs
+++ b/src/libstd/num/mod.rs
@@ -757,7 +757,7 @@ mod tests {
         }
         macro_rules! assert_pow(
             (($num:expr, $exp:expr) => $expected:expr) => {{
-                let result = pow($num, $exp);
+                let result = $num.pow($exp);
                 assert_eq!(result, $expected);
                 assert_eq!(result, naive_pow($num, $exp));
             }}
@@ -775,12 +775,12 @@ mod tests {
 mod bench {
     extern crate test;
     use self::test::Bencher;
-    use num;
+    use num::Int;
     use prelude::*;
 
     #[bench]
     fn bench_pow_function(b: &mut Bencher) {
         let v = Vec::from_fn(1024u, |n| n);
-        b.iter(|| {v.iter().fold(0u, |old, new| num::pow(old, *new));});
+        b.iter(|| {v.iter().fold(0u, |old, new| old.pow(*new));});
     }
 }
diff --git a/src/test/bench/shootout-binarytrees.rs b/src/test/bench/shootout-binarytrees.rs
index a0c69b3736d..e89ae418e2d 100644
--- a/src/test/bench/shootout-binarytrees.rs
+++ b/src/test/bench/shootout-binarytrees.rs
@@ -93,8 +93,8 @@ fn main() {
     let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth);
 
     let mut messages = range_step(min_depth, max_depth + 1, 2).map(|depth| {
-            use std::num::pow;
-            let iterations = pow(2i, (max_depth - depth + min_depth) as uint);
+            use std::num::Int;
+            let iterations = 2i.pow((max_depth - depth + min_depth) as uint);
             Future::spawn(proc() {
                 let mut chk = 0;
                 for i in range(1, iterations + 1) {
diff --git a/src/test/compile-fail/lint-dead-code-4.rs b/src/test/compile-fail/lint-dead-code-4.rs
index 7c3242a6a25..57d62184b93 100644
--- a/src/test/compile-fail/lint-dead-code-4.rs
+++ b/src/test/compile-fail/lint-dead-code-4.rs
@@ -14,7 +14,7 @@
 
 extern crate libc;
 
-use std::num;
+use std::num::Int;
 
 struct Foo {
     x: uint,
@@ -23,7 +23,7 @@ struct Foo {
 }
 
 fn field_read(f: Foo) -> uint {
-    num::pow(f.x, 2)
+    f.x.pow(2)
 }
 
 enum XYZ {