Add _uint module to std, move some code around.
This commit is contained in:
parent
5f9750ca2c
commit
9fc4fc6692
6 changed files with 75 additions and 60 deletions
|
@ -25,66 +25,12 @@ iter range(mutable int lo, int hi) -> int {
|
|||
}
|
||||
}
|
||||
|
||||
iter urange(mutable uint lo, uint hi) -> uint {
|
||||
while (lo < hi) {
|
||||
put lo;
|
||||
lo += 1u;
|
||||
}
|
||||
}
|
||||
|
||||
fn next_power_of_two(uint n) -> uint {
|
||||
// FIXME change |* uint(4)| below to |* uint(8) / uint(2)| and watch the
|
||||
// world explode.
|
||||
let uint halfbits = sys.rustrt.size_of[uint]() * 4u;
|
||||
let uint tmp = n - 1u;
|
||||
let uint shift = 1u;
|
||||
while (shift <= halfbits) {
|
||||
tmp |= tmp >> shift;
|
||||
shift <<= 1u;
|
||||
}
|
||||
ret tmp + 1u;
|
||||
}
|
||||
|
||||
fn uto_str(mutable uint n, uint radix) -> str
|
||||
{
|
||||
check (0u < radix && radix <= 16u);
|
||||
fn digit(uint n) -> str {
|
||||
alt (n) {
|
||||
case (0u) { ret "0"; }
|
||||
case (1u) { ret "1"; }
|
||||
case (2u) { ret "2"; }
|
||||
case (3u) { ret "3"; }
|
||||
case (4u) { ret "4"; }
|
||||
case (5u) { ret "5"; }
|
||||
case (6u) { ret "6"; }
|
||||
case (7u) { ret "7"; }
|
||||
case (8u) { ret "8"; }
|
||||
case (9u) { ret "9"; }
|
||||
case (10u) { ret "a"; }
|
||||
case (11u) { ret "b"; }
|
||||
case (12u) { ret "c"; }
|
||||
case (13u) { ret "d"; }
|
||||
case (14u) { ret "e"; }
|
||||
case (15u) { ret "f"; }
|
||||
}
|
||||
}
|
||||
|
||||
if (n == 0u) { ret "0"; }
|
||||
|
||||
let str s = "";
|
||||
while (n > 0u) {
|
||||
s = digit(n % radix) + s;
|
||||
n /= radix;
|
||||
}
|
||||
ret s;
|
||||
}
|
||||
|
||||
fn to_str(mutable int n, uint radix) -> str
|
||||
{
|
||||
check (0u < radix && radix <= 16u);
|
||||
if (n < 0) {
|
||||
ret "-" + uto_str((-n) as uint, radix);
|
||||
ret "-" + _uint.to_str((-n) as uint, radix);
|
||||
} else {
|
||||
ret uto_str(n as uint, radix);
|
||||
ret _uint.to_str(n as uint, radix);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -146,7 +146,7 @@ fn file_writer(str path,
|
|||
unsafe obj fw(buf_writer out) {
|
||||
fn write_str(str s) { out.write(_str.bytes(s)); }
|
||||
fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); }
|
||||
fn write_uint(uint n) { out.write(_str.bytes(_int.uto_str(n, 10u))); }
|
||||
fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); }
|
||||
}
|
||||
ret fw(new_buf_writer(path, flags));
|
||||
}
|
||||
|
|
68
src/lib/_uint.rs
Normal file
68
src/lib/_uint.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
import std.sys;
|
||||
|
||||
fn add(uint x, uint y) -> uint { ret x + y; }
|
||||
fn sub(uint x, uint y) -> uint { ret x - y; }
|
||||
fn mul(uint x, uint y) -> uint { ret x * y; }
|
||||
fn div(uint x, uint y) -> uint { ret x / y; }
|
||||
fn rem(uint x, uint y) -> uint { ret x % y; }
|
||||
|
||||
fn lt(uint x, uint y) -> bool { ret x < y; }
|
||||
fn le(uint x, uint y) -> bool { ret x <= y; }
|
||||
fn eq(uint x, uint y) -> bool { ret x == y; }
|
||||
fn ne(uint x, uint y) -> bool { ret x != y; }
|
||||
fn ge(uint x, uint y) -> bool { ret x >= y; }
|
||||
fn gt(uint x, uint y) -> bool { ret x > y; }
|
||||
|
||||
iter range(mutable uint lo, uint hi) -> uint {
|
||||
while (lo < hi) {
|
||||
put lo;
|
||||
lo += 1u;
|
||||
}
|
||||
}
|
||||
|
||||
fn next_power_of_two(uint n) -> uint {
|
||||
// FIXME change |* uint(4)| below to |* uint(8) / uint(2)| and watch the
|
||||
// world explode.
|
||||
let uint halfbits = sys.rustrt.size_of[uint]() * 4u;
|
||||
let uint tmp = n - 1u;
|
||||
let uint shift = 1u;
|
||||
while (shift <= halfbits) {
|
||||
tmp |= tmp >> shift;
|
||||
shift <<= 1u;
|
||||
}
|
||||
ret tmp + 1u;
|
||||
}
|
||||
|
||||
fn to_str(mutable uint n, uint radix) -> str
|
||||
{
|
||||
check (0u < radix && radix <= 16u);
|
||||
fn digit(uint n) -> str {
|
||||
alt (n) {
|
||||
case (0u) { ret "0"; }
|
||||
case (1u) { ret "1"; }
|
||||
case (2u) { ret "2"; }
|
||||
case (3u) { ret "3"; }
|
||||
case (4u) { ret "4"; }
|
||||
case (5u) { ret "5"; }
|
||||
case (6u) { ret "6"; }
|
||||
case (7u) { ret "7"; }
|
||||
case (8u) { ret "8"; }
|
||||
case (9u) { ret "9"; }
|
||||
case (10u) { ret "a"; }
|
||||
case (11u) { ret "b"; }
|
||||
case (12u) { ret "c"; }
|
||||
case (13u) { ret "d"; }
|
||||
case (14u) { ret "e"; }
|
||||
case (15u) { ret "f"; }
|
||||
}
|
||||
}
|
||||
|
||||
if (n == 0u) { ret "0"; }
|
||||
|
||||
let str s = "";
|
||||
while (n > 0u) {
|
||||
s = digit(n % radix) + s;
|
||||
n /= radix;
|
||||
}
|
||||
ret s;
|
||||
}
|
|
@ -42,7 +42,7 @@ fn create[T]() -> t[T] {
|
|||
}
|
||||
}
|
||||
|
||||
let uint nalloc = _int.next_power_of_two(nelts + 1u);
|
||||
let uint nalloc = _uint.next_power_of_two(nelts + 1u);
|
||||
let _vec.init_op[cell[T]] copy_op = bind fill[T](_, nelts, lo, elts);
|
||||
ret _vec.init_fn[cell[T]](copy_op, nalloc);
|
||||
}
|
||||
|
|
|
@ -145,7 +145,7 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
|
|||
fn insert(&K key, &V val) -> bool {
|
||||
let util.rational load = rec(num=(nelts + 1u) as int, den=nbkts as int);
|
||||
if (!util.rational_leq(load, lf)) {
|
||||
let uint nnewbkts = _int.next_power_of_two(nbkts + 1u);
|
||||
let uint nnewbkts = _uint.next_power_of_two(nbkts + 1u);
|
||||
|
||||
let vec[mutable bucket[K, V]] newbkts = make_buckets[K, V](nnewbkts);
|
||||
rehash[K, V](hasher, eqer, bkts, nbkts, newbkts, nnewbkts);
|
||||
|
|
|
@ -7,6 +7,7 @@ meta (name = "std",
|
|||
// Built-in types support modules.
|
||||
|
||||
mod _int;
|
||||
mod _uint;
|
||||
mod _u8;
|
||||
mod _vec;
|
||||
mod _str;
|
||||
|
@ -28,7 +29,7 @@ auth _str = unsafe;
|
|||
auth _vec = unsafe;
|
||||
auth _task = unsafe;
|
||||
|
||||
auth _int.next_power_of_two = unsafe;
|
||||
auth _uint.next_power_of_two = unsafe;
|
||||
auth map.mk_hashmap = unsafe;
|
||||
auth rand.mk_rng = unsafe;
|
||||
|
||||
|
|
Loading…
Reference in a new issue