]> git.lizzy.rs Git - rust.git/commitdiff
Make Int::pow() take exp as u32 instead usize
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Fri, 27 Feb 2015 20:04:15 +0000 (21:04 +0100)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Sun, 1 Mar 2015 00:58:55 +0000 (01:58 +0100)
src/libcore/num/mod.rs
src/libcoretest/num/int_macros.rs
src/libstd/num/mod.rs
src/test/bench/shootout-binarytrees.rs

index b1039f79f23de1989343683d2d4cc99cd528a906..318799f59a8101f48a41e9f02d6aa89f6ddc88ff 100644 (file)
@@ -372,9 +372,10 @@ fn saturating_sub(self, other: Self) -> Self {
     #[unstable(feature = "core",
                reason = "pending integer conventions")]
     #[inline]
-    fn pow(self, mut exp: uint) -> Self {
+    fn pow(self, mut exp: u32) -> Self {
         let mut base = self;
         let mut acc: Self = Int::one();
+
         while exp > 0 {
             if (exp & 1) == 1 {
                 acc = acc * base;
index f5657d939b2afccb1258032a88a37251f1809316..c33729876ccf3c5777fdaed6d4f51cde5c14039c 100644 (file)
@@ -201,6 +201,17 @@ fn test_from_str_radix() {
         assert_eq!(FromStrRadix::from_str_radix("Z", 35).ok(), None::<$T>);
         assert_eq!(FromStrRadix::from_str_radix("-9", 2).ok(), None::<$T>);
     }
+
+    #[test]
+    fn test_pow() {
+        let mut r = 2 as $T;
+
+        assert_eq!(r.pow(2u32), 4 as $T);
+        assert_eq!(r.pow(0u32), 1 as $T);
+        r = -2 as $T;
+        assert_eq!(r.pow(2u32), 4 as $T);
+        assert_eq!(r.pow(3u32), -8 as $T);
+    }
 }
 
 )}
index 15ae8b027e128a20451ebc5c8a408ae008f5b42e..d776079efaeeccd00b8cda8e8a180ba26bf3145c 100644 (file)
@@ -1830,6 +1830,6 @@ mod bench {
     #[bench]
     fn bench_pow_function(b: &mut Bencher) {
         let v = (0..1024).collect::<Vec<_>>();
-        b.iter(|| {v.iter().fold(0, |old, new| old.pow(*new));});
+        b.iter(|| {v.iter().fold(0, |old, new| old.pow(*new as u32));});
     }
 }
index 1e23da3020f277b7262145bab28a0c3216e010a5..38fc53ccd366a104896a9bc5dcd313a66aa215c7 100644 (file)
@@ -109,7 +109,7 @@ fn main() {
 
     let messages = range_step(min_depth, max_depth + 1, 2).map(|depth| {
         use std::num::Int;
-        let iterations = 2.pow((max_depth - depth + min_depth) as usize);
+        let iterations = 2.pow((max_depth - depth + min_depth) as u32);
         thread::scoped(move || inner(depth, iterations))
     }).collect::<Vec<_>>();