Change Shl<T, T> for Int to Shl<uint, T>
This commit is contained in:
parent
d623a8bf3c
commit
7b7d23cc3d
@ -400,8 +400,8 @@ pub trait Int: Primitive
|
|||||||
+ BitAnd<Self,Self>
|
+ BitAnd<Self,Self>
|
||||||
+ BitOr<Self,Self>
|
+ BitOr<Self,Self>
|
||||||
+ BitXor<Self,Self>
|
+ BitXor<Self,Self>
|
||||||
+ Shl<Self,Self>
|
+ Shl<uint,Self>
|
||||||
+ Shr<Self,Self> {
|
+ Shr<uint,Self> {
|
||||||
/// Returns the number of ones in the binary representation of the integer.
|
/// Returns the number of ones in the binary representation of the integer.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
@ -667,12 +667,12 @@ int_cast_impl!(i64, u64)
|
|||||||
/// Returns the smallest power of 2 greater than or equal to `n`.
|
/// Returns the smallest power of 2 greater than or equal to `n`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn next_power_of_two<T: Unsigned + Int>(n: T) -> T {
|
pub fn next_power_of_two<T: Unsigned + Int>(n: T) -> T {
|
||||||
let halfbits: T = cast(size_of::<T>() * 4).unwrap();
|
let halfbits = size_of::<T>() * 4;
|
||||||
let mut tmp: T = n - one();
|
let mut tmp: T = n - one();
|
||||||
let mut shift: T = one();
|
let mut shift = 1u;
|
||||||
while shift <= halfbits {
|
while shift <= halfbits {
|
||||||
tmp = tmp | (tmp >> shift);
|
tmp = tmp | (tmp >> shift);
|
||||||
shift = shift << one();
|
shift = shift << 1u;
|
||||||
}
|
}
|
||||||
tmp + one()
|
tmp + one()
|
||||||
}
|
}
|
||||||
@ -688,12 +688,12 @@ pub fn is_power_of_two<T: Unsigned + Int>(n: T) -> bool {
|
|||||||
/// otherwise the power of 2 is wrapped in `Some`.
|
/// otherwise the power of 2 is wrapped in `Some`.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn checked_next_power_of_two<T: Unsigned + Int>(n: T) -> Option<T> {
|
pub fn checked_next_power_of_two<T: Unsigned + Int>(n: T) -> Option<T> {
|
||||||
let halfbits: T = cast(size_of::<T>() * 4).unwrap();
|
let halfbits = size_of::<T>() * 4;
|
||||||
let mut tmp: T = n - one();
|
let mut tmp: T = n - one();
|
||||||
let mut shift: T = one();
|
let mut shift = 1u;
|
||||||
while shift <= halfbits {
|
while shift <= halfbits {
|
||||||
tmp = tmp | (tmp >> shift);
|
tmp = tmp | (tmp >> shift);
|
||||||
shift = shift << one();
|
shift = shift << 1u;
|
||||||
}
|
}
|
||||||
tmp.checked_add(&one())
|
tmp.checked_add(&one())
|
||||||
}
|
}
|
||||||
|
@ -558,10 +558,10 @@ pub trait Shl<RHS,Result> {
|
|||||||
|
|
||||||
macro_rules! shl_impl(
|
macro_rules! shl_impl(
|
||||||
($($t:ty)*) => ($(
|
($($t:ty)*) => ($(
|
||||||
impl Shl<$t, $t> for $t {
|
impl Shl<uint, $t> for $t {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn shl(&self, other: &$t) -> $t {
|
fn shl(&self, other: &uint) -> $t {
|
||||||
(*self) << (*other as uint)
|
(*self) << (*other)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)*)
|
)*)
|
||||||
@ -601,9 +601,9 @@ pub trait Shr<RHS,Result> {
|
|||||||
|
|
||||||
macro_rules! shr_impl(
|
macro_rules! shr_impl(
|
||||||
($($t:ty)*) => ($(
|
($($t:ty)*) => ($(
|
||||||
impl Shr<$t, $t> for $t {
|
impl Shr<uint, $t> for $t {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn shr(&self, other: &$t) -> $t { (*self) >> (*other as uint) }
|
fn shr(&self, other: &uint) -> $t { (*self) >> (*other) }
|
||||||
}
|
}
|
||||||
)*)
|
)*)
|
||||||
)
|
)
|
||||||
|
@ -74,8 +74,8 @@ mod tests {
|
|||||||
assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
|
assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
|
||||||
assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
|
assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
|
||||||
assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
|
assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
|
||||||
assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
|
assert!(0b1110 as $T == (0b0111 as $T).shl(&1));
|
||||||
assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
|
assert!(0b0111 as $T == (0b1110 as $T).shr(&1));
|
||||||
assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not());
|
assert!(-(0b11 as $T) - (1 as $T) == (0b11 as $T).not());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,8 +34,8 @@ mod tests {
|
|||||||
assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
|
assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
|
||||||
assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
|
assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
|
||||||
assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
|
assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
|
||||||
assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
|
assert!(0b1110 as $T == (0b0111 as $T).shl(&1u));
|
||||||
assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
|
assert!(0b0111 as $T == (0b1110 as $T).shr(&1u));
|
||||||
assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
|
assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user