Port the stdlib to the typaram foo<T> syntax.
This commit is contained in:
committed by
Graydon Hoare
parent
8043788e3a
commit
f764f9a8cf
@@ -15,11 +15,11 @@ native "rust" mod rustrt {
|
||||
fn aio_init();
|
||||
fn aio_run();
|
||||
fn aio_stop();
|
||||
fn aio_connect(host: *u8, port: int, connected: &_chan[socket]);
|
||||
fn aio_serve(host: *u8, port: int, acceptChan: &_chan[socket]) -> server;
|
||||
fn aio_writedata(s: socket, buf: *u8, size: uint, status: &_chan[bool]);
|
||||
fn aio_read(s: socket, reader: &_chan[[u8]]);
|
||||
fn aio_close_server(s: server, status: &_chan[bool]);
|
||||
fn aio_connect(host: *u8, port: int, connected: &_chan<socket>);
|
||||
fn aio_serve(host: *u8, port: int, acceptChan: &_chan<socket>) -> server;
|
||||
fn aio_writedata(s: socket, buf: *u8, size: uint, status: &_chan<bool>);
|
||||
fn aio_read(s: socket, reader: &_chan<[u8]>);
|
||||
fn aio_close_server(s: server, status: &_chan<bool>);
|
||||
fn aio_close_socket(s: socket);
|
||||
fn aio_is_null_client(s: socket) -> bool;
|
||||
}
|
||||
@@ -40,36 +40,36 @@ tag socket_event {
|
||||
}
|
||||
|
||||
tag server_event {
|
||||
pending(_chan[_chan[socket_event]]);
|
||||
pending(_chan<_chan<socket_event>>);
|
||||
}
|
||||
|
||||
tag request {
|
||||
quit;
|
||||
connect(pending_connection,_chan[socket_event]);
|
||||
serve(net::ip_addr,int,_chan[server_event],_chan[server]);
|
||||
write(client,[u8],_chan[bool]);
|
||||
close_server(server, _chan[bool]);
|
||||
connect(pending_connection,_chan<socket_event>);
|
||||
serve(net::ip_addr,int,_chan<server_event>,_chan<server>);
|
||||
write(client,[u8],_chan<bool>);
|
||||
close_server(server, _chan<bool>);
|
||||
close_client(client);
|
||||
}
|
||||
|
||||
type ctx = _chan[request];
|
||||
type ctx = _chan<request>;
|
||||
|
||||
fn ip_to_sbuf(ip: net::ip_addr) -> *u8 {
|
||||
vec::to_ptr(str::bytes(net::format_addr(ip)))
|
||||
}
|
||||
|
||||
fn connect_task(ip: net::ip_addr, portnum: int, evt: _chan[socket_event]) {
|
||||
let connecter: _port[client] = mk_port();
|
||||
fn connect_task(ip: net::ip_addr, portnum: int, evt: _chan<socket_event>) {
|
||||
let connecter: _port<client> = mk_port();
|
||||
rustrt::aio_connect(ip_to_sbuf(ip), portnum, connecter.mk_chan());
|
||||
let client = connecter.recv();
|
||||
new_client(client, evt);
|
||||
}
|
||||
|
||||
fn new_client(client: client, evt: _chan[socket_event]) {
|
||||
fn new_client(client: client, evt: _chan<socket_event>) {
|
||||
// Start the read before notifying about the connect. This avoids a race
|
||||
// condition where the receiver can close the socket before we start
|
||||
// reading.
|
||||
let reader: _port[[u8]] = mk_port();
|
||||
let reader: _port<[u8]> = mk_port();
|
||||
rustrt::aio_read(client, reader.mk_chan());
|
||||
|
||||
send(evt, connected(client));
|
||||
@@ -92,18 +92,18 @@ fn new_client(client: client, evt: _chan[socket_event]) {
|
||||
log "close message sent";
|
||||
}
|
||||
|
||||
fn accept_task(client: client, events: _chan[server_event]) {
|
||||
fn accept_task(client: client, events: _chan<server_event>) {
|
||||
log "accept task was spawned";
|
||||
let p: _port[_chan[socket_event]] = mk_port();
|
||||
let p: _port<_chan<socket_event>> = mk_port();
|
||||
send(events, pending(p.mk_chan()));
|
||||
let evt = p.recv();
|
||||
new_client(client, evt);
|
||||
log "done accepting";
|
||||
}
|
||||
|
||||
fn server_task(ip: net::ip_addr, portnum: int, events: _chan[server_event],
|
||||
server: _chan[server]) {
|
||||
let accepter: _port[client] = mk_port();
|
||||
fn server_task(ip: net::ip_addr, portnum: int, events: _chan<server_event>,
|
||||
server: _chan<server>) {
|
||||
let accepter: _port<client> = mk_port();
|
||||
send(server, rustrt::aio_serve(ip_to_sbuf(ip), portnum,
|
||||
accepter.mk_chan()));
|
||||
|
||||
@@ -120,9 +120,9 @@ fn server_task(ip: net::ip_addr, portnum: int, events: _chan[server_event],
|
||||
}
|
||||
}
|
||||
|
||||
fn request_task(c: _chan[ctx]) {
|
||||
fn request_task(c: _chan<ctx>) {
|
||||
// Create a port to accept IO requests on
|
||||
let p: _port[request] = mk_port();
|
||||
let p: _port<request> = mk_port();
|
||||
// Hand of its channel to our spawner
|
||||
send(c, p.mk_chan());
|
||||
log "uv run task spawned";
|
||||
@@ -160,7 +160,7 @@ fn request_task(c: _chan[ctx]) {
|
||||
}
|
||||
}
|
||||
|
||||
fn iotask(c: _chan[ctx]) {
|
||||
fn iotask(c: _chan<ctx>) {
|
||||
log "io task spawned";
|
||||
// Initialize before accepting requests
|
||||
rustrt::aio_init();
|
||||
@@ -178,7 +178,7 @@ fn iotask(c: _chan[ctx]) {
|
||||
}
|
||||
|
||||
fn new() -> ctx {
|
||||
let p: _port[ctx] = mk_port();
|
||||
let p: _port<ctx> = mk_port();
|
||||
task::_spawn(bind iotask(p.mk_chan()));
|
||||
ret p.recv();
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ obj _port[~T](raw_port : @port_ptr) {
|
||||
}
|
||||
}
|
||||
|
||||
fn mk_port[~T]() -> _port[T] {
|
||||
fn mk_port[~T]() -> _port<T> {
|
||||
_port(@port_ptr(rustrt::new_port(sys::size_of[T]())))
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@ type t[T] =
|
||||
fn get(int) -> T ;
|
||||
};
|
||||
|
||||
fn create[@T]() -> t[T] {
|
||||
type cell[T] = option::t[T];
|
||||
fn create[@T]() -> t<T> {
|
||||
type cell[T] = option::t<T>;
|
||||
|
||||
let initial_capacity: uint = 32u; // 2^5
|
||||
/**
|
||||
@@ -26,8 +26,8 @@ fn create[@T]() -> t[T] {
|
||||
*/
|
||||
|
||||
|
||||
fn grow[@T](nelts: uint, lo: uint, elts: &[mutable cell[T]]) ->
|
||||
[mutable cell[T]] {
|
||||
fn grow[@T](nelts: uint, lo: uint, elts: &[mutable cell<T>]) ->
|
||||
[mutable cell<T>] {
|
||||
assert (nelts == vec::len(elts));
|
||||
let rv = ~[mutable];
|
||||
|
||||
@@ -42,22 +42,22 @@ fn create[@T]() -> t[T] {
|
||||
|
||||
ret rv;
|
||||
}
|
||||
fn get[@T](elts: &[mutable cell[T]], i: uint) -> T {
|
||||
fn get[@T](elts: &[mutable cell<T>], i: uint) -> T {
|
||||
ret alt elts.(i) { option::some(t) { t } _ { fail } };
|
||||
}
|
||||
obj deque[@T](mutable nelts: uint,
|
||||
mutable lo: uint,
|
||||
mutable hi: uint,
|
||||
mutable elts: [mutable cell[T]]) {
|
||||
mutable elts: [mutable cell<T>]) {
|
||||
fn size() -> uint { ret nelts; }
|
||||
fn add_front(t: &T) {
|
||||
let oldlo: uint = lo;
|
||||
if lo == 0u {
|
||||
lo = vec::len[cell[T]](elts) - 1u;
|
||||
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;
|
||||
lo = vec::len[cell<T>](elts) - 1u;
|
||||
hi = nelts;
|
||||
}
|
||||
elts.(lo) = option::some[T](t);
|
||||
@@ -70,7 +70,7 @@ fn create[@T]() -> t[T] {
|
||||
hi = nelts;
|
||||
}
|
||||
elts.(hi) = option::some[T](t);
|
||||
hi = (hi + 1u) % vec::len[cell[T]](elts);
|
||||
hi = (hi + 1u) % vec::len[cell<T>](elts);
|
||||
nelts += 1u;
|
||||
}
|
||||
|
||||
@@ -81,13 +81,13 @@ fn create[@T]() -> t[T] {
|
||||
fn pop_front() -> T {
|
||||
let t: T = get[T](elts, lo);
|
||||
elts.(lo) = option::none[T];
|
||||
lo = (lo + 1u) % vec::len[cell[T]](elts);
|
||||
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;
|
||||
hi = vec::len[cell<T>](elts) - 1u;
|
||||
} else { hi -= 1u; }
|
||||
let t: T = get[T](elts, hi);
|
||||
elts.(hi) = option::none[T];
|
||||
@@ -97,11 +97,11 @@ fn create[@T]() -> t[T] {
|
||||
fn peek_front() -> T { ret get[T](elts, lo); }
|
||||
fn peek_back() -> T { ret get[T](elts, hi - 1u); }
|
||||
fn get(i: int) -> T {
|
||||
let idx: uint = (lo + (i as uint)) % vec::len[cell[T]](elts);
|
||||
let idx: uint = (lo + (i as uint)) % vec::len[cell<T>](elts);
|
||||
ret get[T](elts, idx);
|
||||
}
|
||||
}
|
||||
let v: [mutable cell[T]] =
|
||||
let v: [mutable cell<T>] =
|
||||
vec::init_elt_mut(option::none, initial_capacity);
|
||||
ret deque[T](0u, 0u, 0u, v);
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ fn doc_at(data: &@[u8], start: uint) -> doc {
|
||||
ret {data: data, start: elt_size.next, end: end};
|
||||
}
|
||||
|
||||
fn maybe_get_doc(d: doc, tg: uint) -> option::t[doc] {
|
||||
fn maybe_get_doc(d: doc, tg: uint) -> option::t<doc> {
|
||||
let pos = d.start;
|
||||
while pos < d.end {
|
||||
let elt_tag = vint_at(*d.data, pos);
|
||||
|
||||
@@ -6,30 +6,30 @@ import option::none;
|
||||
tag t[T, U] { left(T); right(U); }
|
||||
|
||||
fn either[T, U, V](f_left: &block(&T) -> V, f_right: &block(&U) -> V,
|
||||
value: &t[T, U]) -> V {
|
||||
value: &t<T, U>) -> V {
|
||||
alt value { left(l) { f_left(l) } right(r) { f_right(r) } }
|
||||
}
|
||||
|
||||
fn lefts[T, U](eithers: &[t[T, U]]) -> [T] {
|
||||
fn lefts[T, U](eithers: &[t<T, U>]) -> [T] {
|
||||
let result: [T] = ~[];
|
||||
for elt: t[T, U] in eithers {
|
||||
for elt: t<T, U> in eithers {
|
||||
alt elt { left(l) { result += ~[l] } _ {/* fallthrough */ } }
|
||||
}
|
||||
ret result;
|
||||
}
|
||||
|
||||
fn rights[T, U](eithers: &[t[T, U]]) -> [U] {
|
||||
fn rights[T, U](eithers: &[t<T, U>]) -> [U] {
|
||||
let result: [U] = ~[];
|
||||
for elt: t[T, U] in eithers {
|
||||
for elt: t<T, U> in eithers {
|
||||
alt elt { right(r) { result += ~[r] } _ {/* fallthrough */ } }
|
||||
}
|
||||
ret result;
|
||||
}
|
||||
|
||||
fn partition[T, U](eithers: &[t[T, U]]) -> {lefts: [T], rights: [U]} {
|
||||
fn partition[T, U](eithers: &[t<T, U>]) -> {lefts: [T], rights: [U]} {
|
||||
let lefts: [T] = ~[];
|
||||
let rights: [U] = ~[];
|
||||
for elt: t[T, U] in eithers {
|
||||
for elt: t<T, U> in eithers {
|
||||
alt elt { left(l) { lefts += ~[l] } right(r) { rights += ~[r] } }
|
||||
}
|
||||
ret {lefts: lefts, rights: rights};
|
||||
|
||||
@@ -60,7 +60,7 @@ mod ct {
|
||||
|
||||
// A formatted conversion from an expression to a string
|
||||
type conv =
|
||||
{param: option::t[int],
|
||||
{param: option::t<int>,
|
||||
flags: [flag],
|
||||
width: count,
|
||||
precision: count,
|
||||
@@ -105,7 +105,7 @@ mod ct {
|
||||
ret pieces;
|
||||
}
|
||||
fn peek_num(s: str, i: uint, lim: uint) ->
|
||||
option::t[{num: uint, next: uint}] {
|
||||
option::t<{num: uint, next: uint}> {
|
||||
if i >= lim { ret none; }
|
||||
let c = s.(i);
|
||||
if !('0' as u8 <= c && c <= '9' as u8) { ret option::none; }
|
||||
@@ -135,7 +135,7 @@ mod ct {
|
||||
next: ty.next};
|
||||
}
|
||||
fn parse_parameter(s: str, i: uint, lim: uint) ->
|
||||
{param: option::t[int], next: uint} {
|
||||
{param: option::t<int>, next: uint} {
|
||||
if i >= lim { ret {param: none, next: i}; }
|
||||
let num = peek_num(s, i, lim);
|
||||
ret alt num {
|
||||
|
||||
@@ -3,7 +3,7 @@ import str::sbuf;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[cfg(target_os = "macos")]
|
||||
fn getenv(n: str) -> option::t[str] {
|
||||
fn getenv(n: str) -> option::t<str> {
|
||||
let s = os::libc::getenv(str::buf(n));
|
||||
ret if s as int == 0 {
|
||||
option::none[str]
|
||||
@@ -19,7 +19,7 @@ fn setenv(n: str, v: str) {
|
||||
}
|
||||
|
||||
#[cfg(target_os = "win32")]
|
||||
fn getenv(n: str) -> option::t[str] {
|
||||
fn getenv(n: str) -> option::t<str> {
|
||||
let nbuf = str::buf(n);
|
||||
let nsize = 256u;
|
||||
while true {
|
||||
|
||||
@@ -75,7 +75,7 @@ fn name_str(nm: name) -> str {
|
||||
ret alt nm { short(ch) { str::from_char(ch) } long(s) { s } };
|
||||
}
|
||||
|
||||
fn find_opt(opts: &[opt], nm: name) -> option::t[uint] {
|
||||
fn find_opt(opts: &[opt], nm: name) -> option::t<uint> {
|
||||
let i = 0u;
|
||||
let l = vec::len[opt](opts);
|
||||
while i < l { if opts.(i).name == nm { ret some[uint](i); } i += 1u; }
|
||||
@@ -224,7 +224,7 @@ fn opt_strs(m: &match, nm: str) -> [str] {
|
||||
ret acc;
|
||||
}
|
||||
|
||||
fn opt_maybe_str(m: &match, nm: str) -> option::t[str] {
|
||||
fn opt_maybe_str(m: &match, nm: str) -> option::t<str> {
|
||||
let vals = opt_vals(m, nm);
|
||||
if vec::len[optval](vals) == 0u { ret none[str]; }
|
||||
ret alt vals.(0) { val(s) { some[str](s) } _ { none[str] } };
|
||||
@@ -234,7 +234,7 @@ fn opt_maybe_str(m: &match, nm: str) -> option::t[str] {
|
||||
/// Returns none if the option was not present, `def` if the option was
|
||||
/// present but no argument was provided, and the argument if the option was
|
||||
/// present and an argument was provided.
|
||||
fn opt_default(m: &match, nm: str, def: str) -> option::t[str] {
|
||||
fn opt_default(m: &match, nm: str, def: str) -> option::t<str> {
|
||||
let vals = opt_vals(m, nm);
|
||||
if vec::len[optval](vals) == 0u { ret none[str]; }
|
||||
ret alt vals.(0) { val(s) { some[str](s) } _ { some[str](def) } }
|
||||
|
||||
@@ -59,7 +59,7 @@ resource FILE_res(f: os::libc::FILE) {
|
||||
os::libc::fclose(f);
|
||||
}
|
||||
|
||||
obj FILE_buf_reader(f: os::libc::FILE, res: option::t[@FILE_res]) {
|
||||
obj FILE_buf_reader(f: os::libc::FILE, res: option::t<@FILE_res>) {
|
||||
fn read(len: uint) -> [u8] {
|
||||
let buf = ~[];
|
||||
vec::reserve[u8](buf, len);
|
||||
@@ -243,7 +243,7 @@ type buf_writer =
|
||||
fn tell() -> uint ;
|
||||
};
|
||||
|
||||
obj FILE_writer(f: os::libc::FILE, res: option::t[@FILE_res]) {
|
||||
obj FILE_writer(f: os::libc::FILE, res: option::t<@FILE_res>) {
|
||||
fn write(v: &[u8]) {
|
||||
let len = vec::len[u8](v);
|
||||
let vbuf = vec::to_ptr[u8](v);
|
||||
@@ -262,7 +262,7 @@ resource fd_res(fd: int) {
|
||||
os::libc::close(fd);
|
||||
}
|
||||
|
||||
obj fd_buf_writer(fd: int, res: option::t[@fd_res]) {
|
||||
obj fd_buf_writer(fd: int, res: option::t<@fd_res>) {
|
||||
fn write(v: &[u8]) {
|
||||
let len = vec::len[u8](v);
|
||||
let count = 0u;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import option::some;
|
||||
import option::none;
|
||||
|
||||
tag list[T] { cons(T, @list[T]); nil; }
|
||||
tag list[T] { cons(T, @list<T>); nil; }
|
||||
|
||||
fn from_vec[@T](v: &[T]) -> list[T] {
|
||||
fn from_vec[@T](v: &[T]) -> list<T> {
|
||||
let l = nil[T];
|
||||
// FIXME: This would be faster and more space efficient if it looped over
|
||||
// a reverse vector iterator. Unfortunately generic iterators seem not to
|
||||
@@ -13,7 +13,7 @@ fn from_vec[@T](v: &[T]) -> list[T] {
|
||||
ret l;
|
||||
}
|
||||
|
||||
fn foldl[@T, @U](ls_: &list[T], u: &U, f: &block(&T, &U) -> U ) -> U {
|
||||
fn foldl[@T, @U](ls_: &list<T>, u: &U, f: &block(&T, &U) -> U ) -> U {
|
||||
let accum: U = u;
|
||||
let ls = ls_;
|
||||
while true {
|
||||
@@ -25,8 +25,8 @@ fn foldl[@T, @U](ls_: &list[T], u: &U, f: &block(&T, &U) -> U ) -> U {
|
||||
ret accum;
|
||||
}
|
||||
|
||||
fn find[@T, @U](ls_: &list[T], f: &block(&T) -> option::t[U])
|
||||
-> option::t[U] {
|
||||
fn find[@T, @U](ls_: &list<T>, f: &block(&T) -> option::t<U>)
|
||||
-> option::t<U> {
|
||||
let ls = ls_;
|
||||
while true {
|
||||
alt ls {
|
||||
@@ -39,7 +39,7 @@ fn find[@T, @U](ls_: &list[T], f: &block(&T) -> option::t[U])
|
||||
ret none;
|
||||
}
|
||||
|
||||
fn has[@T](ls_: &list[T], elt: &T) -> bool {
|
||||
fn has[@T](ls_: &list<T>, elt: &T) -> bool {
|
||||
let ls = ls_;
|
||||
while true {
|
||||
alt ls {
|
||||
@@ -50,26 +50,26 @@ fn has[@T](ls_: &list[T], elt: &T) -> bool {
|
||||
ret false;
|
||||
}
|
||||
|
||||
fn length[@T](ls: &list[T]) -> uint {
|
||||
fn length[@T](ls: &list<T>) -> uint {
|
||||
fn count[T](t: &T, u: &uint) -> uint { ret u + 1u; }
|
||||
ret foldl(ls, 0u, count);
|
||||
}
|
||||
|
||||
fn cdr[@T](ls: &list[T]) -> list[T] {
|
||||
fn cdr[@T](ls: &list<T>) -> list<T> {
|
||||
alt ls {
|
||||
cons(_, tl) { ret *tl; }
|
||||
nil. { fail "list empty" }
|
||||
}
|
||||
}
|
||||
|
||||
fn car[@T](ls: &list[T]) -> T {
|
||||
fn car[@T](ls: &list<T>) -> T {
|
||||
alt ls {
|
||||
cons(hd, _) { ret hd; }
|
||||
nil. { fail "list empty" }
|
||||
}
|
||||
}
|
||||
|
||||
fn append[@T](l: &list[T], m: &list[T]) -> list[T] {
|
||||
fn append[@T](l: &list<T>, m: &list<T>) -> list<T> {
|
||||
alt l {
|
||||
nil. { ret m; }
|
||||
cons(x, xs) {
|
||||
|
||||
@@ -11,23 +11,23 @@ type hashmap[K, V] =
|
||||
fn insert(&K, &V) -> bool ;
|
||||
fn contains_key(&K) -> bool ;
|
||||
fn get(&K) -> V ;
|
||||
fn find(&K) -> option::t[V] ;
|
||||
fn remove(&K) -> option::t[V] ;
|
||||
fn find(&K) -> option::t<V> ;
|
||||
fn remove(&K) -> option::t<V> ;
|
||||
fn rehash() ;
|
||||
iter items() -> @{key: K, val: V} ;
|
||||
iter keys() -> K ;
|
||||
};
|
||||
type hashset[K] = hashmap[K, ()];
|
||||
type hashset[K] = hashmap<K, ()>;
|
||||
|
||||
fn set_add[@K](set: hashset[K], key: &K) -> bool { ret set.insert(key, ()); }
|
||||
fn set_add[@K](set: hashset<K>, key: &K) -> bool { ret set.insert(key, ()); }
|
||||
|
||||
fn mk_hashmap[@K, @V](hasher: &hashfn[K], eqer: &eqfn[K]) -> hashmap[K, V] {
|
||||
fn mk_hashmap[@K, @V](hasher: &hashfn<K>, eqer: &eqfn<K>) -> hashmap<K, V> {
|
||||
let initial_capacity: uint = 32u; // 2^5
|
||||
|
||||
let load_factor: util::rational = {num: 3, den: 4};
|
||||
tag bucket[@K, @V] { nil; deleted; some(K, V); }
|
||||
fn make_buckets[@K, @V](nbkts: uint) -> [mutable (bucket[K, V])] {
|
||||
ret vec::init_elt_mut[bucket[K, V]](nil[K, V], nbkts);
|
||||
fn make_buckets[@K, @V](nbkts: uint) -> [mutable (bucket<K, V>)] {
|
||||
ret vec::init_elt_mut[bucket<K, V>](nil[K, V], nbkts);
|
||||
}
|
||||
// Derive two hash functions from the one given by taking the upper
|
||||
// half and lower half of the uint bits. Our bucket probing
|
||||
@@ -53,8 +53,8 @@ fn mk_hashmap[@K, @V](hasher: &hashfn[K], eqer: &eqfn[K]) -> hashmap[K, V] {
|
||||
* will fail.
|
||||
*/
|
||||
|
||||
fn insert_common[@K, @V](hasher: &hashfn[K], eqer: &eqfn[K],
|
||||
bkts: &[mutable bucket[K, V]], nbkts: uint,
|
||||
fn insert_common[@K, @V](hasher: &hashfn<K>, eqer: &eqfn<K>,
|
||||
bkts: &[mutable bucket<K, V>], nbkts: uint,
|
||||
key: &K, val: &V) -> bool {
|
||||
let i: uint = 0u;
|
||||
let h: uint = hasher(key);
|
||||
@@ -76,9 +76,9 @@ fn mk_hashmap[@K, @V](hasher: &hashfn[K], eqer: &eqfn[K]) -> hashmap[K, V] {
|
||||
}
|
||||
fail; // full table
|
||||
}
|
||||
fn find_common[@K, @V](hasher: &hashfn[K], eqer: &eqfn[K],
|
||||
bkts: &[mutable bucket[K, V]], nbkts: uint,
|
||||
key: &K) -> option::t[V] {
|
||||
fn find_common[@K, @V](hasher: &hashfn<K>, eqer: &eqfn<K>,
|
||||
bkts: &[mutable bucket<K, V>], nbkts: uint,
|
||||
key: &K) -> option::t<V> {
|
||||
let i: uint = 0u;
|
||||
let h: uint = hasher(key);
|
||||
while i < nbkts {
|
||||
@@ -97,10 +97,10 @@ fn mk_hashmap[@K, @V](hasher: &hashfn[K], eqer: &eqfn[K]) -> hashmap[K, V] {
|
||||
}
|
||||
ret option::none;
|
||||
}
|
||||
fn rehash[@K, @V](hasher: &hashfn[K], eqer: &eqfn[K],
|
||||
oldbkts: &[mutable bucket[K, V]], noldbkts: uint,
|
||||
newbkts: &[mutable bucket[K, V]], nnewbkts: uint) {
|
||||
for b: bucket[K, V] in oldbkts {
|
||||
fn rehash[@K, @V](hasher: &hashfn<K>, eqer: &eqfn<K>,
|
||||
oldbkts: &[mutable bucket<K, V>], noldbkts: uint,
|
||||
newbkts: &[mutable bucket<K, V>], nnewbkts: uint) {
|
||||
for b: bucket<K, V> in oldbkts {
|
||||
alt b {
|
||||
some(k_, v_) {
|
||||
let k = k_;
|
||||
@@ -111,9 +111,9 @@ fn mk_hashmap[@K, @V](hasher: &hashfn[K], eqer: &eqfn[K]) -> hashmap[K, V] {
|
||||
}
|
||||
}
|
||||
}
|
||||
obj hashmap[@K, @V](hasher: hashfn[K],
|
||||
eqer: eqfn[K],
|
||||
mutable bkts: [mutable bucket[K, V]],
|
||||
obj hashmap[@K, @V](hasher: hashfn<K>,
|
||||
eqer: eqfn<K>,
|
||||
mutable bkts: [mutable bucket<K, V>],
|
||||
mutable nbkts: uint,
|
||||
mutable nelts: uint,
|
||||
lf: util::rational) {
|
||||
@@ -146,10 +146,10 @@ fn mk_hashmap[@K, @V](hasher: &hashfn[K], eqer: &eqfn[K]) -> hashmap[K, V] {
|
||||
_ { fail }
|
||||
};
|
||||
}
|
||||
fn find(key: &K) -> option::t[V] {
|
||||
fn find(key: &K) -> option::t<V> {
|
||||
be find_common(hasher, eqer, bkts, nbkts, key);
|
||||
}
|
||||
fn remove(key: &K) -> option::t[V] {
|
||||
fn remove(key: &K) -> option::t<V> {
|
||||
let i: uint = 0u;
|
||||
let h: uint = hasher(key);
|
||||
while i < nbkts {
|
||||
@@ -177,12 +177,12 @@ fn mk_hashmap[@K, @V](hasher: &hashfn[K], eqer: &eqfn[K]) -> hashmap[K, V] {
|
||||
bkts = newbkts;
|
||||
}
|
||||
iter items() -> @{key: K, val: V} {
|
||||
for b: bucket[K, V] in bkts {
|
||||
for b: bucket<K, V> in bkts {
|
||||
alt b { some(k, v) { put @{key: k, val: v}; } _ { } }
|
||||
}
|
||||
}
|
||||
iter keys() -> K {
|
||||
for b: bucket[K, V] in bkts {
|
||||
for b: bucket<K, V> in bkts {
|
||||
alt b { some(k, _) { put k; } _ { } }
|
||||
}
|
||||
}
|
||||
@@ -193,17 +193,17 @@ fn mk_hashmap[@K, @V](hasher: &hashfn[K], eqer: &eqfn[K]) -> hashmap[K, V] {
|
||||
|
||||
// Hash map constructors for basic types
|
||||
|
||||
fn new_str_hash[@V]() -> hashmap[str, V] {
|
||||
fn new_str_hash[@V]() -> hashmap<str, V> {
|
||||
ret mk_hashmap(str::hash, str::eq);
|
||||
}
|
||||
|
||||
fn new_int_hash[@V]() -> hashmap[int, V] {
|
||||
fn new_int_hash[@V]() -> hashmap<int, V> {
|
||||
fn hash_int(x: &int) -> uint { ret x as uint; }
|
||||
fn eq_int(a: &int, b: &int) -> bool { ret a == b; }
|
||||
ret mk_hashmap(hash_int, eq_int);
|
||||
}
|
||||
|
||||
fn new_uint_hash[@V]() -> hashmap[uint, V] {
|
||||
fn new_uint_hash[@V]() -> hashmap<uint, V> {
|
||||
fn hash_uint(x: &uint) -> uint { ret x; }
|
||||
fn eq_uint(a: &uint, b: &uint) -> bool { ret a == b; }
|
||||
ret mk_hashmap(hash_uint, eq_uint);
|
||||
|
||||
@@ -2,33 +2,33 @@
|
||||
|
||||
tag t[@T] { none; some(T); }
|
||||
|
||||
fn get[@T](opt: &t[T]) -> T {
|
||||
fn get[@T](opt: &t<T>) -> T {
|
||||
alt opt {
|
||||
some(x) { x }
|
||||
none. { fail "option none" }
|
||||
}
|
||||
}
|
||||
|
||||
fn map[@T, @U](f: &block(&T) -> U, opt: &t[T]) -> t[U] {
|
||||
fn map[@T, @U](f: &block(&T) -> U, opt: &t<T>) -> t<U> {
|
||||
alt opt { some(x) { some(f(x)) } none. { none } }
|
||||
}
|
||||
|
||||
fn is_none[@T](opt: &t[T]) -> bool {
|
||||
fn is_none[@T](opt: &t<T>) -> bool {
|
||||
alt opt { none. { true } some(_) { false } }
|
||||
}
|
||||
|
||||
fn is_some[@T](opt: &t[T]) -> bool { !is_none(opt) }
|
||||
fn is_some[@T](opt: &t<T>) -> bool { !is_none(opt) }
|
||||
|
||||
fn from_maybe[@T](def: &T, opt: &t[T]) -> T {
|
||||
fn from_maybe[@T](def: &T, opt: &t<T>) -> T {
|
||||
alt opt { some(x) { x } none. { def } }
|
||||
}
|
||||
|
||||
fn maybe[@T, @U](def: &U, f: &block(&T) -> U, opt: &t[T]) -> U {
|
||||
fn maybe[@T, @U](def: &U, f: &block(&T) -> U, opt: &t<T>) -> U {
|
||||
alt opt { none. { def } some(t) { f(t) } }
|
||||
}
|
||||
|
||||
// Can be defined in terms of the above when/if we have const bind.
|
||||
fn may[@T](f: &block(&T), opt: &t[T]) {
|
||||
fn may[@T](f: &block(&T), opt: &t<T>) {
|
||||
alt opt { none. {/* nothing */ } some(t) { f(t); } }
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ fn list_dir(path: str) -> [str] {
|
||||
/*
|
||||
auto dir = os::libc::opendir(str::buf(path));
|
||||
assert (dir as uint != 0u);
|
||||
let vec[str] result = [];
|
||||
let vec<str> result = [];
|
||||
while (true) {
|
||||
auto ent = os::libc::readdir(dir);
|
||||
if (ent as int == 0) {
|
||||
|
||||
@@ -8,9 +8,9 @@ import net;
|
||||
|
||||
type ctx = aio::ctx;
|
||||
type client = { ctx: ctx, client: aio::client,
|
||||
evt: _port[aio::socket_event] };
|
||||
evt: _port<aio::socket_event> };
|
||||
type server = { ctx: ctx, server: aio::server,
|
||||
evt: _port[aio::server_event] };
|
||||
evt: _port<aio::server_event> };
|
||||
|
||||
fn new() -> ctx {
|
||||
ret aio::new();
|
||||
@@ -20,7 +20,7 @@ fn destroy(ctx: ctx) {
|
||||
send(ctx, aio::quit);
|
||||
}
|
||||
|
||||
fn make_socket(ctx: ctx, p: _port[aio::socket_event]) -> client {
|
||||
fn make_socket(ctx: ctx, p: _port<aio::socket_event>) -> client {
|
||||
let evt: aio::socket_event = p.recv();
|
||||
alt evt {
|
||||
aio::connected(client) {
|
||||
@@ -31,7 +31,7 @@ fn make_socket(ctx: ctx, p: _port[aio::socket_event]) -> client {
|
||||
}
|
||||
|
||||
fn connect_to(ctx: ctx, ip: net::ip_addr, portnum: int) -> client {
|
||||
let p: _port[aio::socket_event] = mk_port();
|
||||
let p: _port<aio::socket_event> = mk_port();
|
||||
send(ctx, aio::connect(aio::remote(ip, portnum), p.mk_chan()));
|
||||
ret make_socket(ctx, p);
|
||||
}
|
||||
@@ -48,8 +48,8 @@ fn read(c: client) -> [u8] {
|
||||
}
|
||||
|
||||
fn create_server(ctx: ctx, ip: net::ip_addr, portnum: int) -> server {
|
||||
let evt: _port[aio::server_event] = mk_port();
|
||||
let p: _port[aio::server] = mk_port();
|
||||
let evt: _port<aio::server_event> = mk_port();
|
||||
let p: _port<aio::server> = mk_port();
|
||||
send(ctx, aio::serve(ip, portnum,
|
||||
evt.mk_chan(), p.mk_chan()));
|
||||
let srv: aio::server = p.recv();
|
||||
@@ -60,7 +60,7 @@ fn accept_from(server: server) -> client {
|
||||
let evt: aio::server_event = server.evt.recv();
|
||||
alt evt {
|
||||
aio::pending(callback) {
|
||||
let p: _port[aio::socket_event] = mk_port();
|
||||
let p: _port<aio::socket_event> = mk_port();
|
||||
send(callback, p.mk_chan());
|
||||
ret make_socket(server.ctx, p);
|
||||
}
|
||||
@@ -68,14 +68,14 @@ fn accept_from(server: server) -> client {
|
||||
}
|
||||
|
||||
fn write_data(c: client, data: [u8]) -> bool {
|
||||
let p: _port[bool] = mk_port();
|
||||
let p: _port<bool> = mk_port();
|
||||
send(c.ctx, aio::write(c.client, data, p.mk_chan()));
|
||||
ret p.recv();
|
||||
}
|
||||
|
||||
fn close_server(server: server) {
|
||||
// TODO: make this unit once we learn to send those from native code
|
||||
let p: _port[bool] = mk_port();
|
||||
let p: _port<bool> = mk_port();
|
||||
send(server.ctx, aio::close_server(server.server, p.mk_chan()));
|
||||
log "Waiting for close";
|
||||
p.recv();
|
||||
|
||||
@@ -7,38 +7,38 @@ import option::some;
|
||||
|
||||
// FIXME: Should not be @; there's a bug somewhere in rustc that requires this
|
||||
// to be.
|
||||
type smallintmap[T] = @{mutable v: [mutable option::t[T]]};
|
||||
type smallintmap[T] = @{mutable v: [mutable option::t<T>]};
|
||||
|
||||
fn mk[@T]() -> smallintmap[T] {
|
||||
let v: [mutable option::t[T]] = ~[mutable];
|
||||
fn mk[@T]() -> smallintmap<T> {
|
||||
let v: [mutable option::t<T>] = ~[mutable];
|
||||
ret @{mutable v: v};
|
||||
}
|
||||
|
||||
fn insert[@T](m: &smallintmap[T], key: uint, val: &T) {
|
||||
vec::grow_set[option::t[T]](m.v, key, none[T], some[T](val));
|
||||
fn insert[@T](m: &smallintmap<T>, key: uint, val: &T) {
|
||||
vec::grow_set[option::t<T>](m.v, key, none[T], some[T](val));
|
||||
}
|
||||
|
||||
fn find[@T](m: &smallintmap[T], key: uint) -> option::t[T] {
|
||||
if key < vec::len[option::t[T]](m.v) { ret m.v.(key); }
|
||||
fn find[@T](m: &smallintmap<T>, key: uint) -> option::t<T> {
|
||||
if key < vec::len[option::t<T>](m.v) { ret m.v.(key); }
|
||||
ret none[T];
|
||||
}
|
||||
|
||||
fn get[@T](m: &smallintmap[T], key: uint) -> T {
|
||||
fn get[@T](m: &smallintmap<T>, key: uint) -> T {
|
||||
alt find[T](m, key) {
|
||||
none[T]. { log_err "smallintmap::get(): key not present"; fail; }
|
||||
some[T](v) { ret v; }
|
||||
}
|
||||
}
|
||||
|
||||
fn contains_key[@T](m: &smallintmap[T], key: uint) -> bool {
|
||||
fn contains_key[@T](m: &smallintmap<T>, key: uint) -> bool {
|
||||
ret !option::is_none(find[T](m, key));
|
||||
}
|
||||
|
||||
fn truncate[@T](m: &smallintmap[T], len: uint) {
|
||||
m.v = vec::slice_mut[option::t[T]](m.v, 0u, len);
|
||||
fn truncate[@T](m: &smallintmap<T>, len: uint) {
|
||||
m.v = vec::slice_mut[option::t<T>](m.v, 0u, len);
|
||||
}
|
||||
|
||||
fn max_key[T](m: &smallintmap[T]) -> uint {
|
||||
ret vec::len[option::t[T]](m.v);
|
||||
fn max_key[T](m: &smallintmap<T>) -> uint {
|
||||
ret vec::len[option::t<T>](m.v);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ export quick_sort3;
|
||||
|
||||
type lteq[T] = block(&T, &T) -> bool ;
|
||||
|
||||
fn merge_sort[@T](le: <eq[T], v: &[T]) -> [T] {
|
||||
fn merge[@T](le: <eq[T], a: &[T], b: &[T]) -> [T] {
|
||||
fn merge_sort[@T](le: <eq<T>, v: &[T]) -> [T] {
|
||||
fn merge[@T](le: <eq<T>, a: &[T], b: &[T]) -> [T] {
|
||||
let rs: [T] = ~[];
|
||||
let a_len: uint = len[T](a);
|
||||
let a_ix: uint = 0u;
|
||||
@@ -39,7 +39,7 @@ fn swap[@T](arr: &[mutable T], x: uint, y: uint) {
|
||||
arr.(y) = a;
|
||||
}
|
||||
|
||||
fn part[@T](compare_func: <eq[T], arr: &[mutable T], left: uint,
|
||||
fn part[@T](compare_func: <eq<T>, arr: &[mutable T], left: uint,
|
||||
right: uint, pivot: uint) -> uint {
|
||||
let pivot_value = arr.(pivot);
|
||||
swap[T](arr, pivot, right);
|
||||
@@ -56,7 +56,7 @@ fn part[@T](compare_func: <eq[T], arr: &[mutable T], left: uint,
|
||||
ret storage_index;
|
||||
}
|
||||
|
||||
fn qsort[@T](compare_func: <eq[T], arr: &[mutable T], left: uint,
|
||||
fn qsort[@T](compare_func: <eq<T>, arr: &[mutable T], left: uint,
|
||||
right: uint) {
|
||||
if right > left {
|
||||
let pivot = (left + right) / 2u;
|
||||
@@ -69,7 +69,7 @@ fn qsort[@T](compare_func: <eq[T], arr: &[mutable T], left: uint,
|
||||
}
|
||||
}
|
||||
|
||||
fn quick_sort[@T](compare_func: <eq[T], arr: &[mutable T]) {
|
||||
fn quick_sort[@T](compare_func: <eq<T>, arr: &[mutable T]) {
|
||||
if len[T](arr) == 0u { ret; }
|
||||
qsort[T](compare_func, arr, 0u, len[T](arr) - 1u);
|
||||
}
|
||||
@@ -79,7 +79,7 @@ fn quick_sort[@T](compare_func: <eq[T], arr: &[mutable T]) {
|
||||
// http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf
|
||||
// According to these slides this is the algorithm of choice for
|
||||
// 'randomly ordered keys, abstract compare' & 'small number of key values'
|
||||
fn qsort3[@T](compare_func_lt: <eq[T], compare_func_eq: <eq[T],
|
||||
fn qsort3[@T](compare_func_lt: <eq<T>, compare_func_eq: <eq<T>,
|
||||
arr: &[mutable T], left: int, right: int) {
|
||||
if right <= left { ret; }
|
||||
let v: T = arr.(right);
|
||||
@@ -127,7 +127,7 @@ fn qsort3[@T](compare_func_lt: <eq[T], compare_func_eq: <eq[T],
|
||||
qsort3[T](compare_func_lt, compare_func_eq, arr, i, right);
|
||||
}
|
||||
|
||||
fn quick_sort3[@T](compare_func_lt: <eq[T], compare_func_eq: <eq[T],
|
||||
fn quick_sort3[@T](compare_func_lt: <eq<T>, compare_func_eq: <eq<T>,
|
||||
arr: &[mutable T]) {
|
||||
if len[T](arr) == 0u { ret; }
|
||||
qsort3[T](compare_func_lt, compare_func_eq, arr, 0,
|
||||
|
||||
@@ -59,13 +59,13 @@ fn test_main_ivec(args: &[str], tests: &[test_desc]) {
|
||||
if !run_tests_console(opts, tests) { fail "Some tests failed"; }
|
||||
}
|
||||
|
||||
fn test_main(args: &vec[str], tests: &[test_desc]) {
|
||||
fn test_main(args: &vec<str>, tests: &[test_desc]) {
|
||||
test_main_ivec(vec::from_vec(args), tests);
|
||||
}
|
||||
|
||||
type test_opts = {filter: option::t[str], run_ignored: bool};
|
||||
type test_opts = {filter: option::t<str>, run_ignored: bool};
|
||||
|
||||
type opt_res = either::t[test_opts, str];
|
||||
type opt_res = either::t<test_opts, str>;
|
||||
|
||||
// Parses command line arguments into test options
|
||||
fn parse_opts(args: &[str]) : vec::is_not_empty(args) -> opt_res {
|
||||
@@ -268,7 +268,7 @@ fn filter_tests(opts: &test_opts, tests: &[test_desc]) -> [test_desc] {
|
||||
|
||||
let filter =
|
||||
bind fn (test: &test_desc, filter_str: str) ->
|
||||
option::t[test_desc] {
|
||||
option::t<test_desc> {
|
||||
if str::find(test.name, filter_str) >= 0 {
|
||||
ret option::some(test);
|
||||
} else { ret option::none; }
|
||||
@@ -284,7 +284,7 @@ fn filter_tests(opts: &test_opts, tests: &[test_desc]) -> [test_desc] {
|
||||
filtered
|
||||
} else {
|
||||
let filter =
|
||||
fn (test: &test_desc) -> option::t[test_desc] {
|
||||
fn (test: &test_desc) -> option::t<test_desc> {
|
||||
if test.ignore {
|
||||
ret option::some({name: test.name,
|
||||
fn: test.fn,
|
||||
|
||||
@@ -6,7 +6,7 @@ import option::some;
|
||||
// A very naive implementation of union-find with unsigned integer nodes.
|
||||
// Maintains the invariant that the root of a node is always equal to or less
|
||||
// than the node itself.
|
||||
type node = option::t[uint];
|
||||
type node = option::t<uint>;
|
||||
|
||||
type ufind = {mutable nodes: [mutable node]};
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ native "rust" mod rustrt {
|
||||
count: uint);
|
||||
}
|
||||
|
||||
fn from_vec[@T](v: &vec[mutable? T]) -> [T] {
|
||||
fn from_vec[@T](v: &vec<mutable? T>) -> [T] {
|
||||
let iv = ~[];
|
||||
for e in v {
|
||||
iv += ~[e];
|
||||
@@ -38,7 +38,7 @@ fn len[T](v: &[mutable? T]) -> uint { ret rusti::ivec_len(v); }
|
||||
|
||||
type init_op[T] = fn(uint) -> T ;
|
||||
|
||||
fn init_fn[@T](op: &init_op[T], n_elts: uint) -> [T] {
|
||||
fn init_fn[@T](op: &init_op<T>, n_elts: uint) -> [T] {
|
||||
let v = ~[];
|
||||
reserve(v, n_elts);
|
||||
let i: uint = 0u;
|
||||
@@ -47,7 +47,7 @@ fn init_fn[@T](op: &init_op[T], n_elts: uint) -> [T] {
|
||||
}
|
||||
|
||||
// TODO: Remove me once we have slots.
|
||||
fn init_fn_mut[@T](op: &init_op[T], n_elts: uint) -> [mutable T] {
|
||||
fn init_fn_mut[@T](op: &init_op<T>, n_elts: uint) -> [mutable T] {
|
||||
let v = ~[mutable];
|
||||
reserve(v, n_elts);
|
||||
let i: uint = 0u;
|
||||
@@ -104,7 +104,7 @@ fn tail[@T](v: &[mutable? T]) : is_not_empty(v) -> [mutable? T] {
|
||||
}
|
||||
|
||||
/// Returns the last element of `v`.
|
||||
fn last[@T](v: &[mutable? T]) -> option::t[T] {
|
||||
fn last[@T](v: &[mutable? T]) -> option::t<T> {
|
||||
if len(v) == 0u { ret none; }
|
||||
ret some(v.(len(v) - 1u));
|
||||
}
|
||||
@@ -210,7 +210,7 @@ fn map2[@T, @U, @V](f: &block(&T, &U) -> V, v0: &[T], v1: &[U])
|
||||
ret u;
|
||||
}
|
||||
|
||||
fn filter_map[@T, @U](f: &block(&T) -> option::t[U],
|
||||
fn filter_map[@T, @U](f: &block(&T) -> option::t<U>,
|
||||
v: &[mutable? T]) -> [U] {
|
||||
let result = ~[];
|
||||
for elem: T in v {
|
||||
@@ -252,18 +252,18 @@ fn count[T](x: &T, v: &[mutable? T]) -> uint {
|
||||
ret cnt;
|
||||
}
|
||||
|
||||
fn find[@T](f: &block(&T) -> bool , v: &[T]) -> option::t[T] {
|
||||
fn find[@T](f: &block(&T) -> bool, v: &[T]) -> option::t<T> {
|
||||
for elt: T in v { if f(elt) { ret some(elt); } }
|
||||
ret none;
|
||||
}
|
||||
|
||||
fn position[@T](x: &T, v: &[T]) -> option::t[uint] {
|
||||
fn position[@T](x: &T, v: &[T]) -> option::t<uint> {
|
||||
let i: uint = 0u;
|
||||
while i < len(v) { if x == v.(i) { ret some[uint](i); } i += 1u; }
|
||||
ret none[uint];
|
||||
}
|
||||
|
||||
fn position_pred[T](f: fn(&T) -> bool , v: &[T]) -> option::t[uint] {
|
||||
fn position_pred[T](f: fn(&T) -> bool, v: &[T]) -> option::t<uint> {
|
||||
let i: uint = 0u;
|
||||
while i < len(v) { if f(v.(i)) { ret some[uint](i); } i += 1u; }
|
||||
ret none[uint];
|
||||
|
||||
Reference in New Issue
Block a user