Skip to content

Commit

Permalink
auto merge of #18827 : bjz/rust/rfc369-numerics, r=alexcrichton
Browse files Browse the repository at this point in the history
This implements a considerable portion of rust-lang/rfcs#369 (tracked in #18640). Some interpretations had to be made in order to get this to work. The breaking changes are listed below:

[breaking-change]

- `core::num::{Num, Unsigned, Primitive}` have been deprecated and their re-exports removed from the `{std, core}::prelude`.
- `core::num::{Zero, One, Bounded}` have been deprecated. Use the static methods on `core::num::{Float, Int}` instead. There is no equivalent to `Zero::is_zero`. Use `(==)` with `{Float, Int}::zero` instead.
- `Signed::abs_sub` has been moved to `std::num::FloatMath`, and is no longer implemented for signed integers.
- `core::num::Signed` has been removed, and its methods have been moved to `core::num::Float` and a new trait, `core::num::SignedInt`. The methods now take the `self` parameter by value.
- `core::num::{Saturating, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}` have been removed, and their methods moved to `core::num::Int`. Their parameters are now taken by value. This means that
- `std::time::Duration` no longer implements `core::num::{Zero, CheckedAdd, CheckedSub}` instead defining the required methods non-polymorphically.
- `core::num::{zero, one, abs, signum}` have been deprecated. Use their respective methods instead.
- The `core::num::{next_power_of_two, is_power_of_two, checked_next_power_of_two}` functions have been deprecated in favor of methods defined a new trait, `core::num::UnsignedInt`
- `core::iter::{AdditiveIterator, MultiplicativeIterator}` are now only implemented for the built-in numeric types.
- `core::iter::{range, range_inclusive, range_step, range_step_inclusive}` now require `core::num::Int` to be implemented for the type they a re parametrized over.
  • Loading branch information
bors committed Nov 14, 2014
2 parents 3c3bfa7 + e2e50d7 commit e57d106
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
9 changes: 4 additions & 5 deletions map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -549,7 +549,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
/// ```
#[inline]
pub fn with_capacity_and_hasher(capacity: uint, hasher: H) -> HashMap<K, V, H> {
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),
Expand All @@ -572,8 +572,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
/// 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);

Expand All @@ -588,7 +587,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
/// 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();
Expand Down
10 changes: 5 additions & 5 deletions table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{Int, UnsignedInt};
use ops::{Deref, DerefMut, Drop};
use option::{Some, None, Option};
use ptr::{RawPtr, copy_nonoverlapping_memory, zero_memory};
Expand Down Expand Up @@ -493,7 +493,7 @@ impl<K, V, M: Deref<RawTable<K, V>>> GapThenFull<K, V, M> {
///
/// 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)
}

Expand Down Expand Up @@ -581,9 +581,9 @@ impl<K, V> RawTable<K, V> {
vals_size, min_align_of::< V >());

// One check for overflow that covers calculation and rounding of size.
let size_of_bucket = size_of::<u64>().checked_add(&size_of::<K>()).unwrap()
.checked_add(&size_of::<V>()).unwrap();
assert!(size >= capacity.checked_mul(&size_of_bucket)
let size_of_bucket = size_of::<u64>().checked_add(size_of::<K>()).unwrap()
.checked_add(size_of::<V>()).unwrap();
assert!(size >= capacity.checked_mul(size_of_bucket)
.expect("capacity overflow"),
"capacity overflow");

Expand Down

0 comments on commit e57d106

Please sign in to comment.