core: Remove old template files

This commit is contained in:
Brian Anderson 2012-11-28 15:24:39 -08:00
parent 16f72df704
commit 9b95d51131
27 changed files with 308 additions and 590 deletions

View File

@ -46,52 +46,52 @@ Implicitly, all crates behave as if they included the following prologue:
/// Operations and constants for `int`
#[path = "int-template.rs"]
#[merge = "int-template/intb.rs"]
#[merge = "int-template/int.rs"]
pub mod int;
/// Operations and constants for `i8`
#[path = "int-template.rs"]
#[merge = "int-template/i8b.rs"]
#[merge = "int-template/i8.rs"]
pub mod i8;
/// Operations and constants for `i16`
#[path = "int-template.rs"]
#[merge = "int-template/i16b.rs"]
#[merge = "int-template/i16.rs"]
pub mod i16;
/// Operations and constants for `i32`
#[path = "int-template.rs"]
#[merge = "int-template/i32b.rs"]
#[merge = "int-template/i32.rs"]
pub mod i32;
/// Operations and constants for `i64`
#[path = "int-template.rs"]
#[merge = "int-template/i64b.rs"]
#[merge = "int-template/i64.rs"]
pub mod i64;
/// Operations and constants for `uint`
#[path = "uint-template.rs"]
#[merge = "uint-template/uintb.rs"]
#[merge = "uint-template/uint.rs"]
pub mod uint;
/// Operations and constants for `u8`
#[path = "uint-template.rs"]
#[merge = "uint-template/u8b.rs"]
#[merge = "uint-template/u8.rs"]
pub mod u8;
/// Operations and constants for `u16`
#[path = "uint-template.rs"]
#[merge = "uint-template/u16b.rs"]
#[merge = "uint-template/u16.rs"]
pub mod u16;
/// Operations and constants for `u32`
#[path = "uint-template.rs"]
#[merge = "uint-template/u32b.rs"]
#[merge = "uint-template/u32.rs"]
pub mod u32;
/// Operations and constants for `u64`
#[path = "uint-template.rs"]
#[merge = "uint-template/u64b.rs"]
#[merge = "uint-template/u64.rs"]
pub mod u64;
@ -121,7 +121,7 @@ pub mod iter;
pub mod logging;
pub mod option;
#[path="iter-trait.rs"]
#[merge = "iter-trait/optionb.rs"]
#[merge = "iter-trait/option.rs"]
pub mod option_iter;
pub mod result;
pub mod to_str;
@ -134,11 +134,11 @@ pub mod clone;
pub mod dvec;
#[path="iter-trait.rs"]
#[merge = "iter-trait/dvecb.rs"]
#[merge = "iter-trait/dvec.rs"]
pub mod dvec_iter;
pub mod dlist;
#[path="iter-trait.rs"]
#[merge = "iter-trait/dlistb.rs"]
#[merge = "iter-trait/dlist.rs"]
pub mod dlist_iter;
pub mod send_map;

View File

@ -1,2 +1,4 @@
pub type T = i16;
pub const bits: uint = u16::bits;
mod inst {
pub type T = i16;
pub const bits: uint = u16::bits;
}

View File

@ -1,4 +0,0 @@
mod inst {
pub type T = i16;
pub const bits: uint = u16::bits;
}

View File

@ -1,2 +1,4 @@
pub type T = i32;
pub const bits: uint = u32::bits;
mod inst {
pub type T = i32;
pub const bits: uint = u32::bits;
}

View File

@ -1,4 +0,0 @@
mod inst {
pub type T = i32;
pub const bits: uint = u32::bits;
}

View File

@ -1,2 +1,4 @@
pub type T = i64;
pub const bits: uint = u64::bits;
mod inst {
pub type T = i64;
pub const bits: uint = u64::bits;
}

View File

@ -1,4 +0,0 @@
mod inst {
pub type T = i64;
pub const bits: uint = u64::bits;
}

View File

@ -1,2 +1,4 @@
pub type T = i8;
pub const bits: uint = u8::bits;
mod inst {
pub type T = i8;
pub const bits: uint = u8::bits;
}

View File

@ -1,4 +0,0 @@
mod inst {
pub type T = i8;
pub const bits: uint = u8::bits;
}

View File

@ -1,38 +1,45 @@
pub type T = int;
pub const bits: uint = uint::bits;
pub use inst::pow;
/// Returns `base` raised to the power of `exponent`
pub fn pow(base: int, exponent: uint) -> int {
if exponent == 0u { return 1; } //Not mathemtically true if ~[base == 0]
if base == 0 { return 0; }
let mut my_pow = exponent;
let mut acc = 1;
let mut multiplier = base;
while(my_pow > 0u) {
if my_pow % 2u == 1u {
acc *= multiplier;
}
my_pow /= 2u;
multiplier *= multiplier;
mod inst {
pub type T = int;
pub const bits: uint = uint::bits;
/// Returns `base` raised to the power of `exponent`
pub fn pow(base: int, exponent: uint) -> int {
if exponent == 0u {
//Not mathemtically true if ~[base == 0]
return 1;
}
if base == 0 { return 0; }
let mut my_pow = exponent;
let mut acc = 1;
let mut multiplier = base;
while(my_pow > 0u) {
if my_pow % 2u == 1u {
acc *= multiplier;
}
my_pow /= 2u;
multiplier *= multiplier;
}
return acc;
}
return acc;
}
#[test]
fn test_pow() {
assert (pow(0, 0u) == 1);
assert (pow(0, 1u) == 0);
assert (pow(0, 2u) == 0);
assert (pow(-1, 0u) == 1);
assert (pow(1, 0u) == 1);
assert (pow(-3, 2u) == 9);
assert (pow(-3, 3u) == -27);
assert (pow(4, 9u) == 262144);
}
#[test]
fn test_pow() {
assert (pow(0, 0u) == 1);
assert (pow(0, 1u) == 0);
assert (pow(0, 2u) == 0);
assert (pow(-1, 0u) == 1);
assert (pow(1, 0u) == 1);
assert (pow(-3, 2u) == 9);
assert (pow(-3, 3u) == -27);
assert (pow(4, 9u) == 262144);
}
#[test]
fn test_overflows() {
assert (max_value > 0);
assert (min_value <= 0);
assert (min_value + max_value + 1 == 0);
}
#[test]
fn test_overflows() {
assert (max_value > 0);
assert (min_value <= 0);
assert (min_value + max_value + 1 == 0);
}
}

View File

@ -1,45 +0,0 @@
pub use inst::pow;
mod inst {
pub type T = int;
pub const bits: uint = uint::bits;
/// Returns `base` raised to the power of `exponent`
pub fn pow(base: int, exponent: uint) -> int {
if exponent == 0u {
//Not mathemtically true if ~[base == 0]
return 1;
}
if base == 0 { return 0; }
let mut my_pow = exponent;
let mut acc = 1;
let mut multiplier = base;
while(my_pow > 0u) {
if my_pow % 2u == 1u {
acc *= multiplier;
}
my_pow /= 2u;
multiplier *= multiplier;
}
return acc;
}
#[test]
fn test_pow() {
assert (pow(0, 0u) == 1);
assert (pow(0, 1u) == 0);
assert (pow(0, 2u) == 0);
assert (pow(-1, 0u) == 1);
assert (pow(1, 0u) == 1);
assert (pow(-3, 2u) == 9);
assert (pow(-3, 3u) == -27);
assert (pow(4, 9u) == 262144);
}
#[test]
fn test_overflows() {
assert (max_value > 0);
assert (min_value <= 0);
assert (min_value + max_value + 1 == 0);
}
}

View File

@ -1,34 +1,38 @@
#[allow(non_camel_case_types)]
pub type IMPL_T<A> = dlist::DList<A>;
mod inst {
#[allow(non_camel_case_types)]
pub type IMPL_T<A> = dlist::DList<A>;
/**
* Iterates through the current contents.
*
* Attempts to access this dlist during iteration are allowed (to allow for
* e.g. breadth-first search with in-place enqueues), but removing the current
* node is forbidden.
*/
pub pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
let mut link = self.peek_n();
while option::is_some(&link) {
let nobe = option::get(link);
assert nobe.linked;
if !f(&nobe.data) { break; }
// Check (weakly) that the user didn't do a remove.
if self.size == 0 {
fail ~"The dlist became empty during iteration??"
/**
* Iterates through the current contents.
*
* Attempts to access this dlist during iteration are allowed (to
* allow for e.g. breadth-first search with in-place enqueues), but
* removing the current node is forbidden.
*/
pub pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
let mut link = self.peek_n();
while option::is_some(&link) {
let nobe = option::get(link);
assert nobe.linked;
if !f(&nobe.data) { break; }
// Check (weakly) that the user didn't do a remove.
if self.size == 0 {
fail ~"The dlist became empty during iteration??"
}
if !nobe.linked ||
(!((nobe.prev.is_some()
|| box::ptr_eq(*self.hd.expect(~"headless dlist?"),
*nobe))
&& (nobe.next.is_some()
|| box::ptr_eq(*self.tl.expect(~"tailless dlist?"),
*nobe)))) {
fail ~"Removing a dlist node during iteration is forbidden!"
}
link = nobe.next_link();
}
if !nobe.linked ||
(!((nobe.prev.is_some()
|| box::ptr_eq(*self.hd.expect(~"headless dlist?"), *nobe)) &&
(nobe.next.is_some()
|| box::ptr_eq(*self.tl.expect(~"tailless dlist?"), *nobe)))) {
fail ~"Removing a dlist node during iteration is forbidden!"
}
link = nobe.next_link();
}
}
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
Some(self.len())
}
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
Some(self.len())
}
}

View File

@ -1,38 +0,0 @@
mod inst {
#[allow(non_camel_case_types)]
pub type IMPL_T<A> = dlist::DList<A>;
/**
* Iterates through the current contents.
*
* Attempts to access this dlist during iteration are allowed (to
* allow for e.g. breadth-first search with in-place enqueues), but
* removing the current node is forbidden.
*/
pub pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
let mut link = self.peek_n();
while option::is_some(&link) {
let nobe = option::get(link);
assert nobe.linked;
if !f(&nobe.data) { break; }
// Check (weakly) that the user didn't do a remove.
if self.size == 0 {
fail ~"The dlist became empty during iteration??"
}
if !nobe.linked ||
(!((nobe.prev.is_some()
|| box::ptr_eq(*self.hd.expect(~"headless dlist?"),
*nobe))
&& (nobe.next.is_some()
|| box::ptr_eq(*self.tl.expect(~"tailless dlist?"),
*nobe)))) {
fail ~"Removing a dlist node during iteration is forbidden!"
}
link = nobe.next_link();
}
}
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
Some(self.len())
}
}

View File

@ -1,20 +1,22 @@
#[allow(non_camel_case_types)]
pub type IMPL_T<A> = dvec::DVec<A>;
mod inst {
#[allow(non_camel_case_types)]
pub type IMPL_T<A> = dvec::DVec<A>;
/**
* Iterates through the current contents.
*
* Attempts to access this dvec during iteration will fail.
*/
pub pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
unsafe {
do self.swap |v| {
v.each(f);
move v
/**
* Iterates through the current contents.
*
* Attempts to access this dvec during iteration will fail.
*/
pub pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
unsafe {
do self.swap |v| {
v.each(f);
move v
}
}
}
}
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
Some(self.len())
}
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
Some(self.len())
}
}

View File

@ -1,22 +0,0 @@
mod inst {
#[allow(non_camel_case_types)]
pub type IMPL_T<A> = dvec::DVec<A>;
/**
* Iterates through the current contents.
*
* Attempts to access this dvec during iteration will fail.
*/
pub pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
unsafe {
do self.swap |v| {
v.each(f);
move v
}
}
}
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
Some(self.len())
}
}

View File

@ -1,16 +1,18 @@
#[allow(non_camel_case_types)]
pub type IMPL_T<A> = Option<A>;
mod inst {
#[allow(non_camel_case_types)]
pub type IMPL_T<A> = Option<A>;
pub pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
match *self {
None => (),
Some(ref a) => { f(a); }
pub pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
match *self {
None => (),
Some(ref a) => { f(a); }
}
}
}
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
match *self {
None => Some(0),
Some(_) => Some(1)
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
match *self {
None => Some(0),
Some(_) => Some(1)
}
}
}
}

View File

@ -1,18 +0,0 @@
mod inst {
#[allow(non_camel_case_types)]
pub type IMPL_T<A> = Option<A>;
pub pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
match *self {
None => (),
Some(ref a) => { f(a); }
}
}
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
match *self {
None => Some(0),
Some(_) => Some(1)
}
}
}

View File

@ -1,2 +1,4 @@
pub type T = u16;
pub const bits: uint = 16;
mod inst {
pub type T = u16;
pub const bits: uint = 16;
}

View File

@ -1,4 +0,0 @@
mod inst {
pub type T = u16;
pub const bits: uint = 16;
}

View File

@ -1,2 +1,4 @@
pub type T = u32;
pub const bits: uint = 32;
mod inst {
pub type T = u32;
pub const bits: uint = 32;
}

View File

@ -1,4 +0,0 @@
mod inst {
pub type T = u32;
pub const bits: uint = 32;
}

View File

@ -1,2 +1,4 @@
pub type T = u64;
pub const bits: uint = 64;
mod inst {
pub type T = u64;
pub const bits: uint = 64;
}

View File

@ -1,4 +0,0 @@
mod inst {
pub type T = u64;
pub const bits: uint = 64;
}

View File

@ -1,7 +1,11 @@
pub type T = u8;
pub const bits: uint = 8;
pub use inst::is_ascii;
// Type-specific functions here. These must be reexported by the
// parent module so that they appear in core::u8 and not core::u8::u8;
mod inst {
pub type T = u8;
pub const bits: uint = 8;
pub pure fn is_ascii(x: T) -> bool { return 0 as T == x & 128 as T; }
// Type-specific functions here. These must be reexported by the
// parent module so that they appear in core::u8 and not core::u8::u8;
pub pure fn is_ascii(x: T) -> bool { return 0 as T == x & 128 as T; }
}

View File

@ -1,11 +0,0 @@
pub use inst::is_ascii;
mod inst {
pub type T = u8;
pub const bits: uint = 8;
// Type-specific functions here. These must be reexported by the
// parent module so that they appear in core::u8 and not core::u8::u8;
pub pure fn is_ascii(x: T) -> bool { return 0 as T == x & 128 as T; }
}

View File

@ -1,153 +1,160 @@
pub type T = uint;
pub use inst::{
div_ceil, div_round, div_floor, iterate,
next_power_of_two
};
#[cfg(target_arch = "x86")]
#[cfg(target_arch = "arm")]
pub const bits: uint = 32;
mod inst {
pub type T = uint;
#[cfg(target_arch = "x86_64")]
pub const bits: uint = 64;
#[cfg(target_arch = "x86")]
#[cfg(target_arch = "arm")]
pub const bits: uint = 32;
/**
* Divide two numbers, return the result, rounded up.
*
* # Arguments
*
* * x - an integer
* * y - an integer distinct from 0u
*
* # Return value
*
* The smallest integer `q` such that `x/y <= q`.
*/
pub pure fn div_ceil(x: uint, y: uint) -> uint {
let div = x / y;
if x % y == 0u { div }
else { div + 1u }
}
#[cfg(target_arch = "x86_64")]
pub const bits: uint = 64;
/**
* Divide two numbers, return the result, rounded to the closest integer.
*
* # Arguments
*
* * x - an integer
* * y - an integer distinct from 0u
*
* # Return value
*
* The integer `q` closest to `x/y`.
*/
pub pure fn div_round(x: uint, y: uint) -> uint {
let div = x / y;
if x % y * 2u < y { div }
else { div + 1u }
}
/**
* Divide two numbers, return the result, rounded down.
*
* Note: This is the same function as `div`.
*
* # Arguments
*
* * x - an integer
* * y - an integer distinct from 0u
*
* # Return value
*
* The smallest integer `q` such that `x/y <= q`. This
* is either `x/y` or `x/y + 1`.
*/
pub pure fn div_floor(x: uint, y: uint) -> uint { return x / y; }
/**
* Iterate over the range [`lo`..`hi`), or stop when requested
*
* # Arguments
*
* * lo - The integer at which to start the loop (included)
* * hi - The integer at which to stop the loop (excluded)
* * it - A block to execute with each consecutive integer of the range.
* Return `true` to continue, `false` to stop.
*
* # Return value
*
* `true` If execution proceeded correctly, `false` if it was interrupted,
* that is if `it` returned `false` at any point.
*/
pub pure fn iterate(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool {
let mut i = lo;
while i < hi {
if (!it(i)) { return false; }
i += 1u;
/**
* Divide two numbers, return the result, rounded up.
*
* # Arguments
*
* * x - an integer
* * y - an integer distinct from 0u
*
* # Return value
*
* The smallest integer `q` such that `x/y <= q`.
*/
pub pure fn div_ceil(x: uint, y: uint) -> uint {
let div = x / y;
if x % y == 0u { div }
else { div + 1u }
}
return true;
}
/// Returns the smallest power of 2 greater than or equal to `n`
#[inline(always)]
pub fn next_power_of_two(n: uint) -> uint {
let halfbits: uint = sys::size_of::<uint>() * 4u;
let mut tmp: uint = n - 1u;
let mut shift: uint = 1u;
while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; }
return tmp + 1u;
}
/**
* Divide two numbers, return the result, rounded to the closest integer.
*
* # Arguments
*
* * x - an integer
* * y - an integer distinct from 0u
*
* # Return value
*
* The integer `q` closest to `x/y`.
*/
pub pure fn div_round(x: uint, y: uint) -> uint {
let div = x / y;
if x % y * 2u < y { div }
else { div + 1u }
}
#[test]
fn test_next_power_of_two() {
assert (uint::next_power_of_two(0u) == 0u);
assert (uint::next_power_of_two(1u) == 1u);
assert (uint::next_power_of_two(2u) == 2u);
assert (uint::next_power_of_two(3u) == 4u);
assert (uint::next_power_of_two(4u) == 4u);
assert (uint::next_power_of_two(5u) == 8u);
assert (uint::next_power_of_two(6u) == 8u);
assert (uint::next_power_of_two(7u) == 8u);
assert (uint::next_power_of_two(8u) == 8u);
assert (uint::next_power_of_two(9u) == 16u);
assert (uint::next_power_of_two(10u) == 16u);
assert (uint::next_power_of_two(11u) == 16u);
assert (uint::next_power_of_two(12u) == 16u);
assert (uint::next_power_of_two(13u) == 16u);
assert (uint::next_power_of_two(14u) == 16u);
assert (uint::next_power_of_two(15u) == 16u);
assert (uint::next_power_of_two(16u) == 16u);
assert (uint::next_power_of_two(17u) == 32u);
assert (uint::next_power_of_two(18u) == 32u);
assert (uint::next_power_of_two(19u) == 32u);
assert (uint::next_power_of_two(20u) == 32u);
assert (uint::next_power_of_two(21u) == 32u);
assert (uint::next_power_of_two(22u) == 32u);
assert (uint::next_power_of_two(23u) == 32u);
assert (uint::next_power_of_two(24u) == 32u);
assert (uint::next_power_of_two(25u) == 32u);
assert (uint::next_power_of_two(26u) == 32u);
assert (uint::next_power_of_two(27u) == 32u);
assert (uint::next_power_of_two(28u) == 32u);
assert (uint::next_power_of_two(29u) == 32u);
assert (uint::next_power_of_two(30u) == 32u);
assert (uint::next_power_of_two(31u) == 32u);
assert (uint::next_power_of_two(32u) == 32u);
assert (uint::next_power_of_two(33u) == 64u);
assert (uint::next_power_of_two(34u) == 64u);
assert (uint::next_power_of_two(35u) == 64u);
assert (uint::next_power_of_two(36u) == 64u);
assert (uint::next_power_of_two(37u) == 64u);
assert (uint::next_power_of_two(38u) == 64u);
assert (uint::next_power_of_two(39u) == 64u);
}
/**
* Divide two numbers, return the result, rounded down.
*
* Note: This is the same function as `div`.
*
* # Arguments
*
* * x - an integer
* * y - an integer distinct from 0u
*
* # Return value
*
* The smallest integer `q` such that `x/y <= q`. This
* is either `x/y` or `x/y + 1`.
*/
pub pure fn div_floor(x: uint, y: uint) -> uint { return x / y; }
#[test]
fn test_overflows() {
assert (uint::max_value > 0u);
assert (uint::min_value <= 0u);
assert (uint::min_value + uint::max_value + 1u == 0u);
}
/**
* Iterate over the range [`lo`..`hi`), or stop when requested
*
* # Arguments
*
* * lo - The integer at which to start the loop (included)
* * hi - The integer at which to stop the loop (excluded)
* * it - A block to execute with each consecutive integer of the range.
* Return `true` to continue, `false` to stop.
*
* # Return value
*
* `true` If execution proceeded correctly, `false` if it was interrupted,
* that is if `it` returned `false` at any point.
*/
pub pure fn iterate(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool {
let mut i = lo;
while i < hi {
if (!it(i)) { return false; }
i += 1u;
}
return true;
}
#[test]
fn test_div() {
assert(uint::div_floor(3u, 4u) == 0u);
assert(uint::div_ceil(3u, 4u) == 1u);
assert(uint::div_round(3u, 4u) == 1u);
}
/// Returns the smallest power of 2 greater than or equal to `n`
#[inline(always)]
pub fn next_power_of_two(n: uint) -> uint {
let halfbits: uint = sys::size_of::<uint>() * 4u;
let mut tmp: uint = n - 1u;
let mut shift: uint = 1u;
while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; }
return tmp + 1u;
}
#[test]
fn test_next_power_of_two() {
assert (uint::next_power_of_two(0u) == 0u);
assert (uint::next_power_of_two(1u) == 1u);
assert (uint::next_power_of_two(2u) == 2u);
assert (uint::next_power_of_two(3u) == 4u);
assert (uint::next_power_of_two(4u) == 4u);
assert (uint::next_power_of_two(5u) == 8u);
assert (uint::next_power_of_two(6u) == 8u);
assert (uint::next_power_of_two(7u) == 8u);
assert (uint::next_power_of_two(8u) == 8u);
assert (uint::next_power_of_two(9u) == 16u);
assert (uint::next_power_of_two(10u) == 16u);
assert (uint::next_power_of_two(11u) == 16u);
assert (uint::next_power_of_two(12u) == 16u);
assert (uint::next_power_of_two(13u) == 16u);
assert (uint::next_power_of_two(14u) == 16u);
assert (uint::next_power_of_two(15u) == 16u);
assert (uint::next_power_of_two(16u) == 16u);
assert (uint::next_power_of_two(17u) == 32u);
assert (uint::next_power_of_two(18u) == 32u);
assert (uint::next_power_of_two(19u) == 32u);
assert (uint::next_power_of_two(20u) == 32u);
assert (uint::next_power_of_two(21u) == 32u);
assert (uint::next_power_of_two(22u) == 32u);
assert (uint::next_power_of_two(23u) == 32u);
assert (uint::next_power_of_two(24u) == 32u);
assert (uint::next_power_of_two(25u) == 32u);
assert (uint::next_power_of_two(26u) == 32u);
assert (uint::next_power_of_two(27u) == 32u);
assert (uint::next_power_of_two(28u) == 32u);
assert (uint::next_power_of_two(29u) == 32u);
assert (uint::next_power_of_two(30u) == 32u);
assert (uint::next_power_of_two(31u) == 32u);
assert (uint::next_power_of_two(32u) == 32u);
assert (uint::next_power_of_two(33u) == 64u);
assert (uint::next_power_of_two(34u) == 64u);
assert (uint::next_power_of_two(35u) == 64u);
assert (uint::next_power_of_two(36u) == 64u);
assert (uint::next_power_of_two(37u) == 64u);
assert (uint::next_power_of_two(38u) == 64u);
assert (uint::next_power_of_two(39u) == 64u);
}
#[test]
fn test_overflows() {
assert (uint::max_value > 0u);
assert (uint::min_value <= 0u);
assert (uint::min_value + uint::max_value + 1u == 0u);
}
#[test]
fn test_div() {
assert(uint::div_floor(3u, 4u) == 0u);
assert(uint::div_ceil(3u, 4u) == 1u);
assert(uint::div_round(3u, 4u) == 1u);
}
}

View File

@ -1,160 +0,0 @@
pub use inst::{
div_ceil, div_round, div_floor, iterate,
next_power_of_two
};
mod inst {
pub type T = uint;
#[cfg(target_arch = "x86")]
#[cfg(target_arch = "arm")]
pub const bits: uint = 32;
#[cfg(target_arch = "x86_64")]
pub const bits: uint = 64;
/**
* Divide two numbers, return the result, rounded up.
*
* # Arguments
*
* * x - an integer
* * y - an integer distinct from 0u
*
* # Return value
*
* The smallest integer `q` such that `x/y <= q`.
*/
pub pure fn div_ceil(x: uint, y: uint) -> uint {
let div = x / y;
if x % y == 0u { div }
else { div + 1u }
}
/**
* Divide two numbers, return the result, rounded to the closest integer.
*
* # Arguments
*
* * x - an integer
* * y - an integer distinct from 0u
*
* # Return value
*
* The integer `q` closest to `x/y`.
*/
pub pure fn div_round(x: uint, y: uint) -> uint {
let div = x / y;
if x % y * 2u < y { div }
else { div + 1u }
}
/**
* Divide two numbers, return the result, rounded down.
*
* Note: This is the same function as `div`.
*
* # Arguments
*
* * x - an integer
* * y - an integer distinct from 0u
*
* # Return value
*
* The smallest integer `q` such that `x/y <= q`. This
* is either `x/y` or `x/y + 1`.
*/
pub pure fn div_floor(x: uint, y: uint) -> uint { return x / y; }
/**
* Iterate over the range [`lo`..`hi`), or stop when requested
*
* # Arguments
*
* * lo - The integer at which to start the loop (included)
* * hi - The integer at which to stop the loop (excluded)
* * it - A block to execute with each consecutive integer of the range.
* Return `true` to continue, `false` to stop.
*
* # Return value
*
* `true` If execution proceeded correctly, `false` if it was interrupted,
* that is if `it` returned `false` at any point.
*/
pub pure fn iterate(lo: uint, hi: uint, it: fn(uint) -> bool) -> bool {
let mut i = lo;
while i < hi {
if (!it(i)) { return false; }
i += 1u;
}
return true;
}
/// Returns the smallest power of 2 greater than or equal to `n`
#[inline(always)]
pub fn next_power_of_two(n: uint) -> uint {
let halfbits: uint = sys::size_of::<uint>() * 4u;
let mut tmp: uint = n - 1u;
let mut shift: uint = 1u;
while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; }
return tmp + 1u;
}
#[test]
fn test_next_power_of_two() {
assert (uint::next_power_of_two(0u) == 0u);
assert (uint::next_power_of_two(1u) == 1u);
assert (uint::next_power_of_two(2u) == 2u);
assert (uint::next_power_of_two(3u) == 4u);
assert (uint::next_power_of_two(4u) == 4u);
assert (uint::next_power_of_two(5u) == 8u);
assert (uint::next_power_of_two(6u) == 8u);
assert (uint::next_power_of_two(7u) == 8u);
assert (uint::next_power_of_two(8u) == 8u);
assert (uint::next_power_of_two(9u) == 16u);
assert (uint::next_power_of_two(10u) == 16u);
assert (uint::next_power_of_two(11u) == 16u);
assert (uint::next_power_of_two(12u) == 16u);
assert (uint::next_power_of_two(13u) == 16u);
assert (uint::next_power_of_two(14u) == 16u);
assert (uint::next_power_of_two(15u) == 16u);
assert (uint::next_power_of_two(16u) == 16u);
assert (uint::next_power_of_two(17u) == 32u);
assert (uint::next_power_of_two(18u) == 32u);
assert (uint::next_power_of_two(19u) == 32u);
assert (uint::next_power_of_two(20u) == 32u);
assert (uint::next_power_of_two(21u) == 32u);
assert (uint::next_power_of_two(22u) == 32u);
assert (uint::next_power_of_two(23u) == 32u);
assert (uint::next_power_of_two(24u) == 32u);
assert (uint::next_power_of_two(25u) == 32u);
assert (uint::next_power_of_two(26u) == 32u);
assert (uint::next_power_of_two(27u) == 32u);
assert (uint::next_power_of_two(28u) == 32u);
assert (uint::next_power_of_two(29u) == 32u);
assert (uint::next_power_of_two(30u) == 32u);
assert (uint::next_power_of_two(31u) == 32u);
assert (uint::next_power_of_two(32u) == 32u);
assert (uint::next_power_of_two(33u) == 64u);
assert (uint::next_power_of_two(34u) == 64u);
assert (uint::next_power_of_two(35u) == 64u);
assert (uint::next_power_of_two(36u) == 64u);
assert (uint::next_power_of_two(37u) == 64u);
assert (uint::next_power_of_two(38u) == 64u);
assert (uint::next_power_of_two(39u) == 64u);
}
#[test]
fn test_overflows() {
assert (uint::max_value > 0u);
assert (uint::min_value <= 0u);
assert (uint::min_value + uint::max_value + 1u == 0u);
}
#[test]
fn test_div() {
assert(uint::div_floor(3u, 4u) == 0u);
assert(uint::div_ceil(3u, 4u) == 1u);
assert(uint::div_round(3u, 4u) == 1u);
}
}