From 79f52cf9ba20c36860bee8f775498a0880189ac1 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 17 Feb 2014 10:12:10 +1100 Subject: Rename Bitwise::population_count to Bitwise::count_ones and add Bitwise::count_zeros These are inspired by the [functions in the Julia standard library](http://docs.julialang.org/en/release-0.2/stdlib/base/#Base.count_ones). --- src/libstd/num/i16.rs | 10 ++++++---- src/libstd/num/i32.rs | 10 ++++++---- src/libstd/num/i64.rs | 9 +++++---- src/libstd/num/i8.rs | 10 ++++++---- src/libstd/num/int.rs | 20 ++++++++++++-------- src/libstd/num/int_macros.rs | 13 +++++++++++-- src/libstd/num/mod.rs | 31 +++++++++++++++++++++++++------ src/libstd/num/uint_macros.rs | 25 ++++++++++++++++++------- 8 files changed, 89 insertions(+), 39 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/num/i16.rs b/src/libstd/num/i16.rs index e19e7c6c3b8..cbeff5d4aa2 100644 --- a/src/libstd/num/i16.rs +++ b/src/libstd/num/i16.rs @@ -25,15 +25,17 @@ use unstable::intrinsics; int_module!(i16, 16) impl Bitwise for i16 { - /// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic. + /// Returns the number of ones in the binary representation of the number. #[inline] - fn population_count(&self) -> i16 { unsafe { intrinsics::ctpop16(*self) } } + fn count_ones(&self) -> i16 { unsafe { intrinsics::ctpop16(*self) } } - /// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic. + /// Returns the number of leading zeros in the in the binary representation + /// of the number. #[inline] fn leading_zeros(&self) -> i16 { unsafe { intrinsics::ctlz16(*self) } } - /// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic. + /// Returns the number of trailing zeros in the in the binary representation + /// of the number. #[inline] fn trailing_zeros(&self) -> i16 { unsafe { intrinsics::cttz16(*self) } } } diff --git a/src/libstd/num/i32.rs b/src/libstd/num/i32.rs index 372d9f680d7..9afc1a14545 100644 --- a/src/libstd/num/i32.rs +++ b/src/libstd/num/i32.rs @@ -25,15 +25,17 @@ use unstable::intrinsics; int_module!(i32, 32) impl Bitwise for i32 { - /// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic. + /// Returns the number of ones in the binary representation of the number. #[inline] - fn population_count(&self) -> i32 { unsafe { intrinsics::ctpop32(*self) } } + fn count_ones(&self) -> i32 { unsafe { intrinsics::ctpop32(*self) } } - /// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic. + /// Returns the number of leading zeros in the in the binary representation + /// of the number. #[inline] fn leading_zeros(&self) -> i32 { unsafe { intrinsics::ctlz32(*self) } } - /// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic. + /// Returns the number of trailing zeros in the in the binary representation + /// of the number. #[inline] fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self) } } } diff --git a/src/libstd/num/i64.rs b/src/libstd/num/i64.rs index 3af082210b9..f1e9f5a4fdc 100644 --- a/src/libstd/num/i64.rs +++ b/src/libstd/num/i64.rs @@ -27,15 +27,16 @@ use unstable::intrinsics; int_module!(i64, 64) impl Bitwise for i64 { - /// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic. + /// Returns the number of ones in the binary representation of the number. #[inline] - fn population_count(&self) -> i64 { unsafe { intrinsics::ctpop64(*self) } } + fn count_ones(&self) -> i64 { unsafe { intrinsics::ctpop64(*self) } } - /// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic. + /// Returns the number of leading zeros in the in the binary representation + /// of the number. #[inline] fn leading_zeros(&self) -> i64 { unsafe { intrinsics::ctlz64(*self) } } - /// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic. + /// Counts the number of trailing zeros. #[inline] fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self) } } } diff --git a/src/libstd/num/i8.rs b/src/libstd/num/i8.rs index 66162ce3502..e0e549b731a 100644 --- a/src/libstd/num/i8.rs +++ b/src/libstd/num/i8.rs @@ -25,15 +25,17 @@ use unstable::intrinsics; int_module!(i8, 8) impl Bitwise for i8 { - /// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic. + /// Returns the number of ones in the binary representation of the number. #[inline] - fn population_count(&self) -> i8 { unsafe { intrinsics::ctpop8(*self) } } + fn count_ones(&self) -> i8 { unsafe { intrinsics::ctpop8(*self) } } - /// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic. + /// Returns the number of leading zeros in the in the binary representation + /// of the number. #[inline] fn leading_zeros(&self) -> i8 { unsafe { intrinsics::ctlz8(*self) } } - /// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic. + /// Returns the number of trailing zeros in the in the binary representation + /// of the number. #[inline] fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self) } } } diff --git a/src/libstd/num/int.rs b/src/libstd/num/int.rs index f336afe12f4..d525639045e 100644 --- a/src/libstd/num/int.rs +++ b/src/libstd/num/int.rs @@ -27,30 +27,34 @@ use unstable::intrinsics; #[cfg(target_word_size = "32")] impl Bitwise for int { - /// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic. + /// Returns the number of ones in the binary representation of the number. #[inline] - fn population_count(&self) -> int { (*self as i32).population_count() as int } + fn count_ones(&self) -> int { (*self as i32).count_ones() as int } - /// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic. + /// Returns the number of leading zeros in the in the binary representation + /// of the number. #[inline] fn leading_zeros(&self) -> int { (*self as i32).leading_zeros() as int } - /// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic. + /// Returns the number of trailing zeros in the in the binary representation + /// of the number. #[inline] fn trailing_zeros(&self) -> int { (*self as i32).trailing_zeros() as int } } #[cfg(target_word_size = "64")] impl Bitwise for int { - /// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic. + /// Returns the number of ones in the binary representation of the number. #[inline] - fn population_count(&self) -> int { (*self as i64).population_count() as int } + fn count_ones(&self) -> int { (*self as i64).count_ones() as int } - /// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic. + /// Returns the number of leading zeros in the in the binary representation + /// of the number. #[inline] fn leading_zeros(&self) -> int { (*self as i64).leading_zeros() as int } - /// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic. + /// Returns the number of trailing zeros in the in the binary representation + /// of the number. #[inline] fn trailing_zeros(&self) -> int { (*self as i64).trailing_zeros() as int } } diff --git a/src/libstd/num/int_macros.rs b/src/libstd/num/int_macros.rs index fe3c18d1c4f..3ecc6f32017 100644 --- a/src/libstd/num/int_macros.rs +++ b/src/libstd/num/int_macros.rs @@ -614,8 +614,17 @@ mod tests { } #[test] - fn test_bitcount() { - assert_eq!((0b010101 as $T).population_count(), 3); + fn test_count_ones() { + assert_eq!((0b0101100 as $T).count_ones(), 3); + assert_eq!((0b0100001 as $T).count_ones(), 2); + assert_eq!((0b1111001 as $T).count_ones(), 5); + } + + #[test] + fn test_count_zeros() { + assert_eq!((0b0101100 as $T).count_zeros(), BITS as $T - 3); + assert_eq!((0b0100001 as $T).count_zeros(), BITS as $T - 2); + assert_eq!((0b1111001 as $T).count_zeros(), BITS as $T - 5); } #[test] diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index 493069139ef..33690a5fddb 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -375,18 +375,35 @@ pub trait Bitwise: Bounded + BitXor + Shl + Shr { - /// Returns the number of bits set in the number. + /// Returns the number of ones in the binary representation of the number. /// /// # Example /// /// ```rust /// use std::num::Bitwise; /// - /// let n = 0b0101000u16; - /// assert_eq!(n.population_count(), 2); + /// let n = 0b01001100u8; + /// assert_eq!(n.count_ones(), 3); /// ``` - fn population_count(&self) -> Self; - /// Returns the number of leading zeros in the number. + fn count_ones(&self) -> Self; + + /// Returns the number of zeros in the binary representation of the number. + /// + /// # Example + /// + /// ```rust + /// use std::num::Bitwise; + /// + /// let n = 0b01001100u8; + /// assert_eq!(n.count_zeros(), 5); + /// ``` + #[inline] + fn count_zeros(&self) -> Self { + (!*self).count_ones() + } + + /// Returns the number of leading zeros in the in the binary representation + /// of the number. /// /// # Example /// @@ -397,7 +414,9 @@ pub trait Bitwise: Bounded /// assert_eq!(n.leading_zeros(), 10); /// ``` fn leading_zeros(&self) -> Self; - /// Returns the number of trailing zeros in the number. + + /// Returns the number of trailing zeros in the in the binary representation + /// of the number. /// /// # Example /// diff --git a/src/libstd/num/uint_macros.rs b/src/libstd/num/uint_macros.rs index bbf1c497c2b..4fc30b43895 100644 --- a/src/libstd/num/uint_macros.rs +++ b/src/libstd/num/uint_macros.rs @@ -266,19 +266,21 @@ impl ToStrRadix for $T { impl Primitive for $T {} impl Bitwise for $T { - /// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic. + /// Returns the number of ones in the binary representation of the number. #[inline] - fn population_count(&self) -> $T { - (*self as $T_SIGNED).population_count() as $T + fn count_ones(&self) -> $T { + (*self as $T_SIGNED).count_ones() as $T } - /// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic. + /// Returns the number of leading zeros in the in the binary representation + /// of the number. #[inline] fn leading_zeros(&self) -> $T { (*self as $T_SIGNED).leading_zeros() as $T } - /// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic. + /// Returns the number of trailing zeros in the in the binary representation + /// of the number. #[inline] fn trailing_zeros(&self) -> $T { (*self as $T_SIGNED).trailing_zeros() as $T @@ -375,8 +377,17 @@ mod tests { } #[test] - fn test_bitcount() { - assert_eq!((0b010101 as $T).population_count(), 3); + fn test_count_ones() { + assert_eq!((0b0101100 as $T).count_ones(), 3); + assert_eq!((0b0100001 as $T).count_ones(), 2); + assert_eq!((0b1111001 as $T).count_ones(), 5); + } + + #[test] + fn test_count_zeros() { + assert_eq!((0b0101100 as $T).count_zeros(), BITS as $T - 3); + assert_eq!((0b0100001 as $T).count_zeros(), BITS as $T - 2); + assert_eq!((0b1111001 as $T).count_zeros(), BITS as $T - 5); } #[test] -- cgit 1.4.1-3-g733a5