1
Fork 0

Port the stdlib to the decl foo<T> syntax.

This commit is contained in:
Erick Tryzelaar 2011-08-12 06:37:10 -07:00 committed by Graydon Hoare
parent 21f46a1655
commit 4c9049c50c
17 changed files with 144 additions and 144 deletions

View file

@ -1,8 +1,8 @@
export ptr_eq;
fn ptr_eq[T](a: &@T, b: &@T) -> bool {
fn ptr_eq<T>(a: &@T, b: &@T) -> bool {
let a_ptr: uint = unsafe::reinterpret_cast(a);
let b_ptr: uint = unsafe::reinterpret_cast(b);
ret a_ptr == b_ptr;
}
}

View file

@ -20,7 +20,7 @@ native "rust" mod rustrt {
fn take_chan(ch : *rust_chan);
fn drop_chan(ch : *rust_chan);
fn chan_send(ch: *rust_chan, v : *void);
fn chan_id_send[~T](target_task : task_id, target_port : port_id,
fn chan_id_send<~T>(target_task : task_id, target_port : port_id,
data : -T);
fn new_port(unit_sz : uint) -> *rust_port;
@ -30,12 +30,12 @@ native "rust" mod rustrt {
}
native "rust-intrinsic" mod rusti {
fn recv[~T](port : *rustrt::rust_port) -> T;
fn recv<~T>(port : *rustrt::rust_port) -> T;
}
type port_id = int;
type _chan[~T] = {
type _chan<~T> = {
task : task_id,
port : port_id
};
@ -45,9 +45,9 @@ resource port_ptr(po: *rustrt::rust_port) {
rustrt::del_port(po);
}
obj _port[~T](raw_port : @port_ptr) {
obj _port<~T>(raw_port : @port_ptr) {
// FIXME: rename this to chan once chan is not a keyword.
fn mk_chan() -> _chan[T] {
fn mk_chan() -> _chan<T> {
{
task: task::get_task_id(),
port: rustrt::get_port_id(**raw_port)
@ -59,10 +59,10 @@ 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]())))
}
fn send[~T](ch : _chan[T], data : -T) {
fn send<~T>(ch : _chan<T>, data : -T) {
rustrt::chan_id_send(ch.task, ch.port, data);
}

View file

@ -12,23 +12,23 @@
const const_refcount: uint = 0x7bad_face_u;
native "rust" mod rustrt {
fn debug_tydesc[T]();
fn debug_opaque[T](x: &T);
fn debug_box[T](x: @T);
fn debug_tag[T](x: &T);
fn debug_obj[T](x: &T, nmethods: uint, nbytes: uint);
fn debug_fn[T](x: &T);
fn debug_ptrcast[T, U](x: @T) -> @U;
fn debug_tydesc<T>();
fn debug_opaque<T>(x: &T);
fn debug_box<T>(x: @T);
fn debug_tag<T>(x: &T);
fn debug_obj<T>(x: &T, nmethods: uint, nbytes: uint);
fn debug_fn<T>(x: &T);
fn debug_ptrcast<T, U>(x: @T) -> @U;
fn debug_trap(msg: str);
}
fn debug_tydesc[T]() { rustrt::debug_tydesc[T](); }
fn debug_opaque[T](x: &T) { rustrt::debug_opaque[T](x); }
fn debug_opaque<T>(x: &T) { rustrt::debug_opaque[T](x); }
fn debug_box[T](x: @T) { rustrt::debug_box[T](x); }
fn debug_box<T>(x: @T) { rustrt::debug_box[T](x); }
fn debug_tag[T](x: &T) { rustrt::debug_tag[T](x); }
fn debug_tag<T>(x: &T) { rustrt::debug_tag[T](x); }
/**
@ -40,13 +40,13 @@ fn debug_tag[T](x: &T) { rustrt::debug_tag[T](x); }
* this to at least be 4u, since an implicit captured tydesc pointer sits in
* the front of any obj's data tuple.x
*/
fn debug_obj[T](x: &T, nmethods: uint, nbytes: uint) {
fn debug_obj<T>(x: &T, nmethods: uint, nbytes: uint) {
rustrt::debug_obj[T](x, nmethods, nbytes);
}
fn debug_fn[T](x: &T) { rustrt::debug_fn[T](x); }
fn debug_fn<T>(x: &T) { rustrt::debug_fn[T](x); }
fn ptr_cast[T, U](x: @T) -> @U { ret rustrt::debug_ptrcast[T, U](x); }
fn ptr_cast<T, U>(x: @T) -> @U { ret rustrt::debug_ptrcast[T, U](x); }
fn trap(s: str) { rustrt::debug_trap(s); }
// Local Variables:

View file

@ -4,7 +4,7 @@
/**
* A deque, for fun. Untested as of yet. Likely buggy.
*/
type t[T] =
type t<T> =
obj {
fn size() -> uint ;
fn add_front(&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,7 +26,7 @@ fn create[@T]() -> t<T> {
*/
fn grow[@T](nelts: uint, lo: uint, elts: &[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,10 +42,10 @@ 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,
obj deque<@T>(mutable nelts: uint,
mutable lo: uint,
mutable hi: uint,
mutable elts: [mutable cell<T>]) {

View file

@ -3,14 +3,14 @@ import option;
import option::some;
import option::none;
tag t[T, U] { left(T); right(U); }
tag t<T, U> { left(T); right(U); }
fn either[T, U, V](f_left: &block(&T) -> V, f_right: &block(&U) -> V,
fn either<T, U, V>(f_left: &block(&T) -> V, f_right: &block(&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 {
alt elt { left(l) { result += ~[l] } _ {/* fallthrough */ } }
@ -18,7 +18,7 @@ fn lefts[T, U](eithers: &[t<T, U>]) -> [T] {
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 {
alt elt { right(r) { result += ~[r] } _ {/* fallthrough */ } }
@ -26,7 +26,7 @@ fn rights[T, U](eithers: &[t<T, U>]) -> [U] {
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 {

View file

@ -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,7 +25,7 @@ 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>)
fn find<@T, @U>(ls_: &list<T>, f: &block(&T) -> option::t<U>)
-> option::t<U> {
let ls = ls_;
while true {
@ -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 count[T](t: &T, u: &uint) -> uint { ret u + 1u; }
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) {

View file

@ -1,11 +1,11 @@
/**
* Hashmap implementation.
*/
type hashfn[K] = fn(&K) -> uint ;
type hashfn<K> = fn(&K) -> uint ;
type eqfn[K] = fn(&K, &K) -> bool ;
type eqfn<K> = fn(&K, &K) -> bool ;
type hashmap[K, V] =
type hashmap<K, V> =
obj {
fn size() -> uint ;
fn insert(&K, &V) -> bool ;
@ -17,16 +17,16 @@ type hashmap[K, V] =
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>)] {
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);
}
// Derive two hash functions from the one given by taking the upper
@ -53,7 +53,7 @@ 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>,
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;
@ -76,7 +76,7 @@ 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>,
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;
@ -97,7 +97,7 @@ 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>,
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 {
@ -111,7 +111,7 @@ fn mk_hashmap[@K, @V](hasher: &hashfn<K>, eqer: &eqfn<K>) -> hashmap<K, V> {
}
}
}
obj hashmap[@K, @V](hasher: hashfn<K>,
obj hashmap<@K, @V>(hasher: hashfn<K>,
eqer: eqfn<K>,
mutable bkts: [mutable bucket<K, V>],
mutable nbkts: uint,
@ -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);

View file

@ -1,34 +1,34 @@
// lib/option::rs
tag t[@T] { none; some(T); }
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); } }
}

View file

@ -1,12 +1,12 @@
// Unsafe pointer utility functions.
native "rust-intrinsic" mod rusti {
fn addr_of[T](val: &T) -> *mutable T;
fn ptr_offset[T](ptr: *T, count: uint) -> *T;
fn addr_of<T>(val: &T) -> *mutable T;
fn ptr_offset<T>(ptr: *T, count: uint) -> *T;
}
fn addr_of[T](val: &T) -> *mutable T { ret rusti::addr_of(val); }
fn offset[T](ptr: *T, count: uint) -> *T {
fn addr_of<T>(val: &T) -> *mutable T { ret rusti::addr_of(val); }
fn offset<T>(ptr: *T, count: uint) -> *T {
ret rusti::ptr_offset(ptr, count);
}

View file

@ -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> {
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) {
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> {
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) {
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 {
fn max_key<T>(m: &smallintmap<T>) -> uint {
ret vec::len[option::t<T>](m.v);
}

View file

@ -6,10 +6,10 @@ export merge_sort;
export quick_sort;
export quick_sort3;
type lteq[T] = block(&T, &T) -> bool ;
type lteq<T> = block(&T, &T) -> bool ;
fn merge_sort[@T](le: &lteq<T>, v: &[T]) -> [T] {
fn merge[@T](le: &lteq<T>, a: &[T], b: &[T]) -> [T] {
fn merge_sort<@T>(le: &lteq<T>, v: &[T]) -> [T] {
fn merge<@T>(le: &lteq<T>, a: &[T], b: &[T]) -> [T] {
let rs: [T] = ~[];
let a_len: uint = len[T](a);
let a_ix: uint = 0u;
@ -33,13 +33,13 @@ fn merge_sort[@T](le: &lteq<T>, v: &[T]) -> [T] {
ret merge[T](le, merge_sort[T](le, a), merge_sort[T](le, b));
}
fn swap[@T](arr: &[mutable T], x: uint, y: uint) {
fn swap<@T>(arr: &[mutable T], x: uint, y: uint) {
let a = arr.(x);
arr.(x) = arr.(y);
arr.(y) = a;
}
fn part[@T](compare_func: &lteq<T>, arr: &[mutable T], left: uint,
fn part<@T>(compare_func: &lteq<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: &lteq<T>, arr: &[mutable T], left: uint,
ret storage_index;
}
fn qsort[@T](compare_func: &lteq<T>, arr: &[mutable T], left: uint,
fn qsort<@T>(compare_func: &lteq<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: &lteq<T>, arr: &[mutable T], left: uint,
}
}
fn quick_sort[@T](compare_func: &lteq<T>, arr: &[mutable T]) {
fn quick_sort<@T>(compare_func: &lteq<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: &lteq<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: &lteq<T>, compare_func_eq: &lteq<T>,
fn qsort3<@T>(compare_func_lt: &lteq<T>, compare_func_eq: &lteq<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: &lteq<T>, compare_func_eq: &lteq<T>,
qsort3[T](compare_func_lt, compare_func_eq, arr, i, right);
}
fn quick_sort3[@T](compare_func_lt: &lteq<T>, compare_func_eq: &lteq<T>,
fn quick_sort3<@T>(compare_func_lt: &lteq<T>, compare_func_eq: &lteq<T>,
arr: &[mutable T]) {
if len[T](arr) == 0u { ret; }
qsort3[T](compare_func_lt, compare_func_eq, arr, 0,

View file

@ -65,7 +65,7 @@ native "rust" mod rustrt {
fn str_from_buf(buf: sbuf, len: uint) -> str;
fn str_push_byte(s: str, byte: uint) -> str;
fn str_slice(s: str, begin: uint, end: uint) -> str;
fn refcount[T](s: str) -> uint;
fn refcount<T>(s: str) -> uint;
}
fn eq(a: &str, b: &str) -> bool {

View file

@ -10,9 +10,9 @@ native "rust" mod rustrt {
// available outside this crate. Otherwise it's
// visible-in-crate, but not re-exported.
fn last_os_error() -> str;
fn size_of[T]() -> uint;
fn align_of[T]() -> uint;
fn refcount[T](t: @T) -> uint;
fn size_of<T>() -> uint;
fn align_of<T>() -> uint;
fn refcount<T>(t: @T) -> uint;
fn do_gc();
fn unsupervise();
}

View file

@ -23,7 +23,7 @@ native "rust" mod rustrt {
fn migrate_alloc(alloc : *u8, target : task_id);
fn leak[@T](thing : -T);
fn leak<@T>(thing : -T);
}
type task_id = int;

View file

@ -1,9 +1,9 @@
// Unsafe operations.
native "rust-intrinsic" mod rusti {
fn cast[T, U](src: &T) -> U;
fn cast<T, U>(src: &T) -> U;
}
// Casts the value at `src` to U. The two types must have the same length.
fn reinterpret_cast[T, U](src: &T) -> U { ret rusti::cast(src); }
fn reinterpret_cast<T, U>(src: &T) -> U { ret rusti::cast(src); }

View file

@ -1,6 +1,6 @@
fn id[T](x: &T) -> T { ret x; }
fn id<T>(x: &T) -> T { ret x; }
/* FIXME (issue #141): See test/run-pass/constrained-type.rs. Uncomment

View file

@ -6,18 +6,18 @@ import uint::next_power_of_two;
import ptr::addr_of;
native "rust-intrinsic" mod rusti {
fn ivec_len[T](v: &[T]) -> uint;
fn ivec_len<T>(v: &[T]) -> uint;
}
native "rust" mod rustrt {
fn ivec_reserve_shared[T](v: &mutable [mutable? T], n: uint);
fn ivec_on_heap[T](v: &[T]) -> uint;
fn ivec_to_ptr[T](v: &[T]) -> *T;
fn ivec_copy_from_buf_shared[T](v: &mutable [mutable? T], ptr: *T,
fn ivec_reserve_shared<T>(v: &mutable [mutable? T], n: uint);
fn ivec_on_heap<T>(v: &[T]) -> uint;
fn ivec_to_ptr<T>(v: &[T]) -> *T;
fn ivec_copy_from_buf_shared<T>(v: &mutable [mutable? T], ptr: *T,
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];
@ -26,19 +26,19 @@ fn from_vec[@T](v: &vec<mutable? T>) -> [T] {
}
/// Reserves space for `n` elements in the given vector.
fn reserve[@T](v: &mutable [mutable? T], n: uint) {
fn reserve<@T>(v: &mutable [mutable? T], n: uint) {
rustrt::ivec_reserve_shared(v, n);
}
fn on_heap[T](v: &[T]) -> bool { ret rustrt::ivec_on_heap(v) != 0u; }
fn on_heap<T>(v: &[T]) -> bool { ret rustrt::ivec_on_heap(v) != 0u; }
fn to_ptr[T](v: &[T]) -> *T { ret rustrt::ivec_to_ptr(v); }
fn to_ptr<T>(v: &[T]) -> *T { ret rustrt::ivec_to_ptr(v); }
fn len[T](v: &[mutable? T]) -> uint { ret rusti::ivec_len(v); }
fn len<T>(v: &[mutable? T]) -> uint { ret rusti::ivec_len(v); }
type init_op[T] = fn(uint) -> T ;
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;
@ -55,7 +55,7 @@ fn init_fn_mut[@T](op: &init_op<T>, n_elts: uint) -> [mutable T] {
ret v;
}
fn init_elt[@T](t: &T, n_elts: uint) -> [T] {
fn init_elt<@T>(t: &T, n_elts: uint) -> [T] {
let v = ~[];
reserve(v, n_elts);
let i: uint = 0u;
@ -64,7 +64,7 @@ fn init_elt[@T](t: &T, n_elts: uint) -> [T] {
}
// TODO: Remove me once we have slots.
fn init_elt_mut[@T](t: &T, n_elts: uint) -> [mutable T] {
fn init_elt_mut<@T>(t: &T, n_elts: uint) -> [mutable T] {
let v = ~[mutable];
reserve(v, n_elts);
let i: uint = 0u;
@ -72,45 +72,45 @@ fn init_elt_mut[@T](t: &T, n_elts: uint) -> [mutable T] {
ret v;
}
fn to_mut[@T](v: &[T]) -> [mutable T] {
fn to_mut<@T>(v: &[T]) -> [mutable T] {
let vres = ~[mutable];
for t: T in v { vres += ~[mutable t]; }
ret vres;
}
fn from_mut[@T](v: &[mutable T]) -> [T] {
fn from_mut<@T>(v: &[mutable T]) -> [T] {
let vres = ~[];
for t: T in v { vres += ~[t]; }
ret vres;
}
// Predicates
pred is_empty[T](v: &[mutable? T]) -> bool {
pred is_empty<T>(v: &[mutable? T]) -> bool {
// FIXME: This would be easier if we could just call len
for t: T in v { ret false; }
ret true;
}
pred is_not_empty[T](v: &[mutable? T]) -> bool { ret !is_empty(v); }
pred is_not_empty<T>(v: &[mutable? T]) -> bool { ret !is_empty(v); }
// Accessors
/// Returns the first element of a vector
fn head[@T](v: &[mutable? T]) : is_not_empty(v) -> T { ret v.(0); }
fn head<@T>(v: &[mutable? T]) : is_not_empty(v) -> T { ret v.(0); }
/// Returns all but the first element of a vector
fn tail[@T](v: &[mutable? T]) : is_not_empty(v) -> [mutable? T] {
fn tail<@T>(v: &[mutable? T]) : is_not_empty(v) -> [mutable? T] {
ret slice(v, 1u, len(v));
}
/// 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));
}
/// Returns a copy of the elements from [`start`..`end`) from `v`.
fn slice[@T](v: &[mutable? T], start: uint, end: uint) -> [T] {
fn slice<@T>(v: &[mutable? T], start: uint, end: uint) -> [T] {
assert (start <= end);
assert (end <= len(v));
let result = ~[];
@ -121,7 +121,7 @@ fn slice[@T](v: &[mutable? T], start: uint, end: uint) -> [T] {
}
// TODO: Remove me once we have slots.
fn slice_mut[@T](v: &[mutable? T], start: uint, end: uint) -> [mutable T] {
fn slice_mut<@T>(v: &[mutable? T], start: uint, end: uint) -> [mutable T] {
assert (start <= end);
assert (end <= len(v));
let result = ~[mutable];
@ -134,7 +134,7 @@ fn slice_mut[@T](v: &[mutable? T], start: uint, end: uint) -> [mutable T] {
// Mutators
fn shift[@T](v: &mutable [mutable? T]) -> T {
fn shift<@T>(v: &mutable [mutable? T]) -> T {
let ln = len[T](v);
assert (ln > 0u);
let e = v.(0);
@ -143,7 +143,7 @@ fn shift[@T](v: &mutable [mutable? T]) -> T {
}
// TODO: Write this, unsafely, in a way that's not O(n).
fn pop[@T](v: &mutable [mutable? T]) -> T {
fn pop<@T>(v: &mutable [mutable? T]) -> T {
let ln = len(v);
assert (ln > 0u);
ln -= 1u;
@ -158,14 +158,14 @@ fn pop[@T](v: &mutable [mutable? T]) -> T {
// Appending
/// Expands the given vector in-place by appending `n` copies of `initval`.
fn grow[@T](v: &mutable [T], n: uint, initval: &T) {
fn grow<@T>(v: &mutable [T], n: uint, initval: &T) {
reserve(v, next_power_of_two(len(v) + n));
let i: uint = 0u;
while i < n { v += ~[initval]; i += 1u; }
}
// TODO: Remove me once we have slots.
fn grow_mut[@T](v: &mutable [mutable T], n: uint, initval: &T) {
fn grow_mut<@T>(v: &mutable [mutable T], n: uint, initval: &T) {
reserve(v, next_power_of_two(len(v) + n));
let i: uint = 0u;
while i < n { v += ~[mutable initval]; i += 1u; }
@ -173,7 +173,7 @@ fn grow_mut[@T](v: &mutable [mutable T], n: uint, initval: &T) {
/// Calls `f` `n` times and appends the results of these calls to the given
/// vector.
fn grow_fn[@T](v: &mutable [T], n: uint, init_fn: fn(uint) -> T ) {
fn grow_fn<@T>(v: &mutable [T], n: uint, init_fn: fn(uint) -> T ) {
reserve(v, next_power_of_two(len(v) + n));
let i: uint = 0u;
while i < n { v += ~[init_fn(i)]; i += 1u; }
@ -182,7 +182,7 @@ fn grow_fn[@T](v: &mutable [T], n: uint, init_fn: fn(uint) -> T ) {
/// Sets the element at position `index` to `val`. If `index` is past the end
/// of the vector, expands the vector by replicating `initval` to fill the
/// intervening space.
fn grow_set[@T](v: &mutable [mutable T], index: uint, initval: &T, val: &T) {
fn grow_set<@T>(v: &mutable [mutable T], index: uint, initval: &T, val: &T) {
if index >= len(v) { grow_mut(v, index - len(v) + 1u, initval); }
v.(index) = val;
}
@ -190,7 +190,7 @@ fn grow_set[@T](v: &mutable [mutable T], index: uint, initval: &T, val: &T) {
// Functional utilities
fn map[@T, @U](f: &block(&T) -> U , v: &[mutable? T]) -> [U] {
fn map<@T, @U>(f: &block(&T) -> U , v: &[mutable? T]) -> [U] {
let result = ~[];
reserve(result, len(v));
for elem: T in v {
@ -200,7 +200,7 @@ fn map[@T, @U](f: &block(&T) -> U , v: &[mutable? T]) -> [U] {
ret result;
}
fn map2[@T, @U, @V](f: &block(&T, &U) -> V, v0: &[T], v1: &[U])
fn map2<@T, @U, @V>(f: &block(&T, &U) -> V, v0: &[T], v1: &[U])
-> [V] {
let v0_len = len[T](v0);
if v0_len != len[U](v1) { fail; }
@ -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 {
@ -223,7 +223,7 @@ fn filter_map[@T, @U](f: &block(&T) -> option::t<U>,
ret result;
}
fn foldl[@T, @U](p: &block(&U, &T) -> U , z: &U, v: &[mutable? T]) -> U {
fn foldl<@T, @U>(p: &block(&U, &T) -> U , z: &U, v: &[mutable? T]) -> U {
let sz = len(v);
if sz == 0u { ret z; }
let first = v.(0);
@ -231,45 +231,45 @@ fn foldl[@T, @U](p: &block(&U, &T) -> U , z: &U, v: &[mutable? T]) -> U {
ret p(foldl(p, z, rest), first);
}
fn any[T](f: &block(&T) -> bool , v: &[T]) -> bool {
fn any<T>(f: &block(&T) -> bool, v: &[T]) -> bool {
for elem: T in v { if f(elem) { ret true; } }
ret false;
}
fn all[T](f: &block(&T) -> bool , v: &[T]) -> bool {
fn all<T>(f: &block(&T) -> bool, v: &[T]) -> bool {
for elem: T in v { if !f(elem) { ret false; } }
ret true;
}
fn member[T](x: &T, v: &[T]) -> bool {
fn member<T>(x: &T, v: &[T]) -> bool {
for elt: T in v { if x == elt { ret true; } }
ret false;
}
fn count[T](x: &T, v: &[mutable? T]) -> uint {
fn count<T>(x: &T, v: &[mutable? T]) -> uint {
let cnt = 0u;
for elt: T in v { if x == elt { cnt += 1u; } }
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];
}
fn unzip[@T, @U](v: &[(T, U)]) -> ([T], [U]) {
fn unzip<@T, @U>(v: &[(T, U)]) -> ([T], [U]) {
let as = ~[], bs = ~[];
for (a, b) in v {
as += ~[a];
@ -279,7 +279,7 @@ fn unzip[@T, @U](v: &[(T, U)]) -> ([T], [U]) {
}
// FIXME make the lengths being equal a constraint
fn zip[@T, @U](v: &[T], u: &[U]) -> [(T, U)] {
fn zip<@T, @U>(v: &[T], u: &[U]) -> [(T, U)] {
let zipped = ~[];
let sz = len(v), i = 0u;
assert (sz == len(u));
@ -291,14 +291,14 @@ fn zip[@T, @U](v: &[T], u: &[U]) -> [(T, U)] {
}
// Swaps two elements in a vector
fn swap[@T](v: &[mutable T], a: uint, b: uint) {
fn swap<@T>(v: &[mutable T], a: uint, b: uint) {
let t: T = v.(a);
v.(a) = v.(b);
v.(b) = t;
}
// In place vector reversal
fn reverse[@T](v: &[mutable T]) {
fn reverse<@T>(v: &[mutable T]) {
let i: uint = 0u;
let ln = len[T](v);
while i < ln / 2u { swap(v, i, ln - i - 1u); i += 1u; }
@ -306,7 +306,7 @@ fn reverse[@T](v: &[mutable T]) {
// Functional vector reversal. Returns a reversed copy of v.
fn reversed[@T](v: &[T]) -> [T] {
fn reversed<@T>(v: &[T]) -> [T] {
let rs: [T] = ~[];
let i = len[T](v);
if i == 0u { ret rs; } else { i -= 1u; }
@ -323,17 +323,17 @@ mod unsafe {
heap_part: *mutable ivec_heap_part};
type ivec_heap_part = {mutable fill: uint};
fn copy_from_buf[T](v: &mutable [T], ptr: *T, count: uint) {
fn copy_from_buf<T>(v: &mutable [T], ptr: *T, count: uint) {
ret rustrt::ivec_copy_from_buf_shared(v, ptr, count);
}
fn from_buf[T](ptr: *T, bytes: uint) -> [T] {
fn from_buf<T>(ptr: *T, bytes: uint) -> [T] {
let v = ~[];
copy_from_buf(v, ptr, bytes);
ret v;
}
fn set_len[T](v: &mutable [T], new_len: uint) {
fn set_len<T>(v: &mutable [T], new_len: uint) {
let new_fill = new_len * sys::size_of[T]();
let stack_part: *mutable ivec_repr =
::unsafe::reinterpret_cast(addr_of(v));