From 1ef5e388b9026841804eaf2bd885b98a7e405e8b Mon Sep 17 00:00:00 2001 From: Felix Raimundo Date: Tue, 14 Oct 2014 01:24:06 +0200 Subject: [PATCH] Switches from uint to u32 in BitV and BitVSet Closes #16736 linked to #18009 --- src/libcollections/bitv.rs | 209 +++++++++++++++++++------------------ 1 file changed, 105 insertions(+), 104 deletions(-) diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs index 8f9873d77d17e..a2c848db614bb 100644 --- a/src/libcollections/bitv.rs +++ b/src/libcollections/bitv.rs @@ -69,13 +69,13 @@ use core::fmt; use core::iter::{Chain, Enumerate, Repeat, Skip, Take}; use core::iter; use core::slice; -use core::uint; +use core::u32; use std::hash; use {Mutable, Set, MutableSet, MutableSeq}; use vec::Vec; -type MatchWords<'a> = Chain, Skip>>>>; +type MatchWords<'a> = Chain, Skip>>>>; // Take two BitV's, and return iterators of their words, where the shorter one // has been padded with 0's fn match_words <'a,'b>(a: &'a Bitv, b: &'b Bitv) -> (MatchWords<'a>, MatchWords<'b>) { @@ -84,11 +84,11 @@ fn match_words <'a,'b>(a: &'a Bitv, b: &'b Bitv) -> (MatchWords<'a>, MatchWords< // have to uselessly pretend to pad the longer one for type matching if a_len < b_len { - (a.mask_words(0).chain(Repeat::new(0u).enumerate().take(b_len).skip(a_len)), - b.mask_words(0).chain(Repeat::new(0u).enumerate().take(0).skip(0))) + (a.mask_words(0).chain(Repeat::new(0u32).enumerate().take(b_len).skip(a_len)), + b.mask_words(0).chain(Repeat::new(0u32).enumerate().take(0).skip(0))) } else { - (a.mask_words(0).chain(Repeat::new(0u).enumerate().take(0).skip(0)), - b.mask_words(0).chain(Repeat::new(0u).enumerate().take(a_len).skip(b_len))) + (a.mask_words(0).chain(Repeat::new(0u32).enumerate().take(0).skip(0)), + b.mask_words(0).chain(Repeat::new(0u32).enumerate().take(a_len).skip(b_len))) } } @@ -124,7 +124,7 @@ static FALSE: bool = false; /// ``` pub struct Bitv { /// Internal representation of the bit vector - storage: Vec, + storage: Vec, /// The number of valid bits in the internal representation nbits: uint } @@ -141,16 +141,16 @@ impl Index for Bitv { } struct MaskWords<'a> { - iter: slice::Items<'a, uint>, - next_word: Option<&'a uint>, - last_word_mask: uint, + iter: slice::Items<'a, u32>, + next_word: Option<&'a u32>, + last_word_mask: u32, offset: uint } -impl<'a> Iterator<(uint, uint)> for MaskWords<'a> { +impl<'a> Iterator<(uint, u32)> for MaskWords<'a> { /// Returns (offset, word) #[inline] - fn next<'a>(&'a mut self) -> Option<(uint, uint)> { + fn next<'a>(&'a mut self) -> Option<(uint, u32)> { let ret = self.next_word; match ret { Some(&w) => { @@ -170,7 +170,7 @@ impl<'a> Iterator<(uint, uint)> for MaskWords<'a> { impl Bitv { #[inline] - fn process(&mut self, other: &Bitv, op: |uint, uint| -> uint) -> bool { + fn process(&mut self, other: &Bitv, op: |u32, u32| -> u32) -> bool { let len = other.storage.len(); assert_eq!(self.storage.len(), len); let mut changed = false; @@ -199,7 +199,7 @@ impl Bitv { next_word: iter.next(), iter: iter, last_word_mask: { - let rem = self.nbits % uint::BITS; + let rem = self.nbits % u32::BITS; if rem > 0 { (1 << rem) - 1 } else { !0 } @@ -236,15 +236,15 @@ impl Bitv { /// ``` pub fn with_capacity(nbits: uint, init: bool) -> Bitv { let mut bitv = Bitv { - storage: Vec::from_elem((nbits + uint::BITS - 1) / uint::BITS, - if init { !0u } else { 0u }), + storage: Vec::from_elem((nbits + u32::BITS - 1) / u32::BITS, + if init { !0u32 } else { 0u32 }), nbits: nbits }; // Zero out any unused bits in the highest word if necessary - let used_bits = bitv.nbits % uint::BITS; + let used_bits = bitv.nbits % u32::BITS; if init && used_bits != 0 { - let largest_used_word = (bitv.nbits + uint::BITS - 1) / uint::BITS - 1; + let largest_used_word = (bitv.nbits + u32::BITS - 1) / u32::BITS - 1; *bitv.storage.get_mut(largest_used_word) &= (1 << used_bits) - 1; } @@ -272,8 +272,8 @@ impl Bitv { #[inline] pub fn get(&self, i: uint) -> bool { assert!(i < self.nbits); - let w = i / uint::BITS; - let b = i % uint::BITS; + let w = i / u32::BITS; + let b = i % u32::BITS; let x = self.storage[w] & (1 << b); x != 0 } @@ -296,8 +296,8 @@ impl Bitv { #[inline] pub fn set(&mut self, i: uint, x: bool) { assert!(i < self.nbits); - let w = i / uint::BITS; - let b = i % uint::BITS; + let w = i / u32::BITS; + let b = i % u32::BITS; let flag = 1 << b; *self.storage.get_mut(w) = if x { self.storage[w] | flag } else { self.storage[w] & !flag }; @@ -319,7 +319,7 @@ impl Bitv { /// ``` #[inline] pub fn set_all(&mut self) { - for w in self.storage.iter_mut() { *w = !0u; } + for w in self.storage.iter_mut() { *w = !0u32; } } /// Flips all bits. @@ -453,12 +453,12 @@ impl Bitv { /// ``` #[inline] pub fn all(&self) -> bool { - let mut last_word = !0u; + let mut last_word = !0u32; // Check that every word but the last is all-ones... self.mask_words(0).all(|(_, elem)| - { let tmp = last_word; last_word = elem; tmp == !0u }) && + { let tmp = last_word; last_word = elem; tmp == !0u32 }) && // ...and that the last word is ones as far as it needs to be - (last_word == ((1 << self.nbits % uint::BITS) - 1) || last_word == !0u) + (last_word == ((1 << self.nbits % u32::BITS) - 1) || last_word == !0u32) } /// Returns an iterator over the elements of the vector in order. @@ -615,10 +615,10 @@ impl Bitv { pub fn truncate(&mut self, len: uint) { if len < self.len() { self.nbits = len; - let word_len = (len + uint::BITS - 1) / uint::BITS; + let word_len = (len + u32::BITS - 1) / u32::BITS; self.storage.truncate(word_len); - if len % uint::BITS > 0 { - let mask = (1 << len % uint::BITS) - 1; + if len % u32::BITS > 0 { + let mask = (1 << len % u32::BITS) - 1; *self.storage.get_mut(word_len - 1) &= mask; } } @@ -638,7 +638,7 @@ impl Bitv { /// ``` pub fn reserve(&mut self, size: uint) { let old_size = self.storage.len(); - let new_size = (size + uint::BITS - 1) / uint::BITS; + let new_size = (size + u32::BITS - 1) / u32::BITS; if old_size < new_size { self.storage.grow(new_size - old_size, 0); } @@ -658,7 +658,7 @@ impl Bitv { /// ``` #[inline] pub fn capacity(&self) -> uint { - self.storage.len() * uint::BITS + self.storage.len() * u32::BITS } /// Grows the `Bitv` in-place, adding `n` copies of `value` to the `Bitv`. @@ -675,12 +675,12 @@ impl Bitv { /// ``` pub fn grow(&mut self, n: uint, value: bool) { let new_nbits = self.nbits + n; - let new_nwords = (new_nbits + uint::BITS - 1) / uint::BITS; + let new_nwords = (new_nbits + u32::BITS - 1) / u32::BITS; let full_value = if value { !0 } else { 0 }; // Correct the old tail word - let old_last_word = (self.nbits + uint::BITS - 1) / uint::BITS - 1; - if self.nbits % uint::BITS > 0 { - let overhang = self.nbits % uint::BITS; // # of already-used bits + let old_last_word = (self.nbits + u32::BITS - 1) / u32::BITS - 1; + if self.nbits % u32::BITS > 0 { + let overhang = self.nbits % u32::BITS; // # of already-used bits let mask = !((1 << overhang) - 1); // e.g. 5 unused bits => 111110....0 if value { *self.storage.get_mut(old_last_word) |= mask; @@ -701,7 +701,7 @@ impl Bitv { // Zero out and unused bits in the new tail word if value { let tail_word = new_nwords - 1; - let used_bits = new_nbits % uint::BITS; + let used_bits = new_nbits % u32::BITS; *self.storage.get_mut(tail_word) &= (1 << used_bits) - 1; } } @@ -729,8 +729,8 @@ impl Bitv { pub fn pop(&mut self) -> bool { let ret = self.get(self.nbits - 1); // If we are unusing a whole word, make sure it is zeroed out - if self.nbits % uint::BITS == 1 { - *self.storage.get_mut(self.nbits / uint::BITS) = 0; + if self.nbits % u32::BITS == 1 { + *self.storage.get_mut(self.nbits / u32::BITS) = 0; } self.nbits -= 1; ret @@ -751,7 +751,7 @@ impl Bitv { pub fn push(&mut self, elem: bool) { let insert_pos = self.nbits; self.nbits += 1; - if self.storage.len() * uint::BITS < self.nbits { + if self.storage.len() * u32::BITS < self.nbits { self.storage.push(0); } self.set(insert_pos, elem); @@ -775,7 +775,7 @@ impl Bitv { /// ``` pub fn from_bytes(bytes: &[u8]) -> Bitv { from_fn(bytes.len() * 8, |i| { - let b = bytes[i / 8] as uint; + let b = bytes[i / 8] as u32; let offset = i % 8; b >> (7 - offset) & 1 == 1 }) @@ -813,7 +813,7 @@ impl Collection for Bitv { impl Mutable for Bitv { #[inline] fn clear(&mut self) { - for w in self.storage.iter_mut() { *w = 0u; } + for w in self.storage.iter_mut() { *w = 0u32; } } } @@ -1168,7 +1168,7 @@ impl BitvSet { } #[inline] - fn other_op(&mut self, other: &BitvSet, f: |uint, uint| -> uint) { + fn other_op(&mut self, other: &BitvSet, f: |u32, u32| -> u32) { // Expand the vector if necessary self.reserve(other.capacity()); @@ -1218,10 +1218,10 @@ impl BitvSet { // Truncate let trunc_len = cmp::max(old_len - n, 1); bitv.storage.truncate(trunc_len); - bitv.nbits = trunc_len * uint::BITS; + bitv.nbits = trunc_len * u32::BITS; } - /// Iterator over each uint stored in the `BitvSet`. + /// Iterator over each u32 stored in the `BitvSet`. /// /// # Example /// @@ -1238,10 +1238,10 @@ impl BitvSet { /// ``` #[inline] pub fn iter<'a>(&'a self) -> BitPositions<'a> { - BitPositions {set: self, next_idx: 0} + BitPositions {set: self, next_idx: 0u} } - /// Iterator over each uint stored in `self` union `other`. + /// Iterator over each u32 stored in `self` union `other`. /// See [union_with](#method.union_with) for an efficient in-place version. /// /// # Example @@ -1264,8 +1264,8 @@ impl BitvSet { set: self, other: other, merge: |w1, w2| w1 | w2, - current_word: 0, - next_idx: 0 + current_word: 0u32, + next_idx: 0u } } @@ -1293,7 +1293,7 @@ impl BitvSet { set: self, other: other, merge: |w1, w2| w1 & w2, - current_word: 0, + current_word: 0u32, next_idx: 0 }.take(min) } @@ -1328,12 +1328,12 @@ impl BitvSet { set: self, other: other, merge: |w1, w2| w1 & !w2, - current_word: 0, + current_word: 0u32, next_idx: 0 } } - /// Iterator over each uint stored in the symmetric difference of `self` and `other`. + /// Iterator over each u32 stored in the symmetric difference of `self` and `other`. /// See [symmetric_difference_with](#method.symmetric_difference_with) for /// an efficient in-place version. /// @@ -1357,7 +1357,7 @@ impl BitvSet { set: self, other: other, merge: |w1, w2| w1 ^ w2, - current_word: 0, + current_word: 0u32, next_idx: 0 } } @@ -1575,8 +1575,8 @@ pub struct BitPositions<'a> { pub struct TwoBitPositions<'a> { set: &'a BitvSet, other: &'a BitvSet, - merge: |uint, uint|: 'a -> uint, - current_word: uint, + merge: |u32, u32|: 'a -> u32, + current_word: u32, next_idx: uint } @@ -1604,13 +1604,13 @@ impl<'a> Iterator for TwoBitPositions<'a> { fn next(&mut self) -> Option { while self.next_idx < self.set.capacity() || self.next_idx < self.other.capacity() { - let bit_idx = self.next_idx % uint::BITS; + let bit_idx = self.next_idx % u32::BITS; if bit_idx == 0 { let &BitvSet(ref s_bitv) = self.set; let &BitvSet(ref o_bitv) = self.other; // Merging the two words is a bit of an awkward dance since // one Bitv might be longer than the other - let word_idx = self.next_idx / uint::BITS; + let word_idx = self.next_idx / u32::BITS; let w1 = if word_idx < s_bitv.storage.len() { s_bitv.storage[word_idx] } else { 0 }; @@ -1639,6 +1639,7 @@ impl<'a> Iterator for TwoBitPositions<'a> { mod tests { use std::prelude::*; use std::iter::range_step; + use std::u32; use std::uint; use std::rand; use std::rand::Rng; @@ -2128,9 +2129,9 @@ mod tests { #[test] fn test_bitv_set_basic() { - // calculate nbits with uint::BITS granularity + // calculate nbits with u32::BITS granularity fn calc_nbits(bits: uint) -> uint { - uint::BITS * ((bits + uint::BITS - 1) / uint::BITS) + u32::BITS * ((bits + u32::BITS - 1) / u32::BITS) } let mut b = BitvSet::new(); @@ -2426,7 +2427,7 @@ mod tests { assert!(a.insert(1000)); assert!(a.remove(&1000)); a.shrink_to_fit(); - assert_eq!(a.capacity(), uint::BITS); + assert_eq!(a.capacity(), u32::BITS); } #[test] @@ -2526,43 +2527,43 @@ mod tests { #[test] fn test_bitv_push_pop() { - let mut s = Bitv::with_capacity(5 * uint::BITS - 2, false); - assert_eq!(s.len(), 5 * uint::BITS - 2); - assert_eq!(s.get(5 * uint::BITS - 3), false); + let mut s = Bitv::with_capacity(5 * u32::BITS - 2, false); + assert_eq!(s.len(), 5 * u32::BITS - 2); + assert_eq!(s.get(5 * u32::BITS - 3), false); s.push(true); s.push(true); - assert_eq!(s.get(5 * uint::BITS - 2), true); - assert_eq!(s.get(5 * uint::BITS - 1), true); + assert_eq!(s.get(5 * u32::BITS - 2), true); + assert_eq!(s.get(5 * u32::BITS - 1), true); // Here the internal vector will need to be extended s.push(false); - assert_eq!(s.get(5 * uint::BITS), false); + assert_eq!(s.get(5 * u32::BITS), false); s.push(false); - assert_eq!(s.get(5 * uint::BITS + 1), false); - assert_eq!(s.len(), 5 * uint::BITS + 2); + assert_eq!(s.get(5 * u32::BITS + 1), false); + assert_eq!(s.len(), 5 * u32::BITS + 2); // Pop it all off assert_eq!(s.pop(), false); assert_eq!(s.pop(), false); assert_eq!(s.pop(), true); assert_eq!(s.pop(), true); - assert_eq!(s.len(), 5 * uint::BITS - 2); + assert_eq!(s.len(), 5 * u32::BITS - 2); } #[test] fn test_bitv_truncate() { - let mut s = Bitv::with_capacity(5 * uint::BITS, true); + let mut s = Bitv::with_capacity(5 * u32::BITS, true); - assert_eq!(s, Bitv::with_capacity(5 * uint::BITS, true)); - assert_eq!(s.len(), 5 * uint::BITS); - s.truncate(4 * uint::BITS); - assert_eq!(s, Bitv::with_capacity(4 * uint::BITS, true)); - assert_eq!(s.len(), 4 * uint::BITS); + assert_eq!(s, Bitv::with_capacity(5 * u32::BITS, true)); + assert_eq!(s.len(), 5 * u32::BITS); + s.truncate(4 * u32::BITS); + assert_eq!(s, Bitv::with_capacity(4 * u32::BITS, true)); + assert_eq!(s.len(), 4 * u32::BITS); // Truncating to a size > s.len() should be a noop - s.truncate(5 * uint::BITS); - assert_eq!(s, Bitv::with_capacity(4 * uint::BITS, true)); - assert_eq!(s.len(), 4 * uint::BITS); - s.truncate(3 * uint::BITS - 10); - assert_eq!(s, Bitv::with_capacity(3 * uint::BITS - 10, true)); - assert_eq!(s.len(), 3 * uint::BITS - 10); + s.truncate(5 * u32::BITS); + assert_eq!(s, Bitv::with_capacity(4 * u32::BITS, true)); + assert_eq!(s.len(), 4 * u32::BITS); + s.truncate(3 * u32::BITS - 10); + assert_eq!(s, Bitv::with_capacity(3 * u32::BITS - 10, true)); + assert_eq!(s.len(), 3 * u32::BITS - 10); s.truncate(0); assert_eq!(s, Bitv::with_capacity(0, true)); assert_eq!(s.len(), 0); @@ -2570,26 +2571,26 @@ mod tests { #[test] fn test_bitv_reserve() { - let mut s = Bitv::with_capacity(5 * uint::BITS, true); + let mut s = Bitv::with_capacity(5 * u32::BITS, true); // Check capacity - assert_eq!(s.capacity(), 5 * uint::BITS); - s.reserve(2 * uint::BITS); - assert_eq!(s.capacity(), 5 * uint::BITS); - s.reserve(7 * uint::BITS); - assert_eq!(s.capacity(), 7 * uint::BITS); - s.reserve(7 * uint::BITS); - assert_eq!(s.capacity(), 7 * uint::BITS); - s.reserve(7 * uint::BITS + 1); - assert_eq!(s.capacity(), 8 * uint::BITS); + assert_eq!(s.capacity(), 5 * u32::BITS); + s.reserve(2 * u32::BITS); + assert_eq!(s.capacity(), 5 * u32::BITS); + s.reserve(7 * u32::BITS); + assert_eq!(s.capacity(), 7 * u32::BITS); + s.reserve(7 * u32::BITS); + assert_eq!(s.capacity(), 7 * u32::BITS); + s.reserve(7 * u32::BITS + 1); + assert_eq!(s.capacity(), 8 * u32::BITS); // Check that length hasn't changed - assert_eq!(s.len(), 5 * uint::BITS); + assert_eq!(s.len(), 5 * u32::BITS); s.push(true); s.push(false); s.push(true); - assert_eq!(s.get(5 * uint::BITS - 1), true); - assert_eq!(s.get(5 * uint::BITS - 0), true); - assert_eq!(s.get(5 * uint::BITS + 1), false); - assert_eq!(s.get(5 * uint::BITS + 2), true); + assert_eq!(s.get(5 * u32::BITS - 1), true); + assert_eq!(s.get(5 * u32::BITS - 0), true); + assert_eq!(s.get(5 * u32::BITS + 1), false); + assert_eq!(s.get(5 * u32::BITS + 2), true); } #[test] @@ -2636,7 +2637,7 @@ mod tests { let mut bitv = 0 as uint; b.iter(|| { for _ in range(0u, 100) { - bitv |= 1 << ((r.next_u32() as uint) % uint::BITS); + bitv |= 1 << ((r.next_u32() as uint) % u32::BITS); } &bitv }) @@ -2669,10 +2670,10 @@ mod tests { #[bench] fn bench_bitv_set_small(b: &mut Bencher) { let mut r = rng(); - let mut bitv = Bitv::with_capacity(uint::BITS, false); + let mut bitv = Bitv::with_capacity(u32::BITS, false); b.iter(|| { for _ in range(0u, 100) { - bitv.set((r.next_u32() as uint) % uint::BITS, true); + bitv.set((r.next_u32() as uint) % u32::BITS, true); } &bitv }) @@ -2684,7 +2685,7 @@ mod tests { let mut bitv = BitvSet::new(); b.iter(|| { for _ in range(0u, 100) { - bitv.insert((r.next_u32() as uint) % uint::BITS); + bitv.insert((r.next_u32() as uint) % u32::BITS); } &bitv }) @@ -2713,9 +2714,9 @@ mod tests { #[bench] fn bench_bitv_small_iter(b: &mut Bencher) { - let bitv = Bitv::with_capacity(uint::BITS, false); + let bitv = Bitv::with_capacity(u32::BITS, false); b.iter(|| { - let mut sum = 0; + let mut sum = 0u; for _ in range(0u, 10) { for pres in bitv.iter() { sum += pres as uint; @@ -2729,7 +2730,7 @@ mod tests { fn bench_bitv_big_iter(b: &mut Bencher) { let bitv = Bitv::with_capacity(BENCH_BITS, false); b.iter(|| { - let mut sum = 0; + let mut sum = 0u; for pres in bitv.iter() { sum += pres as uint; } @@ -2742,9 +2743,9 @@ mod tests { let bitv = BitvSet::from_bitv(from_fn(BENCH_BITS, |idx| {idx % 3 == 0})); b.iter(|| { - let mut sum = 0; + let mut sum = 0u; for idx in bitv.iter() { - sum += idx; + sum += idx as uint; } sum })