Merge pull request #1450 from erickt/master

adding some misc functions and some functions just for [u8]
This commit is contained in:
Graydon Hoare 2012-01-06 13:04:49 -08:00
commit 4f3171ea81
7 changed files with 214 additions and 20 deletions

View File

@ -16,6 +16,49 @@ Return the maximal value for a u32
*/
const max_value: u32 = 0xffff_ffffu32;
/* Function: add */
pure fn add(x: u32, y: u32) -> u32 { ret x + y; }
/* Function: sub */
pure fn sub(x: u32, y: u32) -> u32 { ret x - y; }
/* Function: mul */
pure fn mul(x: u32, y: u32) -> u32 { ret x * y; }
/* Function: div */
pure fn div(x: u32, y: u32) -> u32 { ret x / y; }
/* Function: rem */
pure fn rem(x: u32, y: u32) -> u32 { ret x % y; }
/* Predicate: lt */
pure fn lt(x: u32, y: u32) -> bool { ret x < y; }
/* Predicate: le */
pure fn le(x: u32, y: u32) -> bool { ret x <= y; }
/* Predicate: eq */
pure fn eq(x: u32, y: u32) -> bool { ret x == y; }
/* Predicate: ne */
pure fn ne(x: u32, y: u32) -> bool { ret x != y; }
/* Predicate: ge */
pure fn ge(x: u32, y: u32) -> bool { ret x >= y; }
/* Predicate: gt */
pure fn gt(x: u32, y: u32) -> bool { ret x > y; }
/*
Function: range
Iterate over the range [`lo`..`hi`)
*/
fn range(lo: u32, hi: u32, it: block(u32)) {
let i = lo;
while i < hi { it(i); i += 1u32; }
}
//
// Local Variables:
// mode: rust

View File

@ -16,6 +16,49 @@ Return the maximal value for a u64
*/
const max_value: u64 = 18446744073709551615u64;
/* Function: add */
pure fn add(x: u64, y: u64) -> u64 { ret x + y; }
/* Function: sub */
pure fn sub(x: u64, y: u64) -> u64 { ret x - y; }
/* Function: mul */
pure fn mul(x: u64, y: u64) -> u64 { ret x * y; }
/* Function: div */
pure fn div(x: u64, y: u64) -> u64 { ret x / y; }
/* Function: rem */
pure fn rem(x: u64, y: u64) -> u64 { ret x % y; }
/* Predicate: lt */
pure fn lt(x: u64, y: u64) -> bool { ret x < y; }
/* Predicate: le */
pure fn le(x: u64, y: u64) -> bool { ret x <= y; }
/* Predicate: eq */
pure fn eq(x: u64, y: u64) -> bool { ret x == y; }
/* Predicate: ne */
pure fn ne(x: u64, y: u64) -> bool { ret x != y; }
/* Predicate: ge */
pure fn ge(x: u64, y: u64) -> bool { ret x >= y; }
/* Predicate: gt */
pure fn gt(x: u64, y: u64) -> bool { ret x > y; }
/*
Function: range
Iterate over the range [`lo`..`hi`)
*/
fn range(lo: u64, hi: u64, it: block(u64)) {
let i = lo;
while i < hi { it(i); i += 1u64; }
}
/*
Function: to_str

View File

@ -103,6 +103,13 @@ pure fn ge(x: uint, y: uint) -> bool { ret x >= y; }
/* Predicate: gt */
pure fn gt(x: uint, y: uint) -> bool { ret x > y; }
/*
Function: hash
Produce a uint suitable for use in a hash table
*/
fn hash(x: uint) -> uint { ret x; }
/*
Function: range

View File

@ -719,7 +719,7 @@ Function: enum_chars
Returns a vector containing a range of chars
*/
fn enum_chars(start: u8, end: u8) : u8::le(start, end) -> [char] {
fn enum_chars(start: u8, end: u8) : ::u8::le(start, end) -> [char] {
let i = start;
let r = [];
while i <= end { r += [i as char]; i += 1u as u8; }
@ -889,6 +889,99 @@ mod unsafe {
}
}
/*
Module: u8
*/
mod u8 {
export cmp;
export lt, le, eq, ne, ge, gt;
export hash;
#[nolink]
#[abi = "cdecl"]
native mod libc {
fn memcmp(s1: *u8, s2: *u8, n: ctypes::size_t) -> ctypes::c_int;
}
/*
Function cmp
Bytewise string comparison
*/
pure fn cmp(&&a: [u8], &&b: [u8]) -> int unsafe {
let a_len = len(a);
let b_len = len(b);
let n = math::min(a_len, b_len) as ctypes::size_t;
let r = libc::memcmp(to_ptr(a), to_ptr(b), n) as int;
if r != 0 { r } else {
if a_len == b_len {
0
} else if a_len < b_len {
-1
} else {
1
}
}
}
/*
Function: lt
Bytewise less than or equal
*/
pure fn lt(&&a: [u8], &&b: [u8]) -> bool { cmp(a, b) < 0 }
/*
Function: le
Bytewise less than or equal
*/
pure fn le(&&a: [u8], &&b: [u8]) -> bool { cmp(a, b) <= 0 }
/*
Function: eq
Bytewise equality
*/
pure fn eq(&&a: [u8], &&b: [u8]) -> bool unsafe { cmp(a, b) == 0 }
/*
Function: ne
Bytewise inequality
*/
pure fn ne(&&a: [u8], &&b: [u8]) -> bool unsafe { cmp(a, b) != 0 }
/*
Function: ge
Bytewise greater than or equal
*/
pure fn ge(&&a: [u8], &&b: [u8]) -> bool { cmp(a, b) >= 0 }
/*
Function: gt
Bytewise greater than
*/
pure fn gt(&&a: [u8], &&b: [u8]) -> bool { cmp(a, b) > 0 }
/*
Function: hash
String hash function
*/
fn hash(&&s: [u8]) -> uint {
// djb hash.
// FIXME: replace with murmur.
let u: uint = 5381u;
vec::iter(s, { |c| u *= 33u; u += c as uint; });
ret u;
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -31,7 +31,7 @@ taken to ensure that a reference to the c_vec::t is still held if needed.
export t;
export create, create_with_dtor;
export get, set;
export size;
export len;
export ptr;
/*
@ -43,7 +43,7 @@ export ptr;
*/
tag t<T> {
t({ base: *mutable T, size: uint, rsrc: @dtor_res});
t({ base: *mutable T, len: uint, rsrc: @dtor_res});
}
resource dtor_res(dtor: option::t<fn@()>) {
@ -60,16 +60,16 @@ resource dtor_res(dtor: option::t<fn@()>) {
/*
Function: create
Create a c_vec::t from a native buffer with a given size.
Create a c_vec::t from a native buffer with a given length.
Parameters:
base - A native pointer to a buffer
size - The number of elements in the buffer
len - The number of elements in the buffer
*/
unsafe fn create<T>(base: *mutable T, size: uint) -> t<T> {
unsafe fn create<T>(base: *mutable T, len: uint) -> t<T> {
ret t({base: base,
size: size,
len: len,
rsrc: @dtor_res(option::none)
});
}
@ -77,20 +77,20 @@ unsafe fn create<T>(base: *mutable T, size: uint) -> t<T> {
/*
Function: create_with_dtor
Create a c_vec::t from a native buffer, with a given size,
Create a c_vec::t from a native buffer, with a given length,
and a function to run upon destruction.
Parameters:
base - A native pointer to a buffer
size - The number of elements in the buffer
len - The number of elements in the buffer
dtor - A function to run when the value is destructed, useful
for freeing the buffer, etc.
*/
unsafe fn create_with_dtor<T>(base: *mutable T, size: uint, dtor: fn@())
unsafe fn create_with_dtor<T>(base: *mutable T, len: uint, dtor: fn@())
-> t<T> {
ret t({base: base,
size: size,
len: len,
rsrc: @dtor_res(option::some(dtor))
});
}
@ -109,7 +109,7 @@ Failure:
If `ofs` is greater or equal to the length of the vector
*/
fn get<T: copy>(t: t<T>, ofs: uint) -> T {
assert ofs < (*t).size;
assert ofs < len(t);
ret unsafe { *ptr::mut_offset((*t).base, ofs) };
}
@ -123,7 +123,7 @@ Failure:
If `ofs` is greater or equal to the length of the vector
*/
fn set<T: copy>(t: t<T>, ofs: uint, v: T) {
assert ofs < (*t).size;
assert ofs < len(t);
unsafe { *ptr::mut_offset((*t).base, ofs) = v };
}
@ -131,14 +131,13 @@ fn set<T: copy>(t: t<T>, ofs: uint, v: T) {
Section: Elimination forms
*/
// FIXME: Rename to len
/*
Function: size
Function: len
Returns the length of the vector
*/
fn size<T>(t: t<T>) -> uint {
ret (*t).size;
fn len<T>(t: t<T>) -> uint {
ret (*t).len;
}
/*

View File

@ -378,13 +378,22 @@ fn new_str_hash<V: copy>() -> hashmap<str, V> {
ret mk_hashmap(str::hash, str::eq);
}
/*
Function: new_bytes_hash
Construct a hashmap for byte string keys
*/
fn new_bytes_hash<V: copy>() -> hashmap<[u8], V> {
ret mk_hashmap(vec::u8::hash, vec::u8::eq);
}
/*
Function: new_int_hash
Construct a hashmap for int keys
*/
fn new_int_hash<V: copy>() -> hashmap<int, V> {
fn hash_int(&&x: int) -> uint { ret x as uint; }
fn hash_int(&&x: int) -> uint { int::hash(x) }
fn eq_int(&&a: int, &&b: int) -> bool { ret a == b; }
ret mk_hashmap(hash_int, eq_int);
}
@ -395,7 +404,7 @@ Function: new_uint_hash
Construct a hashmap for uint keys
*/
fn new_uint_hash<V: copy>() -> hashmap<uint, V> {
fn hash_uint(&&x: uint) -> uint { ret x; }
fn hash_uint(&&x: uint) -> uint { uint::hash(x) }
fn eq_uint(&&a: uint, &&b: uint) -> bool { ret a == b; }
ret mk_hashmap(hash_uint, eq_uint);
}

View File

@ -28,7 +28,7 @@ fn test_basic() {
set(cv, 4u, 9u8);
assert get(cv, 3u) == 8u8;
assert get(cv, 4u) == 9u8;
assert size(cv) == 16u;
assert len(cv) == 16u;
}
#[test]