diff options
| author | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2011-08-12 06:37:10 -0700 |
|---|---|---|
| committer | Graydon Hoare <graydon@mozilla.com> | 2011-08-16 15:05:56 -0700 |
| commit | 4c9049c50c5c32f556eaefbcc50209ef8ee353d0 (patch) | |
| tree | bfb83e800b3ebbba65beb080dad463f72da4ac73 /src/lib/vec.rs | |
| parent | 21f46a1655f2a026546792546b07dec9e039ec54 (diff) | |
| download | rust-4c9049c50c5c32f556eaefbcc50209ef8ee353d0.tar.gz rust-4c9049c50c5c32f556eaefbcc50209ef8ee353d0.zip | |
Port the stdlib to the decl foo<T> syntax.
Diffstat (limited to 'src/lib/vec.rs')
| -rw-r--r-- | src/lib/vec.rs | 98 |
1 files changed, 49 insertions, 49 deletions
diff --git a/src/lib/vec.rs b/src/lib/vec.rs index f82eec559ed..0a6228945fb 100644 --- a/src/lib/vec.rs +++ b/src/lib/vec.rs @@ -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)); |
