Skip to content

Commit fec14cd

Browse files
committed
---
yaml --- r: 159574 b: refs/heads/auto c: d1eb68e h: refs/heads/master v: v3
1 parent 020f7b9 commit fec14cd

File tree

8 files changed

+72
-52
lines changed

8 files changed

+72
-52
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1010
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1111
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1212
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
13-
refs/heads/auto: 9fe94bd995ab13afab7078a708b01f365740d2cd
13+
refs/heads/auto: d1eb68e8d7d2883c70304021d5443c96bca18abb
1414
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1515
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1616
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/libarena/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::cmp;
3838
use std::intrinsics::{TyDesc, get_tydesc};
3939
use std::intrinsics;
4040
use std::mem;
41-
use std::num;
41+
use std::num::UnsignedInt;
4242
use std::ptr;
4343
use std::rc::Rc;
4444
use std::rt::heap::{allocate, deallocate};
@@ -187,7 +187,7 @@ impl Arena {
187187
self.chunks.borrow_mut().push(self.copy_head.borrow().clone());
188188

189189
*self.copy_head.borrow_mut() =
190-
chunk(num::next_power_of_two(new_min_chunk_size + 1u), true);
190+
chunk((new_min_chunk_size + 1u).next_power_of_two(), true);
191191

192192
return self.alloc_copy_inner(n_bytes, align);
193193
}
@@ -228,7 +228,7 @@ impl Arena {
228228
self.chunks.borrow_mut().push(self.head.borrow().clone());
229229

230230
*self.head.borrow_mut() =
231-
chunk(num::next_power_of_two(new_min_chunk_size + 1u), false);
231+
chunk((new_min_chunk_size + 1u).next_power_of_two(), false);
232232

233233
return self.alloc_noncopy_inner(n_bytes, align);
234234
}

branches/auto/src/libcollections/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use core::default::Default;
2121
use core::fmt;
2222
use core::kinds::marker::{ContravariantLifetime, InvariantType};
2323
use core::mem;
24-
use core::num;
24+
use core::num::UnsignedInt;
2525
use core::ops;
2626
use core::ptr;
2727
use core::raw::Slice as RawSlice;
@@ -605,7 +605,7 @@ impl<T> Vec<T> {
605605
None => panic!("Vec::reserve: `uint` overflow"),
606606
// if the checked_add
607607
Some(new_cap) => {
608-
let amort_cap = num::next_power_of_two(new_cap);
608+
let amort_cap = new_cap.next_power_of_two();
609609
// next_power_of_two will overflow to exactly 0 for really big capacities
610610
if amort_cap == 0 {
611611
self.grow_capacity(new_cap);

branches/auto/src/libcore/num/mod.rs

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -621,40 +621,47 @@ int_cast_impl!(i64, u64)
621621
#[cfg(target_word_size = "32")] int_cast_impl!(int, u32)
622622
#[cfg(target_word_size = "64")] int_cast_impl!(int, u64)
623623

624-
/// Returns the smallest power of 2 greater than or equal to `n`.
625-
#[inline]
626-
pub fn next_power_of_two<T: Unsigned + Int>(n: T) -> T {
627-
let halfbits = size_of::<T>() * 4;
628-
let mut tmp: T = n - one();
629-
let mut shift = 1u;
630-
while shift <= halfbits {
631-
tmp = tmp | (tmp >> shift);
632-
shift = shift << 1u;
624+
/// Unsigned integers
625+
pub trait UnsignedInt: Int {
626+
/// Returns `true` iff `self == 2^k` for some `k`.
627+
fn is_power_of_two(self) -> bool {
628+
(self - one()) & self == zero()
633629
}
634-
tmp + one()
635-
}
636630

637-
// Returns `true` iff `n == 2^k` for some k.
638-
#[inline]
639-
pub fn is_power_of_two<T: Unsigned + Int>(n: T) -> bool {
640-
(n - one()) & n == zero()
641-
}
631+
/// Returns the smallest power of two greater than or equal to `self`.
632+
#[inline]
633+
fn next_power_of_two(self) -> Self {
634+
let halfbits = size_of::<Self>() * 4;
635+
let mut tmp = self - one();
636+
let mut shift = 1u;
637+
while shift <= halfbits {
638+
tmp = tmp | (tmp >> shift);
639+
shift = shift << 1u;
640+
}
641+
tmp + one()
642+
}
642643

643-
/// Returns the smallest power of 2 greater than or equal to `n`. If the next
644-
/// power of two is greater than the type's maximum value, `None` is returned,
645-
/// otherwise the power of 2 is wrapped in `Some`.
646-
#[inline]
647-
pub fn checked_next_power_of_two<T: Unsigned + Int>(n: T) -> Option<T> {
648-
let halfbits = size_of::<T>() * 4;
649-
let mut tmp: T = n - one();
650-
let mut shift = 1u;
651-
while shift <= halfbits {
652-
tmp = tmp | (tmp >> shift);
653-
shift = shift << 1u;
644+
/// Returns the smallest power of two greater than or equal to `n`. If the
645+
/// next power of two is greater than the type's maximum value, `None` is
646+
/// returned, otherwise the power of two is wrapped in `Some`.
647+
fn checked_next_power_of_two(self) -> Option<Self> {
648+
let halfbits = size_of::<Self>() * 4;
649+
let mut tmp = self - one();
650+
let mut shift = 1u;
651+
while shift <= halfbits {
652+
tmp = tmp | (tmp >> shift);
653+
shift = shift << 1u;
654+
}
655+
tmp.checked_add(&one())
654656
}
655-
tmp.checked_add(&one())
656657
}
657658

659+
impl UnsignedInt for uint {}
660+
impl UnsignedInt for u8 {}
661+
impl UnsignedInt for u16 {}
662+
impl UnsignedInt for u32 {}
663+
impl UnsignedInt for u64 {}
664+
658665
/// A generic trait for converting a value to a number.
659666
pub trait ToPrimitive {
660667
/// Converts the value of `self` to an `int`.
@@ -1525,4 +1532,18 @@ pub trait Float: Signed + Primitive {
15251532
// DEPRECATED
15261533

15271534
#[deprecated = "Use `Signed::abs`"]
1528-
pub fn abs<T: Signed>(value: T) -> T { value.abs() }
1535+
pub fn abs<T: Signed>(value: T) -> T {
1536+
value.abs()
1537+
}
1538+
#[deprecated = "Use `UnsignedInt::next_power_of_two`"]
1539+
pub fn next_power_of_two<T: UnsignedInt>(n: T) -> T {
1540+
n.next_power_of_two()
1541+
}
1542+
#[deprecated = "Use `UnsignedInt::is_power_of_two`"]
1543+
pub fn is_power_of_two<T: UnsignedInt>(n: T) -> bool {
1544+
n.is_power_of_two()
1545+
}
1546+
#[deprecated = "Use `UnsignedInt::checked_next_power_of_two`"]
1547+
pub fn checked_next_power_of_two<T: UnsignedInt>(n: T) -> Option<T> {
1548+
n.checked_next_power_of_two()
1549+
}

branches/auto/src/libstd/collections/hash/map.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use hash::{Hash, Hasher, RandomSipHasher};
1818
use iter::{mod, Iterator, FromIterator, Extend};
1919
use kinds::Sized;
2020
use mem::{mod, replace};
21-
use num;
21+
use num::UnsignedInt;
2222
use ops::{Deref, Index, IndexMut};
2323
use option::{Some, None, Option};
2424
use result::{Result, Ok, Err};
@@ -549,7 +549,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
549549
/// ```
550550
#[inline]
551551
pub fn with_capacity_and_hasher(capacity: uint, hasher: H) -> HashMap<K, V, H> {
552-
let cap = num::next_power_of_two(max(INITIAL_CAPACITY, capacity));
552+
let cap = max(INITIAL_CAPACITY, capacity).next_power_of_two();
553553
HashMap {
554554
hasher: hasher,
555555
resize_policy: DefaultResizePolicy::new(cap),
@@ -572,8 +572,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
572572
/// map.reserve(10);
573573
/// ```
574574
pub fn reserve(&mut self, new_minimum_capacity: uint) {
575-
let cap = num::next_power_of_two(
576-
max(INITIAL_CAPACITY, new_minimum_capacity));
575+
let cap = max(INITIAL_CAPACITY, new_minimum_capacity).next_power_of_two();
577576

578577
self.resize_policy.reserve(cap);
579578

@@ -588,7 +587,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
588587
/// 2) Ensure new_capacity is a power of two.
589588
fn resize(&mut self, new_capacity: uint) {
590589
assert!(self.table.size() <= new_capacity);
591-
assert!(num::is_power_of_two(new_capacity));
590+
assert!(new_capacity.is_power_of_two());
592591

593592
let mut old_table = replace(&mut self.table, RawTable::new(new_capacity));
594593
let old_size = old_table.size();

branches/auto/src/libstd/collections/hash/table.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use iter::{Iterator, count};
1717
use kinds::{Sized, marker};
1818
use mem::{min_align_of, size_of};
1919
use mem;
20-
use num::{CheckedAdd, CheckedMul, is_power_of_two};
20+
use num::{CheckedAdd, CheckedMul, UnsignedInt};
2121
use ops::{Deref, DerefMut, Drop};
2222
use option::{Some, None, Option};
2323
use ptr::{RawPtr, copy_nonoverlapping_memory, zero_memory};
@@ -516,7 +516,7 @@ impl<K, V, M: Deref<RawTable<K, V>>> GapThenFull<K, V, M> {
516516
///
517517
/// Fails if `target_alignment` is not a power of two.
518518
fn round_up_to_next(unrounded: uint, target_alignment: uint) -> uint {
519-
assert!(is_power_of_two(target_alignment));
519+
assert!(target_alignment.is_power_of_two());
520520
(unrounded + target_alignment - 1) & !(target_alignment - 1)
521521
}
522522

branches/auto/src/libstd/num/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use option::Option;
2323
pub use core::num::{Num, div_rem, Zero, zero, One, one};
2424
pub use core::num::{Signed, abs, signum};
2525
pub use core::num::{Unsigned, pow, Bounded};
26-
pub use core::num::{Primitive, Int, Saturating};
26+
pub use core::num::{Primitive, Int, UnsignedInt, Saturating};
2727
pub use core::num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
2828
pub use core::num::{cast, FromPrimitive, NumCast, ToPrimitive};
2929
pub use core::num::{next_power_of_two, is_power_of_two};
@@ -672,10 +672,10 @@ mod tests {
672672
($test_name:ident, $T:ident) => (
673673
fn $test_name() {
674674
#![test]
675-
assert_eq!(next_power_of_two::<$T>(0), 0);
675+
assert_eq!((0 as $T).next_power_of_two(), 0);
676676
let mut next_power = 1;
677677
for i in range::<$T>(1, 40) {
678-
assert_eq!(next_power_of_two(i), next_power);
678+
assert_eq!(i.next_power_of_two(), next_power);
679679
if i == next_power { next_power *= 2 }
680680
}
681681
}
@@ -692,15 +692,15 @@ mod tests {
692692
($test_name:ident, $T:ident) => (
693693
fn $test_name() {
694694
#![test]
695-
assert_eq!(checked_next_power_of_two::<$T>(0), None);
695+
assert_eq!((0 as $T).checked_next_power_of_two(), None);
696696
let mut next_power = 1;
697697
for i in range::<$T>(1, 40) {
698-
assert_eq!(checked_next_power_of_two(i), Some(next_power));
698+
assert_eq!(i.checked_next_power_of_two(), Some(next_power));
699699
if i == next_power { next_power *= 2 }
700700
}
701-
assert!(checked_next_power_of_two::<$T>($T::MAX / 2).is_some());
702-
assert_eq!(checked_next_power_of_two::<$T>($T::MAX - 1), None);
703-
assert_eq!(checked_next_power_of_two::<$T>($T::MAX), None);
701+
assert!(($T::MAX / 2).checked_next_power_of_two().is_some());
702+
assert_eq!(($T::MAX - 1).checked_next_power_of_two(), None);
703+
assert_eq!($T::MAX.checked_next_power_of_two(), None);
704704
}
705705
)
706706
)

branches/auto/src/libsync/mpmc_bounded_queue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use core::prelude::*;
3434

3535
use alloc::arc::Arc;
3636
use collections::Vec;
37-
use core::num::next_power_of_two;
37+
use core::num::UnsignedInt;
3838
use core::cell::UnsafeCell;
3939

4040
use atomic::{AtomicUint,Relaxed,Release,Acquire};
@@ -66,7 +66,7 @@ impl<T: Send> State<T> {
6666
2u
6767
} else {
6868
// use next power of 2 as capacity
69-
next_power_of_two(capacity)
69+
capacity.next_power_of_two()
7070
}
7171
} else {
7272
capacity

0 commit comments

Comments
 (0)