Skip to content

Commit

Permalink
Mul references not clones
Browse files Browse the repository at this point in the history
  • Loading branch information
dten committed Jan 9, 2016
1 parent 838b4e4 commit 912191a
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,23 @@ pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
/// assert_eq!(num::pow(6u8, 3), 216);
/// ```
#[inline]
pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: usize) -> T {
pub fn pow<T: One + Clone>(mut base: T, mut exp: usize) -> T
where for <'a> &'a T: Mul<&'a T,Output = T>, {

if exp == 0 { return T::one() }

while exp & 1 == 0 {
base = base.clone() * base;
base = &base * &base;
exp >>= 1;
}
if exp == 1 { return base }

let mut acc = base.clone();
while exp > 1 {
exp >>= 1;
base = base.clone() * base;
base = &base * &base;
if exp & 1 == 1 {
acc = acc * base.clone();
acc = &acc * &base;
}
}
acc
Expand Down

0 comments on commit 912191a

Please sign in to comment.