From d1eb68e8d7d2883c70304021d5443c96bca18abb Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 9 Nov 2014 17:15:45 +1100 Subject: Create UnsignedInt trait and deprecate free functions --- src/libstd/collections/hash/map.rs | 9 ++++----- src/libstd/collections/hash/table.rs | 4 ++-- src/libstd/num/mod.rs | 16 ++++++++-------- 3 files changed, 14 insertions(+), 15 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index f55ce9ba462..68c428f456d 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -18,7 +18,7 @@ use hash::{Hash, Hasher, RandomSipHasher}; use iter::{mod, Iterator, FromIterator, Extend}; use kinds::Sized; use mem::{mod, replace}; -use num; +use num::UnsignedInt; use ops::{Deref, Index, IndexMut}; use option::{Some, None, Option}; use result::{Result, Ok, Err}; @@ -549,7 +549,7 @@ impl, V, S, H: Hasher> HashMap { /// ``` #[inline] pub fn with_capacity_and_hasher(capacity: uint, hasher: H) -> HashMap { - let cap = num::next_power_of_two(max(INITIAL_CAPACITY, capacity)); + let cap = max(INITIAL_CAPACITY, capacity).next_power_of_two(); HashMap { hasher: hasher, resize_policy: DefaultResizePolicy::new(cap), @@ -572,8 +572,7 @@ impl, V, S, H: Hasher> HashMap { /// map.reserve(10); /// ``` pub fn reserve(&mut self, new_minimum_capacity: uint) { - let cap = num::next_power_of_two( - max(INITIAL_CAPACITY, new_minimum_capacity)); + let cap = max(INITIAL_CAPACITY, new_minimum_capacity).next_power_of_two(); self.resize_policy.reserve(cap); @@ -588,7 +587,7 @@ impl, V, S, H: Hasher> HashMap { /// 2) Ensure new_capacity is a power of two. fn resize(&mut self, new_capacity: uint) { assert!(self.table.size() <= new_capacity); - assert!(num::is_power_of_two(new_capacity)); + assert!(new_capacity.is_power_of_two()); let mut old_table = replace(&mut self.table, RawTable::new(new_capacity)); let old_size = old_table.size(); diff --git a/src/libstd/collections/hash/table.rs b/src/libstd/collections/hash/table.rs index fd964cdf02c..b39b15b3eb9 100644 --- a/src/libstd/collections/hash/table.rs +++ b/src/libstd/collections/hash/table.rs @@ -17,7 +17,7 @@ use iter::{Iterator, count}; use kinds::{Sized, marker}; use mem::{min_align_of, size_of}; use mem; -use num::{CheckedAdd, CheckedMul, is_power_of_two}; +use num::{CheckedAdd, CheckedMul, UnsignedInt}; use ops::{Deref, DerefMut, Drop}; use option::{Some, None, Option}; use ptr::{RawPtr, copy_nonoverlapping_memory, zero_memory}; @@ -516,7 +516,7 @@ impl>> GapThenFull { /// /// Fails if `target_alignment` is not a power of two. fn round_up_to_next(unrounded: uint, target_alignment: uint) -> uint { - assert!(is_power_of_two(target_alignment)); + assert!(target_alignment.is_power_of_two()); (unrounded + target_alignment - 1) & !(target_alignment - 1) } diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index 3f8504f4553..509de4787d7 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -23,7 +23,7 @@ use option::Option; pub use core::num::{Num, div_rem, Zero, zero, One, one}; pub use core::num::{Signed, abs, signum}; pub use core::num::{Unsigned, pow, Bounded}; -pub use core::num::{Primitive, Int, Saturating}; +pub use core::num::{Primitive, Int, UnsignedInt, Saturating}; pub use core::num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}; pub use core::num::{cast, FromPrimitive, NumCast, ToPrimitive}; pub use core::num::{next_power_of_two, is_power_of_two}; @@ -672,10 +672,10 @@ mod tests { ($test_name:ident, $T:ident) => ( fn $test_name() { #![test] - assert_eq!(next_power_of_two::<$T>(0), 0); + assert_eq!((0 as $T).next_power_of_two(), 0); let mut next_power = 1; for i in range::<$T>(1, 40) { - assert_eq!(next_power_of_two(i), next_power); + assert_eq!(i.next_power_of_two(), next_power); if i == next_power { next_power *= 2 } } } @@ -692,15 +692,15 @@ mod tests { ($test_name:ident, $T:ident) => ( fn $test_name() { #![test] - assert_eq!(checked_next_power_of_two::<$T>(0), None); + assert_eq!((0 as $T).checked_next_power_of_two(), None); let mut next_power = 1; for i in range::<$T>(1, 40) { - assert_eq!(checked_next_power_of_two(i), Some(next_power)); + assert_eq!(i.checked_next_power_of_two(), Some(next_power)); if i == next_power { next_power *= 2 } } - assert!(checked_next_power_of_two::<$T>($T::MAX / 2).is_some()); - assert_eq!(checked_next_power_of_two::<$T>($T::MAX - 1), None); - assert_eq!(checked_next_power_of_two::<$T>($T::MAX), None); + assert!(($T::MAX / 2).checked_next_power_of_two().is_some()); + assert_eq!(($T::MAX - 1).checked_next_power_of_two(), None); + assert_eq!($T::MAX.checked_next_power_of_two(), None); } ) ) -- cgit 1.4.1-3-g733a5