Skip to content

Commit 3fcf284

Browse files
committed
libcore: add num::Int::pow() and deprecate num::pow().
Signed-off-by: NODA, Kai <[email protected]>
1 parent 803aacd commit 3fcf284

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

src/libcore/num/mod.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,10 @@ pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
3737
}
3838

3939
/// Raises a `base` to the power of `exp`, using exponentiation by squaring.
40-
///
41-
/// # Example
42-
///
43-
/// ```rust
44-
/// use std::num;
45-
///
46-
/// assert_eq!(num::pow(2i, 4), 16);
47-
/// ```
4840
#[inline]
49-
pub fn pow<T: Int>(mut base: T, mut exp: uint) -> T {
50-
if exp == 1 { base }
51-
else {
52-
let mut acc: T = Int::one();
53-
while exp > 0 {
54-
if (exp & 1) == 1 {
55-
acc = acc * base;
56-
}
57-
base = base * base;
58-
exp = exp >> 1;
59-
}
60-
acc
61-
}
41+
#[deprecated = "Use Int::pow() instead, as in 2i.pow(4)"]
42+
pub fn pow<T: Int>(base: T, exp: uint) -> T {
43+
base.pow(exp)
6244
}
6345

6446
/// A built-in signed or unsigned integer.
@@ -359,6 +341,29 @@ pub trait Int
359341
None => Int::max_value(),
360342
}
361343
}
344+
345+
/// Raises self to the power of `exp`, using exponentiation by squaring.
346+
///
347+
/// # Example
348+
///
349+
/// ```rust
350+
/// use std::num::Int;
351+
///
352+
/// assert_eq!(2i.pow(4), 16);
353+
/// ```
354+
#[inline]
355+
fn pow(self, mut exp: uint) -> Self {
356+
let mut base = self;
357+
let mut acc: Self = Int::one();
358+
while exp > 0 {
359+
if (exp & 1) == 1 {
360+
acc = acc * base;
361+
}
362+
base = base * base;
363+
exp /= 2;
364+
}
365+
acc
366+
}
362367
}
363368

364369
macro_rules! checked_op {

src/libstd/num/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ mod tests {
757757
}
758758
macro_rules! assert_pow(
759759
(($num:expr, $exp:expr) => $expected:expr) => {{
760-
let result = pow($num, $exp);
760+
let result = $num.pow($exp);
761761
assert_eq!(result, $expected);
762762
assert_eq!(result, naive_pow($num, $exp));
763763
}}
@@ -775,12 +775,12 @@ mod tests {
775775
mod bench {
776776
extern crate test;
777777
use self::test::Bencher;
778-
use num;
778+
use num::Int;
779779
use prelude::*;
780780

781781
#[bench]
782782
fn bench_pow_function(b: &mut Bencher) {
783783
let v = Vec::from_fn(1024u, |n| n);
784-
b.iter(|| {v.iter().fold(0u, |old, new| num::pow(old, *new));});
784+
b.iter(|| {v.iter().fold(0u, |old, new| old.pow(*new));});
785785
}
786786
}

src/test/bench/shootout-binarytrees.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ fn main() {
9393
let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth);
9494

9595
let mut messages = range_step(min_depth, max_depth + 1, 2).map(|depth| {
96-
use std::num::pow;
97-
let iterations = pow(2i, (max_depth - depth + min_depth) as uint);
96+
use std::num::Int;
97+
let iterations = 2i.pow((max_depth - depth + min_depth) as uint);
9898
Future::spawn(proc() {
9999
let mut chk = 0;
100100
for i in range(1, iterations + 1) {

src/test/compile-fail/lint-dead-code-4.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
extern crate libc;
1616

17-
use std::num;
17+
use std::num::Int;
1818

1919
struct Foo {
2020
x: uint,
@@ -23,7 +23,7 @@ struct Foo {
2323
}
2424

2525
fn field_read(f: Foo) -> uint {
26-
num::pow(f.x, 2)
26+
f.x.pow(2)
2727
}
2828

2929
enum XYZ {

0 commit comments

Comments
 (0)