1
Fork 0

Reformat standard library; no code changes.

This commit is contained in:
Graydon Hoare 2010-09-22 15:44:13 -07:00
parent 738fe078a4
commit 2880ecd73f
16 changed files with 840 additions and 696 deletions

View file

@ -19,18 +19,27 @@ fn nonpositive(int x) -> bool { ret x <= 0; }
fn nonnegative(int x) -> bool { ret x >= 0; } fn nonnegative(int x) -> bool { ret x >= 0; }
iter range(mutable int lo, int hi) -> int { iter range(mutable int lo, int hi) -> int {
while (lo < hi) { while (lo < hi) {
put lo; put lo;
lo += 1; lo += 1;
} }
} }
fn to_str(mutable int n, uint radix) -> str fn to_str(mutable int n, uint radix) -> str
{ {
check (0u < radix && radix <= 16u); check (0u < radix && radix <= 16u);
if (n < 0) { if (n < 0) {
ret "-" + _uint.to_str((-n) as uint, radix); ret "-" + _uint.to_str((-n) as uint, radix);
} else { } else {
ret _uint.to_str(n as uint, radix); ret _uint.to_str(n as uint, radix);
} }
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -3,58 +3,58 @@ import rustrt.sbuf;
import std._vec.rustrt.vbuf; import std._vec.rustrt.vbuf;
native "rust" mod rustrt { native "rust" mod rustrt {
type sbuf; type sbuf;
fn str_buf(str s) -> sbuf; fn str_buf(str s) -> sbuf;
fn str_byte_len(str s) -> uint; fn str_byte_len(str s) -> uint;
fn str_alloc(uint n_bytes) -> str; fn str_alloc(uint n_bytes) -> str;
fn str_from_vec(vec[u8] b) -> str; fn str_from_vec(vec[u8] b) -> str;
fn refcount[T](str s) -> uint; fn refcount[T](str s) -> uint;
} }
fn eq(&str a, &str b) -> bool { fn eq(&str a, &str b) -> bool {
let uint i = byte_len(a); let uint i = byte_len(a);
if (byte_len(b) != i) { if (byte_len(b) != i) {
ret false; ret false;
}
while (i > 0u) {
i -= 1u;
auto cha = a.(i);
auto chb = b.(i);
if (cha != chb) {
ret false;
} }
} while (i > 0u) {
ret true; i -= 1u;
auto cha = a.(i);
auto chb = b.(i);
if (cha != chb) {
ret false;
}
}
ret true;
} }
fn hash(&str s) -> uint { fn hash(&str s) -> uint {
// djb hash. // djb hash.
// FIXME: replace with murmur. // FIXME: replace with murmur.
let uint u = 5381u; let uint u = 5381u;
for (u8 c in s) { for (u8 c in s) {
u *= 33u; u *= 33u;
u += (c as uint); u += (c as uint);
} }
ret u; ret u;
} }
fn is_utf8(vec[u8] v) -> bool { fn is_utf8(vec[u8] v) -> bool {
fail; // FIXME fail; // FIXME
} }
fn is_ascii(str s) -> bool { fn is_ascii(str s) -> bool {
let uint i = byte_len(s); let uint i = byte_len(s);
while (i > 0u) { while (i > 0u) {
i -= 1u; i -= 1u;
if ((s.(i) & 0x80u8) != 0u8) { if ((s.(i) & 0x80u8) != 0u8) {
ret false; ret false;
}
} }
} ret true;
ret true;
} }
fn alloc(uint n_bytes) -> str { fn alloc(uint n_bytes) -> str {
ret rustrt.str_alloc(n_bytes); ret rustrt.str_alloc(n_bytes);
} }
// Returns the number of bytes (a.k.a. UTF-8 code units) in s. // Returns the number of bytes (a.k.a. UTF-8 code units) in s.
@ -63,152 +63,152 @@ fn alloc(uint n_bytes) -> str {
// http://icu-project.org/apiref/icu4c/classBreakIterator.html for a // http://icu-project.org/apiref/icu4c/classBreakIterator.html for a
// way to implement those. // way to implement those.
fn byte_len(str s) -> uint { fn byte_len(str s) -> uint {
ret rustrt.str_byte_len(s); ret rustrt.str_byte_len(s);
} }
fn buf(str s) -> sbuf { fn buf(str s) -> sbuf {
ret rustrt.str_buf(s); ret rustrt.str_buf(s);
} }
fn bytes(str s) -> vec[u8] { fn bytes(str s) -> vec[u8] {
/* FIXME (issue #58): /* FIXME (issue #58):
* Should be... * Should be...
* *
* fn ith(str s, uint i) -> u8 { * fn ith(str s, uint i) -> u8 {
* ret s.(i); * ret s.(i);
* } * }
* ret _vec.init_fn[u8](bind ith(s, _), byte_len(s)); * ret _vec.init_fn[u8](bind ith(s, _), byte_len(s));
* *
* but we do not correctly decrement refcount of s when * but we do not correctly decrement refcount of s when
* the binding dies, so we have to do this manually. * the binding dies, so we have to do this manually.
*/ */
let uint n = _str.byte_len(s); let uint n = _str.byte_len(s);
let vec[u8] v = _vec.alloc[u8](n); let vec[u8] v = _vec.alloc[u8](n);
let uint i = 0u; let uint i = 0u;
while (i < n) { while (i < n) {
v += vec(s.(i)); v += vec(s.(i));
i += 1u; i += 1u;
} }
ret v; ret v;
} }
fn from_bytes(vec[u8] v) : is_utf8(v) -> str { fn from_bytes(vec[u8] v) : is_utf8(v) -> str {
ret rustrt.str_from_vec(v); ret rustrt.str_from_vec(v);
} }
fn refcount(str s) -> uint { fn refcount(str s) -> uint {
// -1 because calling this function incremented the refcount. // -1 because calling this function incremented the refcount.
ret rustrt.refcount[u8](s) - 1u; ret rustrt.refcount[u8](s) - 1u;
} }
// Standard bits from the world of string libraries. // Standard bits from the world of string libraries.
fn index(str s, u8 c) -> int { fn index(str s, u8 c) -> int {
let int i = 0; let int i = 0;
for (u8 k in s) { for (u8 k in s) {
if (k == c) { if (k == c) {
ret i; ret i;
}
i += 1;
} }
i += 1; ret -1;
}
ret -1;
} }
fn rindex(str s, u8 c) -> int { fn rindex(str s, u8 c) -> int {
let int n = _str.byte_len(s) as int; let int n = _str.byte_len(s) as int;
while (n >= 0) { while (n >= 0) {
if (s.(n) == c) { if (s.(n) == c) {
ret n; ret n;
}
n -= 1;
} }
n -= 1; ret n;
}
ret n;
} }
fn find(str haystack, str needle) -> int { fn find(str haystack, str needle) -> int {
let int haystack_len = byte_len(haystack) as int; let int haystack_len = byte_len(haystack) as int;
let int needle_len = byte_len(needle) as int; let int needle_len = byte_len(needle) as int;
if (needle_len == 0) { if (needle_len == 0) {
ret 0; ret 0;
}
fn match_at(&str haystack,
&str needle,
int i) -> bool {
let int j = i;
for (u8 c in needle) {
if (haystack.(j) != c) {
ret false;
}
j += 1;
} }
ret true;
}
let int i = 0; fn match_at(&str haystack,
while (i <= haystack_len - needle_len) { &str needle,
if (match_at(haystack, needle, i)) { int i) -> bool {
ret i; let int j = i;
for (u8 c in needle) {
if (haystack.(j) != c) {
ret false;
}
j += 1;
}
ret true;
} }
i += 1;
} let int i = 0;
ret -1; while (i <= haystack_len - needle_len) {
if (match_at(haystack, needle, i)) {
ret i;
}
i += 1;
}
ret -1;
} }
fn substr(str s, uint begin, uint len) -> str { fn substr(str s, uint begin, uint len) -> str {
let str accum = ""; let str accum = "";
let uint i = begin; let uint i = begin;
while (i < begin+len) { while (i < begin+len) {
accum += s.(i); accum += s.(i);
i += 1u; i += 1u;
} }
ret accum; ret accum;
} }
fn split(str s, u8 sep) -> vec[str] { fn split(str s, u8 sep) -> vec[str] {
let vec[str] v = vec(); let vec[str] v = vec();
let str accum = ""; let str accum = "";
let bool ends_with_sep = false; let bool ends_with_sep = false;
for (u8 c in s) { for (u8 c in s) {
if (c == sep) { if (c == sep) {
v += accum; v += accum;
accum = ""; accum = "";
ends_with_sep = true; ends_with_sep = true;
} else { } else {
accum += c; accum += c;
ends_with_sep = false; ends_with_sep = false;
}
} }
} if (_str.byte_len(accum) != 0u ||
if (_str.byte_len(accum) != 0u || ends_with_sep) {
ends_with_sep) { v += accum;
v += accum; }
} ret v;
ret v;
} }
fn concat(vec[str] v) -> str { fn concat(vec[str] v) -> str {
let str s = ""; let str s = "";
for (str ss in v) { for (str ss in v) {
s += ss; s += ss;
} }
ret s; ret s;
} }
fn connect(vec[str] v, str sep) -> str { fn connect(vec[str] v, str sep) -> str {
let str s = ""; let str s = "";
let bool first = true; let bool first = true;
for (str ss in v) { for (str ss in v) {
if (first) { if (first) {
first = false; first = false;
} else { } else {
s += sep; s += sep;
}
s += ss;
} }
s += ss; ret s;
}
ret s;
} }
@ -216,6 +216,7 @@ fn connect(vec[str] v, str sep) -> str {
// mode: rust; // mode: rust;
// fill-column: 78; // fill-column: 78;
// indent-tabs-mode: nil // indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix // buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'"; // compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End: // End:

View file

@ -1,5 +1,5 @@
native "rust" mod rustrt { native "rust" mod rustrt {
fn task_sleep(uint time_in_us); fn task_sleep(uint time_in_us);
} }
/** /**
@ -8,5 +8,14 @@ native "rust" mod rustrt {
* arg: time_in_us maximum number of microseconds to yield control for * arg: time_in_us maximum number of microseconds to yield control for
*/ */
fn sleep(uint time_in_us) { fn sleep(uint time_in_us) {
ret rustrt.task_sleep(time_in_us); ret rustrt.task_sleep(time_in_us);
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -12,9 +12,17 @@ fn ge(u8 x, u8 y) -> bool { ret x >= y; }
fn gt(u8 x, u8 y) -> bool { ret x > y; } fn gt(u8 x, u8 y) -> bool { ret x > y; }
iter range(mutable u8 lo, u8 hi) -> u8 { iter range(mutable u8 lo, u8 hi) -> u8 {
while (lo < hi) { while (lo < hi) {
put lo; put lo;
lo += 1u8; lo += 1u8;
} }
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -14,73 +14,82 @@ fn ge(uint x, uint y) -> bool { ret x >= y; }
fn gt(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 { iter range(mutable uint lo, uint hi) -> uint {
while (lo < hi) { while (lo < hi) {
put lo; put lo;
lo += 1u; lo += 1u;
} }
} }
fn next_power_of_two(uint n) -> uint { fn next_power_of_two(uint n) -> uint {
// FIXME change |* uint(4)| below to |* uint(8) / uint(2)| and watch the // FIXME change |* uint(4)| below to |* uint(8) / uint(2)| and watch the
// world explode. // world explode.
let uint halfbits = sys.rustrt.size_of[uint]() * 4u; let uint halfbits = sys.rustrt.size_of[uint]() * 4u;
let uint tmp = n - 1u; let uint tmp = n - 1u;
let uint shift = 1u; let uint shift = 1u;
while (shift <= halfbits) { while (shift <= halfbits) {
tmp |= tmp >> shift; tmp |= tmp >> shift;
shift <<= 1u; shift <<= 1u;
} }
ret tmp + 1u; ret tmp + 1u;
} }
fn to_str(mutable uint n, uint radix) -> str fn to_str(mutable uint n, uint radix) -> str
{ {
check (0u < radix && radix <= 16u); check (0u < radix && radix <= 16u);
fn digit(uint n) -> char { fn digit(uint n) -> char {
alt (n) { alt (n) {
case (0u) { ret '0'; } case (0u) { ret '0'; }
case (1u) { ret '1'; } case (1u) { ret '1'; }
case (2u) { ret '2'; } case (2u) { ret '2'; }
case (3u) { ret '3'; } case (3u) { ret '3'; }
case (4u) { ret '4'; } case (4u) { ret '4'; }
case (5u) { ret '5'; } case (5u) { ret '5'; }
case (6u) { ret '6'; } case (6u) { ret '6'; }
case (7u) { ret '7'; } case (7u) { ret '7'; }
case (8u) { ret '8'; } case (8u) { ret '8'; }
case (9u) { ret '9'; } case (9u) { ret '9'; }
case (10u) { ret 'a'; } case (10u) { ret 'a'; }
case (11u) { ret 'b'; } case (11u) { ret 'b'; }
case (12u) { ret 'c'; } case (12u) { ret 'c'; }
case (13u) { ret 'd'; } case (13u) { ret 'd'; }
case (14u) { ret 'e'; } case (14u) { ret 'e'; }
case (15u) { ret 'f'; } case (15u) { ret 'f'; }
}
} }
}
if (n == 0u) { ret "0"; } if (n == 0u) { ret "0"; }
let uint r = 1u; let uint r = 1u;
if (n > r) { if (n > r) {
while ((r*radix) <= n) { while ((r*radix) <= n) {
r *= radix; r *= radix;
}
} }
}
let str s = ""; let str s = "";
while (n > 0u) { while (n > 0u) {
auto i = n/r; auto i = n/r;
n -= (i * r); n -= (i * r);
r /= radix; r /= radix;
s += digit(i) as u8; s += digit(i) as u8;
} }
while (r > 0u) { while (r > 0u) {
s += '0' as u8; s += '0' as u8;
r /= radix; r /= radix;
} }
ret s; ret s;
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -2,114 +2,123 @@ import vbuf = rustrt.vbuf;
import op = util.operator; import op = util.operator;
native "rust" mod rustrt { native "rust" mod rustrt {
type vbuf; type vbuf;
fn vec_buf[T](vec[T] v, uint offset) -> vbuf; fn vec_buf[T](vec[T] v, uint offset) -> vbuf;
fn vec_len[T](vec[T] v) -> uint; fn vec_len[T](vec[T] v) -> uint;
/** /**
* Sometimes we modify the vec internal data via vec_buf and need to update * Sometimes we modify the vec internal data via vec_buf and need to
* the vec's fill length accordingly. * update the vec's fill length accordingly.
*/ */
fn vec_len_set[T](vec[T] v, uint n); fn vec_len_set[T](vec[T] v, uint n);
/** /**
* The T in vec_alloc[T, U] is the type of the vec to allocate. The * The T in vec_alloc[T, U] is the type of the vec to allocate. The
* U is the type of an element in the vec. So to allocate a vec[U] we * U is the type of an element in the vec. So to allocate a vec[U] we
* want to invoke this as vec_alloc[vec[U], U]. * want to invoke this as vec_alloc[vec[U], U].
*/ */
fn vec_alloc[T, U](uint n_elts) -> vec[U]; fn vec_alloc[T, U](uint n_elts) -> vec[U];
fn refcount[T](vec[T] v) -> uint; fn refcount[T](vec[T] v) -> uint;
fn vec_print_debug_info[T](vec[T] v); fn vec_print_debug_info[T](vec[T] v);
} }
fn alloc[T](uint n_elts) -> vec[T] { fn alloc[T](uint n_elts) -> vec[T] {
ret rustrt.vec_alloc[vec[T], T](n_elts); ret rustrt.vec_alloc[vec[T], T](n_elts);
} }
fn refcount[T](vec[T] v) -> uint { fn refcount[T](vec[T] v) -> uint {
// -1 because calling this function incremented the refcount. // -1 because calling this function incremented the refcount.
ret rustrt.refcount[T](v) - 1u; ret rustrt.refcount[T](v) - 1u;
} }
type init_op[T] = fn(uint i) -> T; type init_op[T] = fn(uint i) -> T;
fn init_fn[T](&init_op[T] op, uint n_elts) -> vec[T] { fn init_fn[T](&init_op[T] op, uint n_elts) -> vec[T] {
let vec[T] v = alloc[T](n_elts); let vec[T] v = alloc[T](n_elts);
let uint i = 0u; let uint i = 0u;
while (i < n_elts) { while (i < n_elts) {
v += vec(op(i)); v += vec(op(i));
i += 1u; i += 1u;
} }
ret v; ret v;
} }
fn init_elt[T](&T t, uint n_elts) -> vec[T] { fn init_elt[T](&T t, uint n_elts) -> vec[T] {
/** /**
* FIXME (issue #81): should be: * FIXME (issue #81): should be:
* *
* fn elt_op[T](&T x, uint i) -> T { ret x; } * fn elt_op[T](&T x, uint i) -> T { ret x; }
* let init_op[T] inner = bind elt_op[T](t, _); * let init_op[T] inner = bind elt_op[T](t, _);
* ret init_fn[T](inner, n_elts); * ret init_fn[T](inner, n_elts);
*/ */
let vec[T] v = alloc[T](n_elts); let vec[T] v = alloc[T](n_elts);
let uint i = n_elts; let uint i = n_elts;
while (i > 0u) { while (i > 0u) {
i -= 1u; i -= 1u;
v += vec(t); v += vec(t);
} }
ret v; ret v;
} }
fn buf[T](vec[T] v) -> vbuf { fn buf[T](vec[T] v) -> vbuf {
ret rustrt.vec_buf[T](v, 0u); ret rustrt.vec_buf[T](v, 0u);
} }
fn len[T](vec[T] v) -> uint { fn len[T](vec[T] v) -> uint {
ret rustrt.vec_len[T](v); ret rustrt.vec_len[T](v);
} }
fn len_set[T](vec[T] v, uint n) { fn len_set[T](vec[T] v, uint n) {
rustrt.vec_len_set[T](v, n); rustrt.vec_len_set[T](v, n);
} }
fn buf_off[T](vec[T] v, uint offset) -> vbuf { fn buf_off[T](vec[T] v, uint offset) -> vbuf {
check (offset < len[T](v)); check (offset < len[T](v));
ret rustrt.vec_buf[T](v, offset); ret rustrt.vec_buf[T](v, offset);
} }
fn print_debug_info[T](vec[T] v) { fn print_debug_info[T](vec[T] v) {
rustrt.vec_print_debug_info[T](v); rustrt.vec_print_debug_info[T](v);
} }
// Returns elements from [start..end) from v. // Returns elements from [start..end) from v.
fn slice[T](vec[T] v, int start, int end) -> vec[T] { fn slice[T](vec[T] v, int start, int end) -> vec[T] {
check (0 <= start); check (0 <= start);
check (start <= end); check (start <= end);
check (end <= (len[T](v) as int)); check (end <= (len[T](v) as int));
auto result = alloc[T]((end - start) as uint); auto result = alloc[T]((end - start) as uint);
let int i = start; let int i = start;
while (i < end) { while (i < end) {
result += vec(v.(i)); result += vec(v.(i));
i += 1; i += 1;
} }
ret result; ret result;
} }
fn grow[T](&mutable vec[T] v, int n, &T initval) { fn grow[T](&mutable vec[T] v, int n, &T initval) {
let int i = n; let int i = n;
while (i > 0) { while (i > 0) {
i -= 1; i -= 1;
v += vec(initval); v += vec(initval);
} }
} }
fn map[T, U](&op[T,U] f, &vec[T] v) -> vec[U] { fn map[T, U](&op[T,U] f, &vec[T] v) -> vec[U] {
let vec[U] u = alloc[U](len[T](v)); let vec[U] u = alloc[U](len[T](v));
for (T ve in v) { for (T ve in v) {
u += vec(f(ve)); u += vec(f(ve));
} }
ret u; ret u;
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -8,33 +8,33 @@
import std._vec; import std._vec;
native "rust" mod rustrt { native "rust" mod rustrt {
fn debug_tydesc[T](); fn debug_tydesc[T]();
fn debug_opaque[T](&T x); fn debug_opaque[T](&T x);
fn debug_box[T](@T x); fn debug_box[T](@T x);
fn debug_tag[T](&T x); fn debug_tag[T](&T x);
fn debug_obj[T](&T x, uint nmethods, uint nbytes); fn debug_obj[T](&T x, uint nmethods, uint nbytes);
fn debug_fn[T](&T x); fn debug_fn[T](&T x);
fn debug_ptrcast[T, U](@T x) -> @U; fn debug_ptrcast[T, U](@T x) -> @U;
} }
fn debug_vec[T](vec[T] v) { fn debug_vec[T](vec[T] v) {
_vec.print_debug_info[T](v); _vec.print_debug_info[T](v);
} }
fn debug_tydesc[T]() { fn debug_tydesc[T]() {
rustrt.debug_tydesc[T](); rustrt.debug_tydesc[T]();
} }
fn debug_opaque[T](&T x) { fn debug_opaque[T](&T x) {
rustrt.debug_opaque[T](x); rustrt.debug_opaque[T](x);
} }
fn debug_box[T](@T x) { fn debug_box[T](@T x) {
rustrt.debug_box[T](x); rustrt.debug_box[T](x);
} }
fn debug_tag[T](&T x) { fn debug_tag[T](&T x) {
rustrt.debug_tag[T](x); rustrt.debug_tag[T](x);
} }
/** /**
@ -47,13 +47,22 @@ fn debug_tag[T](&T x) {
* the front of any obj's data tuple.x * the front of any obj's data tuple.x
*/ */
fn debug_obj[T](&T x, uint nmethods, uint nbytes) { fn debug_obj[T](&T x, uint nmethods, uint nbytes) {
rustrt.debug_obj[T](x, nmethods, nbytes); rustrt.debug_obj[T](x, nmethods, nbytes);
} }
fn debug_fn[T](&T x) { fn debug_fn[T](&T x) {
rustrt.debug_fn[T](x); rustrt.debug_fn[T](x);
} }
fn ptr_cast[T, U](@T x) -> @U { fn ptr_cast[T, U](@T x) -> @U {
ret rustrt.debug_ptrcast[T, U](x); ret rustrt.debug_ptrcast[T, U](x);
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -7,132 +7,142 @@ import std._vec;
import std._int; import std._int;
type t[T] = obj { type t[T] = obj {
fn size() -> uint; fn size() -> uint;
fn add_front(&T t); fn add_front(&T t);
fn add_back(&T t); fn add_back(&T t);
fn pop_front() -> T; fn pop_front() -> T;
fn pop_back() -> T; fn pop_back() -> T;
fn peek_front() -> T; fn peek_front() -> T;
fn peek_back() -> T; fn peek_back() -> T;
fn get(int i) -> T; fn get(int i) -> T;
}; };
fn create[T]() -> t[T] { fn create[T]() -> t[T] {
type cell[T] = mutable util.option[T]; type cell[T] = mutable util.option[T];
let uint initial_capacity = 32u; // 2^5 let uint initial_capacity = 32u; // 2^5
/**
* Grow is only called on full elts, so nelts is also len(elts), unlike
* elsewhere.
*/
fn grow[T](uint nelts, uint lo, vec[cell[T]] elts) -> vec[cell[T]] {
check (nelts == _vec.len[cell[T]](elts));
fn fill[T](uint i, uint nelts, uint lo, &vec[cell[T]] old) -> cell[T] {
if (i < nelts) {
ret old.((lo + i) % nelts);
} else {
ret util.none[T];
}
}
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);
}
fn get[T](vec[cell[T]] elts, uint i) -> T {
alt (elts.(i)) {
case (util.some[T](?t)) { ret t; }
case (_) { fail; }
}
}
obj deque[T](mutable uint nelts,
mutable uint lo,
mutable uint hi,
mutable vec[cell[T]] elts)
{
fn size() -> uint { ret nelts; }
fn add_front(&T t) {
let uint oldlo = lo;
if (lo == 0u) {
lo = _vec.len[cell[T]](elts) - 1u;
} else {
lo -= 1u;
}
if (lo == hi) {
elts = grow[T](nelts, oldlo, elts);
lo = _vec.len[cell[T]](elts) - 1u;
hi = nelts;
}
elts.(lo) = util.some[T](t);
nelts += 1u;
}
fn add_back(&T t) {
if (lo == hi && nelts != 0u) {
elts = grow[T](nelts, lo, elts);
lo = 0u;
hi = nelts;
}
elts.(hi) = util.some[T](t);
hi = (hi + 1u) % _vec.len[cell[T]](elts);
nelts += 1u;
}
/** /**
* We actually release (turn to none()) the T we're popping so that we * Grow is only called on full elts, so nelts is also len(elts), unlike
* don't keep anyone's refcount up unexpectedly. * elsewhere.
*/ */
fn pop_front() -> T { fn grow[T](uint nelts, uint lo, vec[cell[T]] elts) -> vec[cell[T]] {
let T t = get[T](elts, lo); check (nelts == _vec.len[cell[T]](elts));
elts.(lo) = util.none[T];
lo = (lo + 1u) % _vec.len[cell[T]](elts); fn fill[T](uint i, uint nelts, uint lo,
nelts -= 1u; &vec[cell[T]] old) -> cell[T] {
ret t; if (i < nelts) {
ret old.((lo + i) % nelts);
} else {
ret util.none[T];
}
}
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);
} }
fn pop_back() -> T { fn get[T](vec[cell[T]] elts, uint i) -> T {
if (hi == 0u) { alt (elts.(i)) {
hi = _vec.len[cell[T]](elts) - 1u; case (util.some[T](?t)) { ret t; }
} else { case (_) { fail; }
hi -= 1u; }
}
let T t = get[T](elts, hi);
elts.(hi) = util.none[T];
nelts -= 1u;
ret t;
} }
fn peek_front() -> T { obj deque[T](mutable uint nelts,
ret get[T](elts, lo); mutable uint lo,
} mutable uint hi,
mutable vec[cell[T]] elts)
{
fn size() -> uint { ret nelts; }
fn peek_back() -> T { fn add_front(&T t) {
ret get[T](elts, hi - 1u); let uint oldlo = lo;
}
fn get(int i) -> T { if (lo == 0u) {
let uint idx = (lo + (i as uint)) % _vec.len[cell[T]](elts); lo = _vec.len[cell[T]](elts) - 1u;
ret get[T](elts, idx); } else {
} lo -= 1u;
}
} if (lo == hi) {
let vec[cell[T]] v = _vec.init_elt[cell[T]](util.none[T], elts = grow[T](nelts, oldlo, elts);
initial_capacity); lo = _vec.len[cell[T]](elts) - 1u;
hi = nelts;
}
ret deque[T](0u, 0u, 0u, v); elts.(lo) = util.some[T](t);
nelts += 1u;
}
fn add_back(&T t) {
if (lo == hi && nelts != 0u) {
elts = grow[T](nelts, lo, elts);
lo = 0u;
hi = nelts;
}
elts.(hi) = util.some[T](t);
hi = (hi + 1u) % _vec.len[cell[T]](elts);
nelts += 1u;
}
/**
* We actually release (turn to none()) the T we're popping so
* that we don't keep anyone's refcount up unexpectedly.
*/
fn pop_front() -> T {
let T t = get[T](elts, lo);
elts.(lo) = util.none[T];
lo = (lo + 1u) % _vec.len[cell[T]](elts);
nelts -= 1u;
ret t;
}
fn pop_back() -> T {
if (hi == 0u) {
hi = _vec.len[cell[T]](elts) - 1u;
} else {
hi -= 1u;
}
let T t = get[T](elts, hi);
elts.(hi) = util.none[T];
nelts -= 1u;
ret t;
}
fn peek_front() -> T {
ret get[T](elts, lo);
}
fn peek_back() -> T {
ret get[T](elts, hi - 1u);
}
fn get(int i) -> T {
let uint idx = (lo + (i as uint)) % _vec.len[cell[T]](elts);
ret get[T](elts, idx);
}
}
let vec[cell[T]] v = _vec.init_elt[cell[T]](util.none[T],
initial_capacity);
ret deque[T](0u, 0u, 0u, v);
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -3,38 +3,48 @@ import _vec.vbuf;
native mod libc = "libc.so.6" { native mod libc = "libc.so.6" {
fn open(sbuf s, int flags, uint mode) -> int; fn open(sbuf s, int flags, uint mode) -> int;
fn read(int fd, vbuf buf, uint count) -> int; fn read(int fd, vbuf buf, uint count) -> int;
fn write(int fd, vbuf buf, uint count) -> int; fn write(int fd, vbuf buf, uint count) -> int;
fn close(int fd) -> int; fn close(int fd) -> int;
type FILE; type FILE;
fn fopen(sbuf path, sbuf mode) -> FILE; fn fopen(sbuf path, sbuf mode) -> FILE;
fn fclose(FILE f); fn fclose(FILE f);
fn fgetc(FILE f) -> int; fn fgetc(FILE f) -> int;
fn ungetc(int c, FILE f); fn ungetc(int c, FILE f);
type dir; type dir;
// readdir is a mess; handle via wrapper function in rustrt. // readdir is a mess; handle via wrapper function in rustrt.
fn opendir(sbuf d) -> dir; fn opendir(sbuf d) -> dir;
fn closedir(dir d) -> int; fn closedir(dir d) -> int;
fn getenv(sbuf n) -> sbuf; fn getenv(sbuf n) -> sbuf;
fn setenv(sbuf n, sbuf v, int overwrite) -> int; fn setenv(sbuf n, sbuf v, int overwrite) -> int;
fn unsetenv(sbuf n) -> int; fn unsetenv(sbuf n) -> int;
} }
mod libc_constants { mod libc_constants {
fn O_RDONLY() -> int { ret 0x0000; } fn O_RDONLY() -> int { ret 0x0000; }
fn O_WRONLY() -> int { ret 0x0001; } fn O_WRONLY() -> int { ret 0x0001; }
fn O_RDWR() -> int { ret 0x0002; } fn O_RDWR() -> int { ret 0x0002; }
fn O_APPEND() -> int { ret 0x0400; } fn O_APPEND() -> int { ret 0x0400; }
fn O_CREAT() -> int { ret 0x0040; } fn O_CREAT() -> int { ret 0x0040; }
fn O_EXCL() -> int { ret 0x0080; } fn O_EXCL() -> int { ret 0x0080; }
fn O_TRUNC() -> int { ret 0x0200; } fn O_TRUNC() -> int { ret 0x0200; }
fn O_TEXT() -> int { ret 0x0000; } // nonexistent in linux libc fn O_TEXT() -> int { ret 0x0000; } // nonexistent in linux libc
fn O_BINARY() -> int { ret 0x0000; } // nonexistent in linux libc fn O_BINARY() -> int { ret 0x0000; } // nonexistent in linux libc
fn S_IRUSR() -> uint { ret 0x0100u; } fn S_IRUSR() -> uint { ret 0x0100u; }
fn S_IWUSR() -> uint { ret 0x0080u; } fn S_IWUSR() -> uint { ret 0x0080u; }
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -3,38 +3,47 @@ import _vec.vbuf;
native mod libc = "libc.dylib" { native mod libc = "libc.dylib" {
fn open(sbuf s, int flags, uint mode) -> int; fn open(sbuf s, int flags, uint mode) -> int;
fn read(int fd, vbuf buf, uint count) -> int; fn read(int fd, vbuf buf, uint count) -> int;
fn write(int fd, vbuf buf, uint count) -> int; fn write(int fd, vbuf buf, uint count) -> int;
fn close(int fd) -> int; fn close(int fd) -> int;
type FILE; type FILE;
fn fopen(sbuf path, sbuf mode) -> FILE; fn fopen(sbuf path, sbuf mode) -> FILE;
fn fclose(FILE f); fn fclose(FILE f);
fn fgetc(FILE f) -> int; fn fgetc(FILE f) -> int;
fn ungetc(int c, FILE f); fn ungetc(int c, FILE f);
type dir; type dir;
// readdir is a mess; handle via wrapper function in rustrt. // readdir is a mess; handle via wrapper function in rustrt.
fn opendir(sbuf d) -> dir; fn opendir(sbuf d) -> dir;
fn closedir(dir d) -> int; fn closedir(dir d) -> int;
fn getenv(sbuf n) -> sbuf; fn getenv(sbuf n) -> sbuf;
fn setenv(sbuf n, sbuf v, int overwrite) -> int; fn setenv(sbuf n, sbuf v, int overwrite) -> int;
fn unsetenv(sbuf n) -> int; fn unsetenv(sbuf n) -> int;
} }
mod libc_constants { mod libc_constants {
fn O_RDONLY() -> int { ret 0x0000; } fn O_RDONLY() -> int { ret 0x0000; }
fn O_WRONLY() -> int { ret 0x0001; } fn O_WRONLY() -> int { ret 0x0001; }
fn O_RDWR() -> int { ret 0x0002; } fn O_RDWR() -> int { ret 0x0002; }
fn O_APPEND() -> int { ret 0x0008; } fn O_APPEND() -> int { ret 0x0008; }
fn O_CREAT() -> int { ret 0x0200; } fn O_CREAT() -> int { ret 0x0200; }
fn O_EXCL() -> int { ret 0x0800; } fn O_EXCL() -> int { ret 0x0800; }
fn O_TRUNC() -> int { ret 0x0400; } fn O_TRUNC() -> int { ret 0x0400; }
fn O_TEXT() -> int { ret 0x0000; } // nonexistent in darwin libc fn O_TEXT() -> int { ret 0x0000; } // nonexistent in darwin libc
fn O_BINARY() -> int { ret 0x0000; } // nonexistent in darwin libc fn O_BINARY() -> int { ret 0x0000; } // nonexistent in darwin libc
fn S_IRUSR() -> uint { ret 0x0400u; } fn S_IRUSR() -> uint { ret 0x0400u; }
fn S_IWUSR() -> uint { ret 0x0200u; } fn S_IWUSR() -> uint { ret 0x0200u; }
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -13,206 +13,223 @@ type hashfn[K] = fn(&K) -> uint;
type eqfn[K] = fn(&K, &K) -> bool; type eqfn[K] = fn(&K, &K) -> bool;
type hashmap[K, V] = obj { type hashmap[K, V] = obj {
fn size() -> uint; fn size() -> uint;
fn insert(&K key, &V val) -> bool; fn insert(&K key, &V val) -> bool;
fn contains_key(&K key) -> bool; fn contains_key(&K key) -> bool;
fn get(&K key) -> V; fn get(&K key) -> V;
fn find(&K key) -> util.option[V]; fn find(&K key) -> util.option[V];
fn remove(&K key) -> util.option[V]; fn remove(&K key) -> util.option[V];
fn rehash(); fn rehash();
}; };
fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] { fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
let uint initial_capacity = 32u; // 2^5 let uint initial_capacity = 32u; // 2^5
let util.rational load_factor = rec(num=3, den=4); let util.rational load_factor = rec(num=3, den=4);
tag bucket[K, V] { tag bucket[K, V] {
nil; nil;
deleted; deleted;
some(K, V); some(K, V);
} }
fn make_buckets[K, V](uint nbkts) -> vec[mutable bucket[K, V]] { fn make_buckets[K, V](uint nbkts) -> vec[mutable bucket[K, V]] {
ret _vec.init_elt[mutable bucket[K, V]](nil[K, V], nbkts); ret _vec.init_elt[mutable bucket[K, V]](nil[K, V], nbkts);
} }
// Derive two hash functions from the one given by taking the upper // Derive two hash functions from the one given by taking the upper
// half and lower half of the uint bits. Our bucket probing // half and lower half of the uint bits. Our bucket probing
// sequence is then defined by // sequence is then defined by
// //
// hash(key, i) := hashl(key) + i * hashr(key) for i = 0, 1, 2, ... // hash(key, i) := hashl(key) + i * hashr(key) for i = 0, 1, 2, ...
// //
// Tearing the hash function apart this way is kosher in practice // Tearing the hash function apart this way is kosher in practice
// as, assuming 32-bit uints, the table would have to be at 2^32 // as, assuming 32-bit uints, the table would have to be at 2^32
// buckets before the resulting pair of hash functions no longer // buckets before the resulting pair of hash functions no longer
// probes all buckets for a fixed key. Note that hashr is made to // probes all buckets for a fixed key. Note that hashr is made to
// output odd numbers (hence coprime to the number of nbkts, which // output odd numbers (hence coprime to the number of nbkts, which
// is always a power of 2), so that all buckets are probed for a // is always a power of 2), so that all buckets are probed for a
// fixed key. // fixed key.
fn hashl[K](&hashfn[K] hasher, uint nbkts, &K key) -> uint { fn hashl[K](&hashfn[K] hasher, uint nbkts, &K key) -> uint {
ret (hasher(key) >>> (sys.rustrt.size_of[uint]() * 8u / 2u)); ret (hasher(key) >>> (sys.rustrt.size_of[uint]() * 8u / 2u));
} }
fn hashr[K](&hashfn[K] hasher, uint nbkts, &K key) -> uint { fn hashr[K](&hashfn[K] hasher, uint nbkts, &K key) -> uint {
ret ((((~ 0u) >>> (sys.rustrt.size_of[uint]() * 8u / 2u)) ret ((((~ 0u) >>> (sys.rustrt.size_of[uint]() * 8u / 2u))
& hasher(key)) * 2u + 1u); & hasher(key)) * 2u + 1u);
} }
fn hash[K](&hashfn[K] hasher, uint nbkts, &K key, uint i) -> uint { fn hash[K](&hashfn[K] hasher, uint nbkts, &K key, uint i) -> uint {
ret (hashl[K](hasher, nbkts, key) ret (hashl[K](hasher, nbkts, key)
+ i * hashr[K](hasher, nbkts, key)) % nbkts; + i * hashr[K](hasher, nbkts, key)) % nbkts;
} }
/** /**
* We attempt to never call this with a full table. If we do, it * We attempt to never call this with a full table. If we do, it
* will fail. * will fail.
*/ */
fn insert_common[K, V](&hashfn[K] hasher, fn insert_common[K, V](&hashfn[K] hasher,
&eqfn[K] eqer,
vec[mutable bucket[K, V]] bkts,
uint nbkts,
&K key,
&V val)
-> bool
{
let uint i = 0u;
while (i < nbkts) {
let uint j = hash[K](hasher, nbkts, key, i);
alt (bkts.(j)) {
case (some[K, V](?k, _)) {
if (eqer(key, k)) {
bkts.(j) = some[K, V](k, val);
ret false;
}
i += 1u;
}
case (_) {
bkts.(j) = some[K, V](key, val);
ret true;
}
}
}
fail; // full table
}
fn find_common[K, V](&hashfn[K] hasher,
&eqfn[K] eqer, &eqfn[K] eqer,
vec[mutable bucket[K, V]] bkts, vec[mutable bucket[K, V]] bkts,
uint nbkts, uint nbkts,
&K key, &K key)
&V val) -> util.option[V]
-> bool {
{ let uint i = 0u;
let uint i = 0u; while (i < nbkts) {
while (i < nbkts) { let uint j = (hash[K](hasher, nbkts, key, i));
let uint j = hash[K](hasher, nbkts, key, i); alt (bkts.(j)) {
alt (bkts.(j)) { case (some[K, V](?k, ?v)) {
case (some[K, V](?k, _)) { if (eqer(key, k)) {
if (eqer(key, k)) { ret util.some[V](v);
bkts.(j) = some[K, V](k, val); }
ret false; }
} case (nil[K, V]) {
i += 1u; ret util.none[V];
} }
case (_) { case (deleted[K, V]) { }
bkts.(j) = some[K, V](key, val); }
ret true; i += 1u;
}
}
}
fail; // full table
}
fn find_common[K, V](&hashfn[K] hasher,
&eqfn[K] eqer,
vec[mutable bucket[K, V]] bkts,
uint nbkts,
&K key)
-> util.option[V]
{
let uint i = 0u;
while (i < nbkts) {
let uint j = (hash[K](hasher, nbkts, key, i));
alt (bkts.(j)) {
case (some[K, V](?k, ?v)) {
if (eqer(key, k)) {
ret util.some[V](v);
}
}
case (nil[K, V]) {
ret util.none[V];
}
case (deleted[K, V]) { }
}
i += 1u;
}
ret util.none[V];
}
fn rehash[K, V](&hashfn[K] hasher,
&eqfn[K] eqer,
vec[mutable bucket[K, V]] oldbkts, uint noldbkts,
vec[mutable bucket[K, V]] newbkts, uint nnewbkts)
{
for (bucket[K, V] b in oldbkts) {
alt (b) {
case (some[K, V](?k, ?v)) {
insert_common[K, V](hasher, eqer, newbkts, nnewbkts, k, v);
}
case (_) { }
}
}
}
obj hashmap[K, V](hashfn[K] hasher,
eqfn[K] eqer,
mutable vec[mutable bucket[K, V]] bkts,
mutable uint nbkts,
mutable uint nelts,
util.rational lf)
{
fn size() -> uint { ret nelts; }
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 = _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);
bkts = newbkts;
nbkts = nnewbkts;
}
if (insert_common[K, V](hasher, eqer, bkts, nbkts, key, val)) {
nelts += 1u;
ret true;
}
ret false;
}
fn contains_key(&K key) -> bool {
alt (find_common[K, V](hasher, eqer, bkts, nbkts, key)) {
case (util.some[V](_)) { ret true; }
case (_) { ret false; }
}
}
fn get(&K key) -> V {
alt (find_common[K, V](hasher, eqer, bkts, nbkts, key)) {
case (util.some[V](?val)) { ret val; }
case (_) { fail; }
}
}
fn find(&K key) -> util.option[V] {
be find_common[K, V](hasher, eqer, bkts, nbkts, key);
}
fn remove(&K key) -> util.option[V] {
let uint i = 0u;
while (i < nbkts) {
let uint j = (hash[K](hasher, nbkts, key, i));
alt (bkts.(j)) {
case (some[K, V](?k, ?v)) {
if (eqer(key, k)) {
bkts.(j) = deleted[K, V];
nelts -= 1u;
ret util.some[V](v);
} }
}
case (deleted[K, V]) { }
case (nil[K, V]) {
ret util.none[V]; ret util.none[V];
}
} }
i += 1u;
}
ret util.none[V];
}
fn rehash() {
let vec[mutable bucket[K, V]] newbkts = make_buckets[K, V](nbkts);
rehash[K, V](hasher, eqer, bkts, nbkts, newbkts, nbkts);
bkts = newbkts;
}
}
let vec[mutable bucket[K, V]] bkts = make_buckets[K, V](initial_capacity); fn rehash[K, V](&hashfn[K] hasher,
&eqfn[K] eqer,
vec[mutable bucket[K, V]] oldbkts, uint noldbkts,
vec[mutable bucket[K, V]] newbkts, uint nnewbkts)
{
for (bucket[K, V] b in oldbkts) {
alt (b) {
case (some[K, V](?k, ?v)) {
insert_common[K, V](hasher, eqer, newbkts,
nnewbkts, k, v);
}
case (_) { }
}
}
}
ret hashmap[K, V](hasher, eqer, bkts, initial_capacity, 0u, load_factor); obj hashmap[K, V](hashfn[K] hasher,
eqfn[K] eqer,
mutable vec[mutable bucket[K, V]] bkts,
mutable uint nbkts,
mutable uint nelts,
util.rational lf)
{
fn size() -> uint { ret nelts; }
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 = _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);
bkts = newbkts;
nbkts = nnewbkts;
}
if (insert_common[K, V](hasher, eqer, bkts,
nbkts, key, val)) {
nelts += 1u;
ret true;
}
ret false;
}
fn contains_key(&K key) -> bool {
alt (find_common[K, V](hasher, eqer, bkts, nbkts, key)) {
case (util.some[V](_)) { ret true; }
case (_) { ret false; }
}
}
fn get(&K key) -> V {
alt (find_common[K, V](hasher, eqer, bkts, nbkts, key)) {
case (util.some[V](?val)) { ret val; }
case (_) { fail; }
}
}
fn find(&K key) -> util.option[V] {
be find_common[K, V](hasher, eqer, bkts, nbkts, key);
}
fn remove(&K key) -> util.option[V] {
let uint i = 0u;
while (i < nbkts) {
let uint j = (hash[K](hasher, nbkts, key, i));
alt (bkts.(j)) {
case (some[K, V](?k, ?v)) {
if (eqer(key, k)) {
bkts.(j) = deleted[K, V];
nelts -= 1u;
ret util.some[V](v);
}
}
case (deleted[K, V]) { }
case (nil[K, V]) {
ret util.none[V];
}
}
i += 1u;
}
ret util.none[V];
}
fn rehash() {
let vec[mutable bucket[K, V]] newbkts =
make_buckets[K, V](nbkts);
rehash[K, V](hasher, eqer, bkts, nbkts, newbkts, nbkts);
bkts = newbkts;
}
}
let vec[mutable bucket[K, V]] bkts =
make_buckets[K, V](initial_capacity);
ret hashmap[K, V](hasher, eqer, bkts, initial_capacity, 0u, load_factor);
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -3,23 +3,32 @@
*/ */
native "rust" mod rustrt { native "rust" mod rustrt {
type rctx; type rctx;
fn rand_new() -> rctx; fn rand_new() -> rctx;
fn rand_next(rctx c) -> u32; fn rand_next(rctx c) -> u32;
fn rand_free(rctx c); fn rand_free(rctx c);
} }
type rng = obj { fn next() -> u32; }; type rng = obj { fn next() -> u32; };
fn mk_rng() -> rng { fn mk_rng() -> rng {
obj rt_rng(rustrt.rctx c) { obj rt_rng(rustrt.rctx c) {
fn next() -> u32 { fn next() -> u32 {
ret rustrt.rand_next(c); ret rustrt.rand_next(c);
}
drop {
rustrt.rand_free(c);
}
} }
drop {
rustrt.rand_free(c);
}
}
ret rt_rng(rustrt.rand_new()); ret rt_rng(rustrt.rand_new());
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -38,17 +38,26 @@ auth rand.mk_rng = unsafe;
// Target-OS module. // Target-OS module.
alt (target_os) { alt (target_os) {
case ("win32") { case ("win32") {
mod os = "win32_os.rs"; mod os = "win32_os.rs";
} case ("macos") { } case ("macos") {
mod os = "macos_os.rs"; mod os = "macos_os.rs";
} else { } else {
mod os = "linux_os.rs"; mod os = "linux_os.rs";
} }
} }
// FIXME: parametric // FIXME: parametric
mod map; mod map;
mod deque; mod deque;
mod rand; mod rand;
mod dbg; mod dbg;
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -2,21 +2,29 @@ export rustrt;
native "rust" mod rustrt { native "rust" mod rustrt {
// Explicitly re-export native stuff we want to be made // Explicitly re-export native stuff we want to be made
// available outside this crate. Otherwise it's // available outside this crate. Otherwise it's
// visible-in-crate, but not re-exported. // visible-in-crate, but not re-exported.
export last_os_error; export last_os_error;
export size_of; export size_of;
export align_of; export align_of;
export refcount; export refcount;
export gc; export gc;
fn last_os_error() -> str; fn last_os_error() -> str;
fn size_of[T]() -> uint; fn size_of[T]() -> uint;
fn align_of[T]() -> uint; fn align_of[T]() -> uint;
fn refcount[T](@T t) -> uint; fn refcount[T](@T t) -> uint;
fn gc(); fn gc();
fn unsupervise(); fn unsupervise();
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -1,23 +1,23 @@
tag option[T] { tag option[T] {
none; none;
some(T); some(T);
} }
type operator[T, U] = fn(&T) -> U; type operator[T, U] = fn(&T) -> U;
fn option_map[T, U](&operator[T, U] f, &option[T] opt) -> option[U] { fn option_map[T, U](&operator[T, U] f, &option[T] opt) -> option[U] {
alt (opt) { alt (opt) {
case (some[T](?x)) { case (some[T](?x)) {
ret some[U](f(x)); ret some[U](f(x));
}
case (none[T]) {
ret none[U];
}
} }
case (none[T]) {
ret none[U];
}
}
} }
fn id[T](&T x) -> T { fn id[T](&T x) -> T {
ret x; ret x;
} }
/* FIXME (issue #141): See test/run-pass/constrained-type.rs. Uncomment /* FIXME (issue #141): See test/run-pass/constrained-type.rs. Uncomment
@ -25,6 +25,15 @@ fn id[T](&T x) -> T {
type rational = rec(int num, int den); // : _int.positive(*.den); type rational = rec(int num, int den); // : _int.positive(*.den);
fn rational_leq(&rational x, &rational y) -> bool { fn rational_leq(&rational x, &rational y) -> bool {
// NB: Uses the fact that rationals have positive denominators WLOG. // NB: Uses the fact that rationals have positive denominators WLOG.
ret x.num * y.den <= y.num * x.den; ret x.num * y.den <= y.num * x.den;
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View file

@ -2,29 +2,38 @@ import _str.sbuf;
import _vec.vbuf; import _vec.vbuf;
native mod libc = "msvcrt.dll" { native mod libc = "msvcrt.dll" {
fn open(sbuf s, int flags, uint mode) -> int = "_open"; fn open(sbuf s, int flags, uint mode) -> int = "_open";
fn read(int fd, vbuf buf, uint count) -> int = "_read"; fn read(int fd, vbuf buf, uint count) -> int = "_read";
fn write(int fd, vbuf buf, uint count) -> int = "_write"; fn write(int fd, vbuf buf, uint count) -> int = "_write";
fn close(int fd) -> int = "_close"; fn close(int fd) -> int = "_close";
type FILE; type FILE;
fn fopen(sbuf path, sbuf mode) -> FILE; fn fopen(sbuf path, sbuf mode) -> FILE;
fn fclose(FILE f); fn fclose(FILE f);
fn fgetc(FILE f) -> int; fn fgetc(FILE f) -> int;
fn ungetc(int c, FILE f); fn ungetc(int c, FILE f);
} }
mod libc_constants { mod libc_constants {
fn O_RDONLY() -> int { ret 0x0000; } fn O_RDONLY() -> int { ret 0x0000; }
fn O_WRONLY() -> int { ret 0x0001; } fn O_WRONLY() -> int { ret 0x0001; }
fn O_RDWR() -> int { ret 0x0002; } fn O_RDWR() -> int { ret 0x0002; }
fn O_APPEND() -> int { ret 0x0400; } fn O_APPEND() -> int { ret 0x0400; }
fn O_CREAT() -> int { ret 0x0040; } fn O_CREAT() -> int { ret 0x0040; }
fn O_EXCL() -> int { ret 0x0080; } fn O_EXCL() -> int { ret 0x0080; }
fn O_TRUNC() -> int { ret 0x0200; } fn O_TRUNC() -> int { ret 0x0200; }
fn O_TEXT() -> int { ret 0x4000; } fn O_TEXT() -> int { ret 0x4000; }
fn O_BINARY() -> int { ret 0x8000; } fn O_BINARY() -> int { ret 0x8000; }
fn S_IRUSR() -> uint { ret 0x0100u; } // really _S_IREAD in win32 fn S_IRUSR() -> uint { ret 0x0100u; } // really _S_IREAD in win32
fn S_IWUSR() -> uint { ret 0x0080u; } // really _S_IWRITE in win32 fn S_IWUSR() -> uint { ret 0x0080u; } // really _S_IWRITE in win32
} }
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End: