Skip to content

Commit 268533a

Browse files
committed
Add a precondition on vec::zip
vec::zip now has the precondition that the two argument vectors are the same length. Changed uses of it to reflect that. Also added a few vector-enumerating utilities to vec.rs, which necessitated in making some functions in u8 declared-pure.
1 parent d37e8cf commit 268533a

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

src/lib/u8.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11

22

3-
fn add(x: u8, y: u8) -> u8 { ret x + y; }
3+
pure fn add(x: u8, y: u8) -> u8 { ret x + y; }
44

5-
fn sub(x: u8, y: u8) -> u8 { ret x - y; }
5+
pure fn sub(x: u8, y: u8) -> u8 { ret x - y; }
66

7-
fn mul(x: u8, y: u8) -> u8 { ret x * y; }
7+
pure fn mul(x: u8, y: u8) -> u8 { ret x * y; }
88

9-
fn div(x: u8, y: u8) -> u8 { ret x / y; }
9+
pure fn div(x: u8, y: u8) -> u8 { ret x / y; }
1010

11-
fn rem(x: u8, y: u8) -> u8 { ret x % y; }
11+
pure fn rem(x: u8, y: u8) -> u8 { ret x % y; }
1212

13-
fn lt(x: u8, y: u8) -> bool { ret x < y; }
13+
pure fn lt(x: u8, y: u8) -> bool { ret x < y; }
1414

15-
fn le(x: u8, y: u8) -> bool { ret x <= y; }
15+
pure fn le(x: u8, y: u8) -> bool { ret x <= y; }
1616

17-
fn eq(x: u8, y: u8) -> bool { ret x == y; }
17+
pure fn eq(x: u8, y: u8) -> bool { ret x == y; }
1818

19-
fn ne(x: u8, y: u8) -> bool { ret x != y; }
19+
pure fn ne(x: u8, y: u8) -> bool { ret x != y; }
2020

21-
fn ge(x: u8, y: u8) -> bool { ret x >= y; }
21+
pure fn ge(x: u8, y: u8) -> bool { ret x >= y; }
2222

23-
fn gt(x: u8, y: u8) -> bool { ret x > y; }
23+
pure fn gt(x: u8, y: u8) -> bool { ret x > y; }
2424

2525
iter range(lo: u8, hi: u8) -> u8 { while lo < hi { put lo; lo += 1u8; } }
2626
// Local Variables:

src/lib/vec.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ fn init_elt_mut<@T>(t: &T, n_elts: uint) -> [mutable T] {
5757
ret v;
5858
}
5959

60+
// FIXME: Possible typestate postcondition:
61+
// len(result) == len(v) (needs issue #586)
6062
fn to_mut<@T>(v: &[T]) -> [mutable T] {
6163
let vres = [mutable];
6264
for t: T in v { vres += [mutable t]; }
6365
ret vres;
6466
}
6567

68+
// Same comment as from_mut
6669
fn from_mut<@T>(v: &[mutable T]) -> [T] {
6770
let vres = [];
6871
for t: T in v { vres += [t]; }
@@ -253,14 +256,23 @@ fn position_pred<T>(f: fn(&T) -> bool, v: &[T]) -> option::t<uint> {
253256
ret none;
254257
}
255258

259+
pure fn same_length<T, U>(xs: &[T], ys: &[U]) -> bool {
260+
let xlen = unchecked { vec::len(xs) };
261+
let ylen = unchecked { vec::len(ys) };
262+
xlen == ylen
263+
}
264+
265+
// FIXME: if issue #586 gets implemented, could have a postcondition
266+
// saying the two result lists have the same length -- or, could
267+
// return a nominal record with a constraint saying that, instead of
268+
// returning a tuple (contingent on issue #869)
256269
fn unzip<@T, @U>(v: &[(T, U)]) -> ([T], [U]) {
257270
let as = [], bs = [];
258271
for (a, b) in v { as += [a]; bs += [b]; }
259272
ret (as, bs);
260273
}
261274

262-
// FIXME make the lengths being equal a constraint
263-
fn zip<@T, @U>(v: &[T], u: &[U]) -> [(T, U)] {
275+
fn zip<@T, @U>(v: &[T], u: &[U]) : same_length(v, u) -> [(T, U)] {
264276
let zipped = [];
265277
let sz = len(v), i = 0u;
266278
assert (sz == len(u));
@@ -293,6 +305,27 @@ fn reversed<@T>(v: &[T]) -> [T] {
293305
ret rs;
294306
}
295307

308+
// Generating vecs.
309+
fn enum_chars(start:u8, end:u8) : u8::le(start, end) -> [char] {
310+
let i = start;
311+
let r = [];
312+
while (i <= end) {
313+
r += [i as char];
314+
i += (1u as u8);
315+
}
316+
ret r;
317+
}
318+
319+
fn enum_uints(start:uint, end:uint) : uint::le(start, end) -> [uint] {
320+
let i = start;
321+
let r = [];
322+
while (i <= end) {
323+
r += [i];
324+
i += 1u;
325+
}
326+
ret r;
327+
}
328+
296329
// Iterate over a list with with the indexes
297330
iter iter2<@T>(v: &[T]) -> (uint, T) {
298331
let i = 0u;

src/test/stdtest/qsort.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ fn test_simple() {
4949
fn lteq(a: &int, b: &int) -> bool { int::le(a, b) }
5050
sort::quick_sort(lteq, names);
5151

52-
let pairs = vec::zip(expected, vec::from_mut(names));
52+
let immut_names = vec::from_mut(names);
53+
54+
// Silly, but what else can we do?
55+
check vec::same_length(expected, immut_names);
56+
let pairs = vec::zip(expected, immut_names);
5357
for (a, b) in pairs { log #fmt["%d %d", a, b]; assert (a == b); }
5458
}
5559

src/test/stdtest/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ fn sort_tests() {
8686
"test::ignored_tests_result_in_ignored", "test::parse_ignored_flag",
8787
"test::sort_tests"];
8888

89+
check vec::same_length(expected, filtered);
8990
let pairs = vec::zip(expected, filtered);
9091

9192

src/test/stdtest/vec.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ fn test_any_and_all() {
302302
fn test_zip_unzip() {
303303
let v1 = [1, 2, 3];
304304
let v2 = [4, 5, 6];
305+
306+
check same_length(v1, v2); // Silly, but what else can we do?
305307
let z1 = vec::zip(v1, v2);
306308

307309
assert ((1, 4) == z1[0]);

0 commit comments

Comments
 (0)