Skip to content

Commit 2bba723

Browse files
committed
auto merge of #12331 : bjz/rust/count-ones, r=alexcrichton
This is inspired by the [function naming in the Julia standard library](http://docs.julialang.org/en/release-0.2/stdlib/base/#Base.count_ones). It seems like a more self-explanatory name, and is more consistent with the accompanying methods, `leading_zeros` and `trailing_zeros`.
2 parents 8802869 + 79f52cf commit 2bba723

File tree

10 files changed

+91
-41
lines changed

10 files changed

+91
-41
lines changed

src/libextra/enum_set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<E:CLike> Iterator<E> for Items<E> {
129129
}
130130

131131
fn size_hint(&self) -> (uint, Option<uint>) {
132-
let exact = self.bits.population_count();
132+
let exact = self.bits.count_ones();
133133
(exact, Some(exact))
134134
}
135135
}

src/librustc/middle/trans/adt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ fn generic_type_of(cx: &CrateContext, r: &Repr, name: Option<&str>, sizing: bool
432432
4 => Type::array(&Type::i32(), align_units),
433433
8 if machine::llalign_of_min(cx, Type::i64()) == 8 =>
434434
Type::array(&Type::i64(), align_units),
435-
a if a.population_count() == 1 => Type::array(&Type::vector(&Type::i32(), a / 4),
435+
a if a.count_ones() == 1 => Type::array(&Type::vector(&Type::i32(), a / 4),
436436
align_units),
437437
_ => fail!("unsupported enum alignment: {:?}", align)
438438
};

src/libstd/num/i16.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ use unstable::intrinsics;
2525
int_module!(i16, 16)
2626

2727
impl Bitwise for i16 {
28-
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
28+
/// Returns the number of ones in the binary representation of the number.
2929
#[inline]
30-
fn population_count(&self) -> i16 { unsafe { intrinsics::ctpop16(*self) } }
30+
fn count_ones(&self) -> i16 { unsafe { intrinsics::ctpop16(*self) } }
3131

32-
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
32+
/// Returns the number of leading zeros in the in the binary representation
33+
/// of the number.
3334
#[inline]
3435
fn leading_zeros(&self) -> i16 { unsafe { intrinsics::ctlz16(*self) } }
3536

36-
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
37+
/// Returns the number of trailing zeros in the in the binary representation
38+
/// of the number.
3739
#[inline]
3840
fn trailing_zeros(&self) -> i16 { unsafe { intrinsics::cttz16(*self) } }
3941
}

src/libstd/num/i32.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ use unstable::intrinsics;
2525
int_module!(i32, 32)
2626

2727
impl Bitwise for i32 {
28-
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
28+
/// Returns the number of ones in the binary representation of the number.
2929
#[inline]
30-
fn population_count(&self) -> i32 { unsafe { intrinsics::ctpop32(*self) } }
30+
fn count_ones(&self) -> i32 { unsafe { intrinsics::ctpop32(*self) } }
3131

32-
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
32+
/// Returns the number of leading zeros in the in the binary representation
33+
/// of the number.
3334
#[inline]
3435
fn leading_zeros(&self) -> i32 { unsafe { intrinsics::ctlz32(*self) } }
3536

36-
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
37+
/// Returns the number of trailing zeros in the in the binary representation
38+
/// of the number.
3739
#[inline]
3840
fn trailing_zeros(&self) -> i32 { unsafe { intrinsics::cttz32(*self) } }
3941
}

src/libstd/num/i64.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ use unstable::intrinsics;
2727
int_module!(i64, 64)
2828

2929
impl Bitwise for i64 {
30-
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
30+
/// Returns the number of ones in the binary representation of the number.
3131
#[inline]
32-
fn population_count(&self) -> i64 { unsafe { intrinsics::ctpop64(*self) } }
32+
fn count_ones(&self) -> i64 { unsafe { intrinsics::ctpop64(*self) } }
3333

34-
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
34+
/// Returns the number of leading zeros in the in the binary representation
35+
/// of the number.
3536
#[inline]
3637
fn leading_zeros(&self) -> i64 { unsafe { intrinsics::ctlz64(*self) } }
3738

38-
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
39+
/// Counts the number of trailing zeros.
3940
#[inline]
4041
fn trailing_zeros(&self) -> i64 { unsafe { intrinsics::cttz64(*self) } }
4142
}

src/libstd/num/i8.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ use unstable::intrinsics;
2525
int_module!(i8, 8)
2626

2727
impl Bitwise for i8 {
28-
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
28+
/// Returns the number of ones in the binary representation of the number.
2929
#[inline]
30-
fn population_count(&self) -> i8 { unsafe { intrinsics::ctpop8(*self) } }
30+
fn count_ones(&self) -> i8 { unsafe { intrinsics::ctpop8(*self) } }
3131

32-
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
32+
/// Returns the number of leading zeros in the in the binary representation
33+
/// of the number.
3334
#[inline]
3435
fn leading_zeros(&self) -> i8 { unsafe { intrinsics::ctlz8(*self) } }
3536

36-
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
37+
/// Returns the number of trailing zeros in the in the binary representation
38+
/// of the number.
3739
#[inline]
3840
fn trailing_zeros(&self) -> i8 { unsafe { intrinsics::cttz8(*self) } }
3941
}

src/libstd/num/int.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,34 @@ use unstable::intrinsics;
2727

2828
#[cfg(target_word_size = "32")]
2929
impl Bitwise for int {
30-
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
30+
/// Returns the number of ones in the binary representation of the number.
3131
#[inline]
32-
fn population_count(&self) -> int { (*self as i32).population_count() as int }
32+
fn count_ones(&self) -> int { (*self as i32).count_ones() as int }
3333

34-
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
34+
/// Returns the number of leading zeros in the in the binary representation
35+
/// of the number.
3536
#[inline]
3637
fn leading_zeros(&self) -> int { (*self as i32).leading_zeros() as int }
3738

38-
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
39+
/// Returns the number of trailing zeros in the in the binary representation
40+
/// of the number.
3941
#[inline]
4042
fn trailing_zeros(&self) -> int { (*self as i32).trailing_zeros() as int }
4143
}
4244

4345
#[cfg(target_word_size = "64")]
4446
impl Bitwise for int {
45-
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
47+
/// Returns the number of ones in the binary representation of the number.
4648
#[inline]
47-
fn population_count(&self) -> int { (*self as i64).population_count() as int }
49+
fn count_ones(&self) -> int { (*self as i64).count_ones() as int }
4850

49-
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
51+
/// Returns the number of leading zeros in the in the binary representation
52+
/// of the number.
5053
#[inline]
5154
fn leading_zeros(&self) -> int { (*self as i64).leading_zeros() as int }
5255

53-
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
56+
/// Returns the number of trailing zeros in the in the binary representation
57+
/// of the number.
5458
#[inline]
5559
fn trailing_zeros(&self) -> int { (*self as i64).trailing_zeros() as int }
5660
}

src/libstd/num/int_macros.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,17 @@ mod tests {
614614
}
615615

616616
#[test]
617-
fn test_bitcount() {
618-
assert_eq!((0b010101 as $T).population_count(), 3);
617+
fn test_count_ones() {
618+
assert_eq!((0b0101100 as $T).count_ones(), 3);
619+
assert_eq!((0b0100001 as $T).count_ones(), 2);
620+
assert_eq!((0b1111001 as $T).count_ones(), 5);
621+
}
622+
623+
#[test]
624+
fn test_count_zeros() {
625+
assert_eq!((0b0101100 as $T).count_zeros(), BITS as $T - 3);
626+
assert_eq!((0b0100001 as $T).count_zeros(), BITS as $T - 2);
627+
assert_eq!((0b1111001 as $T).count_zeros(), BITS as $T - 5);
619628
}
620629

621630
#[test]

src/libstd/num/mod.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,18 +375,35 @@ pub trait Bitwise: Bounded
375375
+ BitXor<Self,Self>
376376
+ Shl<Self,Self>
377377
+ Shr<Self,Self> {
378-
/// Returns the number of bits set in the number.
378+
/// Returns the number of ones in the binary representation of the number.
379379
///
380380
/// # Example
381381
///
382382
/// ```rust
383383
/// use std::num::Bitwise;
384384
///
385-
/// let n = 0b0101000u16;
386-
/// assert_eq!(n.population_count(), 2);
385+
/// let n = 0b01001100u8;
386+
/// assert_eq!(n.count_ones(), 3);
387387
/// ```
388-
fn population_count(&self) -> Self;
389-
/// Returns the number of leading zeros in the number.
388+
fn count_ones(&self) -> Self;
389+
390+
/// Returns the number of zeros in the binary representation of the number.
391+
///
392+
/// # Example
393+
///
394+
/// ```rust
395+
/// use std::num::Bitwise;
396+
///
397+
/// let n = 0b01001100u8;
398+
/// assert_eq!(n.count_zeros(), 5);
399+
/// ```
400+
#[inline]
401+
fn count_zeros(&self) -> Self {
402+
(!*self).count_ones()
403+
}
404+
405+
/// Returns the number of leading zeros in the in the binary representation
406+
/// of the number.
390407
///
391408
/// # Example
392409
///
@@ -397,7 +414,9 @@ pub trait Bitwise: Bounded
397414
/// assert_eq!(n.leading_zeros(), 10);
398415
/// ```
399416
fn leading_zeros(&self) -> Self;
400-
/// Returns the number of trailing zeros in the number.
417+
418+
/// Returns the number of trailing zeros in the in the binary representation
419+
/// of the number.
401420
///
402421
/// # Example
403422
///

src/libstd/num/uint_macros.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,19 +266,21 @@ impl ToStrRadix for $T {
266266
impl Primitive for $T {}
267267

268268
impl Bitwise for $T {
269-
/// Counts the number of bits set. Wraps LLVM's `ctpop` intrinsic.
269+
/// Returns the number of ones in the binary representation of the number.
270270
#[inline]
271-
fn population_count(&self) -> $T {
272-
(*self as $T_SIGNED).population_count() as $T
271+
fn count_ones(&self) -> $T {
272+
(*self as $T_SIGNED).count_ones() as $T
273273
}
274274

275-
/// Counts the number of leading zeros. Wraps LLVM's `ctlz` intrinsic.
275+
/// Returns the number of leading zeros in the in the binary representation
276+
/// of the number.
276277
#[inline]
277278
fn leading_zeros(&self) -> $T {
278279
(*self as $T_SIGNED).leading_zeros() as $T
279280
}
280281

281-
/// Counts the number of trailing zeros. Wraps LLVM's `cttz` intrinsic.
282+
/// Returns the number of trailing zeros in the in the binary representation
283+
/// of the number.
282284
#[inline]
283285
fn trailing_zeros(&self) -> $T {
284286
(*self as $T_SIGNED).trailing_zeros() as $T
@@ -375,8 +377,17 @@ mod tests {
375377
}
376378

377379
#[test]
378-
fn test_bitcount() {
379-
assert_eq!((0b010101 as $T).population_count(), 3);
380+
fn test_count_ones() {
381+
assert_eq!((0b0101100 as $T).count_ones(), 3);
382+
assert_eq!((0b0100001 as $T).count_ones(), 2);
383+
assert_eq!((0b1111001 as $T).count_ones(), 5);
384+
}
385+
386+
#[test]
387+
fn test_count_zeros() {
388+
assert_eq!((0b0101100 as $T).count_zeros(), BITS as $T - 3);
389+
assert_eq!((0b0100001 as $T).count_zeros(), BITS as $T - 2);
390+
assert_eq!((0b1111001 as $T).count_zeros(), BITS as $T - 5);
380391
}
381392

382393
#[test]

0 commit comments

Comments
 (0)