-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathpower.rs
More file actions
119 lines (108 loc) · 3.48 KB
/
power.rs
File metadata and controls
119 lines (108 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use traits::{Zero, checked_pow};
use Integer;
pub trait Power: Integer {
/// Returns whether `self` is a power of `other`.
///
/// # Examples
///
/// ~~~
/// use num_integer::Power;
/// assert_eq!(100u32.is_power_of(&10), true);
/// assert_eq!(4u32.is_power_of(&10), false);
/// ~~~
#[inline]
fn is_power_of(&self, other: &Self) -> bool {
self.checked_next_power_of(other).map_or(false, |new| self == &new)
}
/// Returns the truncated base-`other` logarithm of `self`.
/// Panics if `self.is_zero()`.
///
/// # Examples
///
/// ~~~
/// use num_integer::Power;
/// assert_eq!(100u32.log(&10), 2);
/// assert_eq!(4u32.log(&10), 0);
/// ~~~
#[inline]
fn log(&self, other: &Self) -> u32 {
self.checked_log(other).expect("log: `self` is zero")
}
/// Returns the truncated base-`other` logarithm of `self`, or `None` if `self.is_zero()`.
///
/// # Examples
///
/// ~~~
/// use num_integer::Power;
/// assert_eq!(100u32.checked_log(&10), Some(2));
/// assert_eq!(4u32.checked_log(&10), Some(0));
/// assert_eq!(0u32.checked_log(&10), None);
/// ~~~
fn checked_log(&self, other: &Self) -> Option<u32>;
/// Returns the least power of `other` not less than `self`, or `0` if the value is out of
/// bounds of the type.
///
/// # Examples
///
/// ~~~
/// use num_integer::Power;
/// assert_eq!(100u32.wrapping_next_power_of(&10), 100);
/// assert_eq!(4u32.wrapping_next_power_of(&10), 10);
/// assert_eq!(200u8.wrapping_next_power_of(&10), 0);
/// ~~~
#[inline]
fn wrapping_next_power_of(&self, other: &Self) -> Self {
self.checked_next_power_of(other).unwrap_or(Self::zero())
}
/// Returns the least power of `other` not less than `self`, or `None` if the value is out
/// of bounds of the type.
///
/// # Examples
///
/// ~~~
/// use num_integer::Power;
/// assert_eq!(100u32.checked_next_power_of(&10), Some(100));
/// assert_eq!(4u32.checked_next_power_of(&10), Some(10));
/// assert_eq!(200u8.checked_next_power_of(&10), None);
/// ~~~
fn checked_next_power_of(&self, other: &Self) -> Option<Self>;
/// Returns the least power of `other` not less than `self`.
/// Panics if the value is out of bounds of the type.
///
/// # Examples
///
/// ~~~
/// use num_integer::Power;
/// assert_eq!(100u32.next_power_of(&10), 100);
/// assert_eq!(4u32.next_power_of(&10), 10);
/// ~~~
#[inline]
fn next_power_of(&self, other: &Self) -> Self {
self.checked_next_power_of(other).expect("checked_next_power_of: out of range")
}
}
macro_rules! impl_Power {
($t:ty) => (impl Power for $t {
#[inline]
fn checked_log(&self, other: &Self) -> Option<u32> {
let mut n = self.clone();
let mut a = None;
while !n.is_zero() {
a = Some(a.map_or(0, |a| a+1));
n /= *other;
}
a
}
#[inline]
fn checked_next_power_of(&self, other: &Self) -> Option<Self> {
self.checked_sub(1)
.map_or(Some(1), |new|
checked_pow(*other, new.checked_log(other)
.map_or(0, |a| a as Self + 1) as usize))
}
});
($($t:ty),*) => { $(impl_Power!($t);)* };
}
impl_Power!(usize, u8, u16, u32, u64);
#[cfg(has_i128)]
impl_Power!(u128);