Skip to content

Commit

Permalink
Auto merge of #22600 - brson:num, r=Gankro
Browse files Browse the repository at this point in the history
* count_ones/zeros, trailing_ones/zeros return u32, not usize
* rotate_left/right take u32, not usize
* RADIX, MANTISSA_DIGITS, DIGITS, BITS, BYTES are u32, not usize

Doesn't touch pow because there's another PR for it.

cc #22240

r? @gankro
  • Loading branch information
bors committed Mar 3, 2015
2 parents b4c965e + 76e9fa6 commit 5457eab
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 155 deletions.
118 changes: 59 additions & 59 deletions src/libcollections/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,17 @@ fn blocks_for_bits(bits: usize) -> usize {
//
// Note that we can technically avoid this branch with the expression
// `(nbits + u32::BITS - 1) / 32::BITS`, but if nbits is almost usize::MAX this will overflow.
if bits % u32::BITS == 0 {
bits / u32::BITS
if bits % u32::BITS as usize == 0 {
bits / u32::BITS as usize
} else {
bits / u32::BITS + 1
bits / u32::BITS as usize + 1
}
}

/// Computes the bitmask for the final word of the vector
fn mask_for_bits(bits: usize) -> u32 {
// Note especially that a perfect multiple of u32::BITS should mask all 1s.
!0u32 >> (u32::BITS - bits % u32::BITS) % u32::BITS
!0u32 >> (u32::BITS as usize - bits % u32::BITS as usize) % u32::BITS as usize
}

impl BitVec {
Expand Down Expand Up @@ -237,7 +237,7 @@ impl BitVec {
/// An operation might screw up the unused bits in the last block of the
/// `BitVec`. As per (3), it's assumed to be all 0s. This method fixes it up.
fn fix_last_block(&mut self) {
let extra_bits = self.len() % u32::BITS;
let extra_bits = self.len() % u32::BITS as usize;
if extra_bits > 0 {
let mask = (1 << extra_bits) - 1;
let storage_len = self.storage.len();
Expand Down Expand Up @@ -313,7 +313,7 @@ impl BitVec {
/// false, false, true, false]));
/// ```
pub fn from_bytes(bytes: &[u8]) -> BitVec {
let len = bytes.len().checked_mul(u8::BITS).expect("capacity overflow");
let len = bytes.len().checked_mul(u8::BITS as usize).expect("capacity overflow");
let mut bit_vec = BitVec::with_capacity(len);
let complete_words = bytes.len() / 4;
let extra_bytes = bytes.len() % 4;
Expand Down Expand Up @@ -380,8 +380,8 @@ impl BitVec {
if i >= self.nbits {
return None;
}
let w = i / u32::BITS;
let b = i % u32::BITS;
let w = i / u32::BITS as usize;
let b = i % u32::BITS as usize;
self.storage.get(w).map(|&block|
(block & (1 << b)) != 0
)
Expand All @@ -407,8 +407,8 @@ impl BitVec {
reason = "panic semantics are likely to change in the future")]
pub fn set(&mut self, i: usize, x: bool) {
assert!(i < self.nbits);
let w = i / u32::BITS;
let b = i % u32::BITS;
let w = i / u32::BITS as usize;
let b = i % u32::BITS as usize;
let flag = 1 << b;
let val = if x { self.storage[w] | flag }
else { self.storage[w] & !flag };
Expand Down Expand Up @@ -789,7 +789,7 @@ impl BitVec {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn capacity(&self) -> usize {
self.storage.capacity().checked_mul(u32::BITS).unwrap_or(usize::MAX)
self.storage.capacity().checked_mul(u32::BITS as usize).unwrap_or(usize::MAX)
}

/// Grows the `BitVec` in-place, adding `n` copies of `value` to the `BitVec`.
Expand Down Expand Up @@ -819,7 +819,7 @@ impl BitVec {

// Correct the old tail word, setting or clearing formerly unused bits
let old_last_word = blocks_for_bits(self.nbits) - 1;
if self.nbits % u32::BITS > 0 {
if self.nbits % u32::BITS as usize > 0 {
let mask = mask_for_bits(self.nbits);
if value {
self.storage[old_last_word] |= !mask;
Expand Down Expand Up @@ -868,7 +868,7 @@ impl BitVec {
// (3)
self.set(i, false);
self.nbits = i;
if self.nbits % u32::BITS == 0 {
if self.nbits % u32::BITS as usize == 0 {
// (2)
self.storage.pop();
}
Expand All @@ -890,7 +890,7 @@ impl BitVec {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push(&mut self, elem: bool) {
if self.nbits % u32::BITS == 0 {
if self.nbits % u32::BITS as usize == 0 {
self.storage.push(0);
}
let insert_pos = self.nbits;
Expand Down Expand Up @@ -1406,7 +1406,7 @@ impl BitSet {
// Truncate
let trunc_len = cmp::max(old_len - n, 1);
bit_vec.storage.truncate(trunc_len);
bit_vec.nbits = trunc_len * u32::BITS;
bit_vec.nbits = trunc_len * u32::BITS as usize;
}

/// Iterator over each u32 stored in the `BitSet`.
Expand Down Expand Up @@ -1663,7 +1663,7 @@ impl BitSet {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn len(&self) -> usize {
self.bit_vec.blocks().fold(0, |acc, n| acc + n.count_ones())
self.bit_vec.blocks().fold(0, |acc, n| acc + n.count_ones() as usize)
}

/// Returns whether there are no bits set in this set
Expand Down Expand Up @@ -1831,13 +1831,13 @@ impl<'a> Iterator for TwoBitPositions<'a> {
fn next(&mut self) -> Option<usize> {
while self.next_idx < self.set.bit_vec.len() ||
self.next_idx < self.other.bit_vec.len() {
let bit_idx = self.next_idx % u32::BITS;
let bit_idx = self.next_idx % u32::BITS as usize;
if bit_idx == 0 {
let s_bit_vec = &self.set.bit_vec;
let o_bit_vec = &self.other.bit_vec;
// Merging the two words is a bit of an awkward dance since
// one BitVec might be longer than the other
let word_idx = self.next_idx / u32::BITS;
let word_idx = self.next_idx / u32::BITS as usize;
let w1 = if word_idx < s_bit_vec.storage.len() {
s_bit_vec.storage[word_idx]
} else { 0 };
Expand Down Expand Up @@ -2441,70 +2441,70 @@ mod tests {

#[test]
fn test_bit_vec_push_pop() {
let mut s = BitVec::from_elem(5 * u32::BITS - 2, false);
assert_eq!(s.len(), 5 * u32::BITS - 2);
assert_eq!(s[5 * u32::BITS - 3], false);
let mut s = BitVec::from_elem(5 * u32::BITS as usize - 2, false);
assert_eq!(s.len(), 5 * u32::BITS as usize - 2);
assert_eq!(s[5 * u32::BITS as usize - 3], false);
s.push(true);
s.push(true);
assert_eq!(s[5 * u32::BITS - 2], true);
assert_eq!(s[5 * u32::BITS - 1], true);
assert_eq!(s[5 * u32::BITS as usize - 2], true);
assert_eq!(s[5 * u32::BITS as usize - 1], true);
// Here the internal vector will need to be extended
s.push(false);
assert_eq!(s[5 * u32::BITS], false);
assert_eq!(s[5 * u32::BITS as usize], false);
s.push(false);
assert_eq!(s[5 * u32::BITS + 1], false);
assert_eq!(s.len(), 5 * u32::BITS + 2);
assert_eq!(s[5 * u32::BITS as usize + 1], false);
assert_eq!(s.len(), 5 * u32::BITS as usize + 2);
// Pop it all off
assert_eq!(s.pop(), Some(false));
assert_eq!(s.pop(), Some(false));
assert_eq!(s.pop(), Some(true));
assert_eq!(s.pop(), Some(true));
assert_eq!(s.len(), 5 * u32::BITS - 2);
assert_eq!(s.len(), 5 * u32::BITS as usize - 2);
}

#[test]
fn test_bit_vec_truncate() {
let mut s = BitVec::from_elem(5 * u32::BITS, true);
let mut s = BitVec::from_elem(5 * u32::BITS as usize, true);

assert_eq!(s, BitVec::from_elem(5 * u32::BITS, true));
assert_eq!(s.len(), 5 * u32::BITS);
s.truncate(4 * u32::BITS);
assert_eq!(s, BitVec::from_elem(4 * u32::BITS, true));
assert_eq!(s.len(), 4 * u32::BITS);
assert_eq!(s, BitVec::from_elem(5 * u32::BITS as usize, true));
assert_eq!(s.len(), 5 * u32::BITS as usize);
s.truncate(4 * u32::BITS as usize);
assert_eq!(s, BitVec::from_elem(4 * u32::BITS as usize, true));
assert_eq!(s.len(), 4 * u32::BITS as usize);
// Truncating to a size > s.len() should be a noop
s.truncate(5 * u32::BITS);
assert_eq!(s, BitVec::from_elem(4 * u32::BITS, true));
assert_eq!(s.len(), 4 * u32::BITS);
s.truncate(3 * u32::BITS - 10);
assert_eq!(s, BitVec::from_elem(3 * u32::BITS - 10, true));
assert_eq!(s.len(), 3 * u32::BITS - 10);
s.truncate(5 * u32::BITS as usize);
assert_eq!(s, BitVec::from_elem(4 * u32::BITS as usize, true));
assert_eq!(s.len(), 4 * u32::BITS as usize);
s.truncate(3 * u32::BITS as usize - 10);
assert_eq!(s, BitVec::from_elem(3 * u32::BITS as usize - 10, true));
assert_eq!(s.len(), 3 * u32::BITS as usize - 10);
s.truncate(0);
assert_eq!(s, BitVec::from_elem(0, true));
assert_eq!(s.len(), 0);
}

#[test]
fn test_bit_vec_reserve() {
let mut s = BitVec::from_elem(5 * u32::BITS, true);
let mut s = BitVec::from_elem(5 * u32::BITS as usize, true);
// Check capacity
assert!(s.capacity() >= 5 * u32::BITS);
s.reserve(2 * u32::BITS);
assert!(s.capacity() >= 7 * u32::BITS);
s.reserve(7 * u32::BITS);
assert!(s.capacity() >= 12 * u32::BITS);
s.reserve_exact(7 * u32::BITS);
assert!(s.capacity() >= 12 * u32::BITS);
s.reserve(7 * u32::BITS + 1);
assert!(s.capacity() >= 12 * u32::BITS + 1);
assert!(s.capacity() >= 5 * u32::BITS as usize);
s.reserve(2 * u32::BITS as usize);
assert!(s.capacity() >= 7 * u32::BITS as usize);
s.reserve(7 * u32::BITS as usize);
assert!(s.capacity() >= 12 * u32::BITS as usize);
s.reserve_exact(7 * u32::BITS as usize);
assert!(s.capacity() >= 12 * u32::BITS as usize);
s.reserve(7 * u32::BITS as usize + 1);
assert!(s.capacity() >= 12 * u32::BITS as usize + 1);
// Check that length hasn't changed
assert_eq!(s.len(), 5 * u32::BITS);
assert_eq!(s.len(), 5 * u32::BITS as usize);
s.push(true);
s.push(false);
s.push(true);
assert_eq!(s[5 * u32::BITS - 1], true);
assert_eq!(s[5 * u32::BITS - 0], true);
assert_eq!(s[5 * u32::BITS + 1], false);
assert_eq!(s[5 * u32::BITS + 2], true);
assert_eq!(s[5 * u32::BITS as usize - 1], true);
assert_eq!(s[5 * u32::BITS as usize - 0], true);
assert_eq!(s[5 * u32::BITS as usize + 1], false);
assert_eq!(s[5 * u32::BITS as usize + 2], true);
}

#[test]
Expand Down Expand Up @@ -2557,7 +2557,7 @@ mod bit_vec_bench {
let mut bit_vec = 0 as usize;
b.iter(|| {
for _ in 0..100 {
bit_vec |= 1 << ((r.next_u32() as usize) % u32::BITS);
bit_vec |= 1 << ((r.next_u32() as usize) % u32::BITS as usize);
}
black_box(&bit_vec);
});
Expand Down Expand Up @@ -2590,10 +2590,10 @@ mod bit_vec_bench {
#[bench]
fn bench_bit_set_small(b: &mut Bencher) {
let mut r = rng();
let mut bit_vec = BitVec::from_elem(u32::BITS, false);
let mut bit_vec = BitVec::from_elem(u32::BITS as usize, false);
b.iter(|| {
for _ in 0..100 {
bit_vec.set((r.next_u32() as usize) % u32::BITS, true);
bit_vec.set((r.next_u32() as usize) % u32::BITS as usize, true);
}
black_box(&bit_vec);
});
Expand All @@ -2610,7 +2610,7 @@ mod bit_vec_bench {

#[bench]
fn bench_bit_vec_small_iter(b: &mut Bencher) {
let bit_vec = BitVec::from_elem(u32::BITS, false);
let bit_vec = BitVec::from_elem(u32::BITS as usize, false);
b.iter(|| {
let mut sum = 0;
for _ in 0..10 {
Expand Down Expand Up @@ -3052,7 +3052,7 @@ mod bit_set_bench {
let mut bit_vec = BitSet::new();
b.iter(|| {
for _ in 0..100 {
bit_vec.insert((r.next_u32() as usize) % u32::BITS);
bit_vec.insert((r.next_u32() as usize) % u32::BITS as usize);
}
black_box(&bit_vec);
});
Expand Down
6 changes: 3 additions & 3 deletions src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub trait CLike {
fn bit<E:CLike>(e: &E) -> usize {
use core::usize;
let value = e.to_usize();
assert!(value < usize::BITS,
assert!(value < usize::BITS as usize,
"EnumSet only supports up to {} variants.", usize::BITS - 1);
1 << value
}
Expand All @@ -95,7 +95,7 @@ impl<E:CLike> EnumSet<E> {
#[unstable(feature = "collections",
reason = "matches collection reform specification, waiting for dust to settle")]
pub fn len(&self) -> usize {
self.bits.count_ones()
self.bits.count_ones() as usize
}

/// Returns true if the `EnumSet` is empty.
Expand Down Expand Up @@ -250,7 +250,7 @@ impl<E:CLike> Iterator for Iter<E> {
}

fn size_hint(&self) -> (usize, Option<usize>) {
let exact = self.bits.count_ones();
let exact = self.bits.count_ones() as usize;
(exact, Some(exact))
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
deccum = deccum / radix_gen;
deccum = deccum.trunc();

let c = char::from_digit(current_digit.to_int().unwrap() as u32, radix);
let c = char::from_digit(current_digit.to_isize().unwrap() as u32, radix);
buf[end] = c.unwrap() as u8;
end += 1;

Expand Down Expand Up @@ -211,7 +211,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
// See note in first loop.
let current_digit = deccum.trunc().abs();

let c = char::from_digit(current_digit.to_int().unwrap() as u32,
let c = char::from_digit(current_digit.to_isize().unwrap() as u32,
radix);
buf[end] = c.unwrap() as u8;
end += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ mod impls {
}

fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
let newlen = data.len() * ::$ty::BYTES;
let newlen = data.len() * ::$ty::BYTES as usize;
let ptr = data.as_ptr() as *const u8;
state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
}
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2467,15 +2467,15 @@ impl<A: Int + ToPrimitive> Iterator for Range<A> {
Some(a) => {
let sz = self.stop.to_i64().map(|b| b.checked_sub(a));
match sz {
Some(Some(bound)) => bound.to_uint(),
Some(Some(bound)) => bound.to_usize(),
_ => None,
}
},
None => match self.state.to_u64() {
Some(a) => {
let sz = self.stop.to_u64().map(|b| b.checked_sub(a));
match sz {
Some(Some(bound)) => bound.to_uint(),
Some(Some(bound)) => bound.to_usize(),
_ => None
}
},
Expand Down Expand Up @@ -2741,7 +2741,7 @@ impl<A: Int> Iterator for ::ops::Range<A> {
if self.start >= self.end {
(0, Some(0))
} else {
let length = (self.end - self.start).to_uint();
let length = (self.end - self.start).to_usize();
(length.unwrap_or(0), length)
}
}
Expand Down
Loading

0 comments on commit 5457eab

Please sign in to comment.