1 // Interior vector utility functions.
3 import option::{some, none};
4 import uint::next_power_of_two;
7 native "rust-intrinsic" mod rusti {
8 fn vec_len<T>(v: [T]) -> uint;
11 native "rust" mod rustrt {
12 fn vec_reserve_shared<T>(&v: [mutable? T], n: uint);
13 fn vec_from_buf_shared<T>(ptr: *T, count: uint) -> [T];
16 /// Reserves space for `n` elements in the given vector.
17 fn reserve<@T>(&v: [mutable? T], n: uint) {
18 rustrt::vec_reserve_shared(v, n);
21 fn len<T>(v: [mutable? T]) -> uint { ret rusti::vec_len(v); }
23 type init_op<T> = fn(uint) -> T;
25 fn init_fn<@T>(op: init_op<T>, n_elts: uint) -> [T] {
29 while i < n_elts { v += [op(i)]; i += 1u; }
33 // TODO: Remove me once we have slots.
34 fn init_fn_mut<@T>(op: init_op<T>, n_elts: uint) -> [mutable T] {
38 while i < n_elts { v += [mutable op(i)]; i += 1u; }
42 fn init_elt<@T>(t: T, n_elts: uint) -> [T] {
46 while i < n_elts { v += [t]; i += 1u; }
50 // TODO: Remove me once we have slots.
51 fn init_elt_mut<@T>(t: T, n_elts: uint) -> [mutable T] {
55 while i < n_elts { v += [mutable t]; i += 1u; }
59 // FIXME: Possible typestate postcondition:
60 // len(result) == len(v) (needs issue #586)
61 fn to_mut<@T>(v: [T]) -> [mutable T] {
63 for t: T in v { vres += [mutable t]; }
67 // Same comment as from_mut
68 fn from_mut<@T>(v: [mutable T]) -> [T] {
70 for t: T in v { vres += [t]; }
75 pure fn is_empty<T>(v: [mutable? T]) -> bool {
76 // FIXME: This would be easier if we could just call len
77 for t: T in v { ret false; }
81 pure fn is_not_empty<T>(v: [mutable? T]) -> bool { ret !is_empty(v); }
85 /// Returns the first element of a vector
86 fn head<@T>(v: [mutable? T]) : is_not_empty(v) -> T { ret v[0]; }
88 /// Returns all but the first element of a vector
89 fn tail<@T>(v: [mutable? T]) : is_not_empty(v) -> [mutable? T] {
90 ret slice(v, 1u, len(v));
93 /// Returns the last element of `v`.
94 fn last<@T>(v: [mutable? T]) -> option::t<T> {
95 if len(v) == 0u { ret none; }
96 ret some(v[len(v) - 1u]);
99 /// Returns the last element of a non-empty vector `v`.
100 fn last_total<@T>(v: [mutable? T]) : is_not_empty(v) -> T {
104 /// Returns a copy of the elements from [`start`..`end`) from `v`.
105 fn slice<@T>(v: [mutable? T], start: uint, end: uint) -> [T] {
106 assert (start <= end);
107 assert (end <= len(v));
109 reserve(result, end - start);
111 while i < end { result += [v[i]]; i += 1u; }
115 // TODO: Remove me once we have slots.
116 fn slice_mut<@T>(v: [mutable? T], start: uint, end: uint) -> [mutable T] {
117 assert (start <= end);
118 assert (end <= len(v));
119 let result = [mutable];
120 reserve(result, end - start);
122 while i < end { result += [mutable v[i]]; i += 1u; }
129 fn shift<@T>(&v: [mutable? T]) -> T {
130 let ln = len::<T>(v);
133 v = slice::<T>(v, 1u, ln);
137 // TODO: Write this, unsafely, in a way that's not O(n).
138 fn pop<@T>(&v: [mutable? T]) -> T {
143 v = slice(v, 0u, ln);
152 /// Expands the given vector in-place by appending `n` copies of `initval`.
153 fn grow<@T>(&v: [T], n: uint, initval: T) {
154 reserve(v, next_power_of_two(len(v) + n));
156 while i < n { v += [initval]; i += 1u; }
159 // TODO: Remove me once we have slots.
160 fn grow_mut<@T>(&v: [mutable T], n: uint, initval: T) {
161 reserve(v, next_power_of_two(len(v) + n));
163 while i < n { v += [mutable initval]; i += 1u; }
166 /// Calls `f` `n` times and appends the results of these calls to the given
168 fn grow_fn<@T>(&v: [T], n: uint, init_fn: fn(uint) -> T) {
169 reserve(v, next_power_of_two(len(v) + n));
171 while i < n { v += [init_fn(i)]; i += 1u; }
174 /// Sets the element at position `index` to `val`. If `index` is past the end
175 /// of the vector, expands the vector by replicating `initval` to fill the
176 /// intervening space.
177 fn grow_set<@T>(&v: [mutable T], index: uint, initval: T, val: T) {
178 if index >= len(v) { grow_mut(v, index - len(v) + 1u, initval); }
183 // Functional utilities
185 fn map<@T, @U>(f: block(T) -> U, v: [mutable? T]) -> [U] {
187 reserve(result, len(v));
189 let elem2 = elem; // satisfies alias checker
190 result += [f(elem2)];
195 fn map2<@T, @U, @V>(f: block(T, U) -> V, v0: [T], v1: [U]) -> [V] {
196 let v0_len = len::<T>(v0);
197 if v0_len != len::<U>(v1) { fail; }
200 while i < v0_len { u += [f({ v0[i] }, { v1[i] })]; i += 1u; }
204 fn filter_map<@T, @U>(f: block(T) -> option::t<U>, v: [mutable? T]) -> [U] {
207 let elem2 = elem; // satisfies alias checker
210 some(result_elem) { result += [result_elem]; }
216 fn filter<@T>(f: block(T) -> bool, v: [mutable? T]) -> [T] {
219 let elem2 = elem; // satisfies alias checker
227 fn foldl<@T, @U>(p: block(U, T) -> U, z: U, v: [mutable? T]) -> U {
229 if sz == 0u { ret z; }
231 let rest = slice(v, 1u, sz);
232 ret p(foldl(p, z, rest), first);
235 fn any<T>(f: block(T) -> bool, v: [T]) -> bool {
236 for elem: T in v { if f(elem) { ret true; } }
240 fn all<T>(f: block(T) -> bool, v: [T]) -> bool {
241 for elem: T in v { if !f(elem) { ret false; } }
245 fn member<T>(x: T, v: [T]) -> bool {
246 for elt: T in v { if x == elt { ret true; } }
250 fn count<T>(x: T, v: [mutable? T]) -> uint {
252 for elt: T in v { if x == elt { cnt += 1u; } }
256 fn find<@T>(f: block(T) -> bool, v: [T]) -> option::t<T> {
257 for elt: T in v { if f(elt) { ret some(elt); } }
261 fn position<@T>(x: T, v: [T]) -> option::t<uint> {
263 while i < len(v) { if x == v[i] { ret some::<uint>(i); } i += 1u; }
267 fn position_pred<T>(f: fn(T) -> bool, v: [T]) -> option::t<uint> {
269 while i < len(v) { if f(v[i]) { ret some::<uint>(i); } i += 1u; }
273 pure fn same_length<T, U>(xs: [T], ys: [U]) -> bool {
274 let xlen = unchecked{ vec::len(xs) };
275 let ylen = unchecked{ vec::len(ys) };
279 // FIXME: if issue #586 gets implemented, could have a postcondition
280 // saying the two result lists have the same length -- or, could
281 // return a nominal record with a constraint saying that, instead of
282 // returning a tuple (contingent on issue #869)
283 fn unzip<@T, @U>(v: [(T, U)]) -> ([T], [U]) {
284 let as = [], bs = [];
285 for (a, b) in v { as += [a]; bs += [b]; }
289 fn zip<@T, @U>(v: [T], u: [U]) : same_length(v, u) -> [(T, U)] {
291 let sz = len(v), i = 0u;
292 assert (sz == len(u));
293 while i < sz { zipped += [(v[i], u[i])]; i += 1u; }
297 // Swaps two elements in a vector
298 fn swap<@T>(v: [mutable T], a: uint, b: uint) {
304 // In place vector reversal
305 fn reverse<@T>(v: [mutable T]) {
307 let ln = len::<T>(v);
308 while i < ln / 2u { swap(v, i, ln - i - 1u); i += 1u; }
312 // Functional vector reversal. Returns a reversed copy of v.
313 fn reversed<@T>(v: [T]) -> [T] {
316 if i == 0u { ret rs; } else { i -= 1u; }
317 while i != 0u { rs += [v[i]]; i -= 1u; }
323 fn enum_chars(start: u8, end: u8) : u8::le(start, end) -> [char] {
326 while i <= end { r += [i as char]; i += 1u as u8; }
330 fn enum_uints(start: uint, end: uint) : uint::le(start, end) -> [uint] {
333 while i <= end { r += [i]; i += 1u; }
337 // Iterate over a list with with the indexes
338 iter iter2<@T>(v: [T]) -> (uint, T) {
340 for x in v { put (i, x); i += 1u; }
344 type vec_repr = {mutable fill: uint, mutable alloc: uint, data: u8};
346 fn from_buf<T>(ptr: *T, elts: uint) -> [T] {
347 ret rustrt::vec_from_buf_shared(ptr, elts);
350 fn set_len<T>(&v: [T], new_len: uint) {
351 let repr: **vec_repr = ::unsafe::reinterpret_cast(addr_of(v));
352 (**repr).fill = new_len * sys::size_of::<T>();
355 fn to_ptr<T>(v: [T]) -> *T {
356 let repr: **vec_repr = ::unsafe::reinterpret_cast(addr_of(v));
357 ret ::unsafe::reinterpret_cast(addr_of((**repr).data));
361 fn to_ptr<T>(v: [T]) -> *T { ret unsafe::to_ptr(v); }
366 // indent-tabs-mode: nil
368 // buffer-file-coding-system: utf-8-unix
369 // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";