2011-07-08 04:16:46 -05:00
|
|
|
/*
|
|
|
|
|
|
|
|
Idea: provide functions for 'exhaustive' and 'random' modification of vecs.
|
|
|
|
|
2012-04-13 13:59:17 -05:00
|
|
|
two functions, "return all edits" and "return a random edit" <--
|
|
|
|
leaning toward this model or two functions, "return the number of
|
|
|
|
possible edits" and "return edit #n"
|
2011-07-08 04:16:46 -05:00
|
|
|
|
2012-04-13 13:59:17 -05:00
|
|
|
It would be nice if this could be data-driven, so the two functions
|
|
|
|
could share information:
|
2012-06-29 18:26:56 -05:00
|
|
|
type vec_modifier = rec(fn (<T> v, uint i) -> ~[T] fun, uint lo, uint di);
|
|
|
|
const ~[vec_modifier] vec_modifiers = ~[rec(fun=vec_omit, 0u, 1u), ...]/~;
|
2012-04-13 13:59:17 -05:00
|
|
|
But that gives me "error: internal compiler error unimplemented consts
|
|
|
|
that's not a plain literal".
|
2011-07-08 04:16:46 -05:00
|
|
|
https://github.com/graydon/rust/issues/570
|
|
|
|
|
2011-09-10 14:03:13 -05:00
|
|
|
vec_edits is not an iter because iters might go away.
|
2011-07-08 04:16:46 -05:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
use std;
|
2011-12-13 18:25:51 -06:00
|
|
|
import vec;
|
|
|
|
import vec::slice;
|
|
|
|
import vec::len;
|
|
|
|
import int;
|
2011-07-08 04:16:46 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn vec_omit<T: copy>(v: ~[T], i: uint) -> ~[T] {
|
2011-07-27 07:19:39 -05:00
|
|
|
slice(v, 0u, i) + slice(v, i + 1u, len(v))
|
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
fn vec_dup<T: copy>(v: ~[T], i: uint) -> ~[T] {
|
2011-08-19 17:16:48 -05:00
|
|
|
slice(v, 0u, i) + [v[i]] + slice(v, i, len(v))
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
fn vec_swadj<T: copy>(v: ~[T], i: uint) -> ~[T] {
|
2011-08-19 17:16:48 -05:00
|
|
|
slice(v, 0u, i) + [v[i + 1u], v[i]] + slice(v, i + 2u, len(v))
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
fn vec_prefix<T: copy>(v: ~[T], i: uint) -> ~[T] { slice(v, 0u, i) }
|
|
|
|
fn vec_suffix<T: copy>(v: ~[T], i: uint) -> ~[T] { slice(v, i, len(v)) }
|
2011-07-08 04:16:46 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn vec_poke<T: copy>(v: ~[T], i: uint, x: T) -> ~[T] {
|
|
|
|
slice(v, 0u, i) + ~[x] + slice(v, i + 1u, len(v))
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
fn vec_insert<T: copy>(v: ~[T], i: uint, x: T) -> ~[T] {
|
|
|
|
slice(v, 0u, i) + ~[x] + slice(v, i, len(v))
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-07-08 04:16:46 -05:00
|
|
|
|
|
|
|
// Iterates over 0...length, skipping the specified number on each side.
|
2011-10-21 06:14:28 -05:00
|
|
|
fn ix(skip_low: uint, skip_high: uint, length: uint, it: block(uint)) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i: uint = skip_low;
|
2011-10-21 06:14:28 -05:00
|
|
|
while i + skip_high <= length { it(i); i += 1u; }
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-07-08 04:16:46 -05:00
|
|
|
|
2012-04-13 13:59:17 -05:00
|
|
|
// Returns a bunch of modified versions of v, some of which introduce
|
|
|
|
// new elements (borrowed from xs).
|
2012-06-29 18:26:56 -05:00
|
|
|
fn vec_edits<T: copy>(v: ~[T], xs: ~[T]) -> ~[~[T]] {
|
|
|
|
let edits: ~[~[T]] = ~[];
|
2011-07-27 07:19:39 -05:00
|
|
|
let Lv: uint = len(v);
|
|
|
|
|
|
|
|
if Lv != 1u {
|
2011-09-10 14:12:44 -05:00
|
|
|
// When Lv == 1u, this is redundant with omit.
|
2012-06-29 18:26:56 -05:00
|
|
|
vec::push(edits, ~[]);
|
2011-09-10 14:12:44 -05:00
|
|
|
}
|
|
|
|
if Lv >= 3u {
|
|
|
|
// When Lv == 2u, this is redundant with swap.
|
2012-06-15 20:16:42 -05:00
|
|
|
vec::push(edits, vec::reversed(v));
|
2011-08-19 17:16:48 -05:00
|
|
|
}
|
2012-06-29 18:26:56 -05:00
|
|
|
ix(0u, 1u, Lv) {|i| edits += ~[vec_omit(v, i)]; }
|
|
|
|
ix(0u, 1u, Lv) {|i| edits += ~[vec_dup(v, i)]; }
|
|
|
|
ix(0u, 2u, Lv) {|i| edits += ~[vec_swadj(v, i)]; }
|
|
|
|
ix(1u, 2u, Lv) {|i| edits += ~[vec_prefix(v, i)]; }
|
|
|
|
ix(2u, 1u, Lv) {|i| edits += ~[vec_suffix(v, i)]; }
|
2011-10-21 06:14:28 -05:00
|
|
|
|
|
|
|
ix(0u, 1u, len(xs)) {|j|
|
|
|
|
ix(0u, 1u, Lv) {|i|
|
2012-06-15 20:16:42 -05:00
|
|
|
vec::push(edits, vec_poke(v, i, xs[j]));
|
2011-10-21 07:12:12 -05:00
|
|
|
}
|
2011-10-21 06:14:28 -05:00
|
|
|
ix(0u, 0u, Lv) {|i|
|
2012-06-15 20:16:42 -05:00
|
|
|
vec::push(edits, vec_insert(v, i, xs[j]));
|
2011-10-21 07:12:12 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
|
2011-07-08 04:16:46 -05:00
|
|
|
edits
|
|
|
|
}
|
|
|
|
|
2012-04-13 13:59:17 -05:00
|
|
|
// Would be nice if this were built in:
|
|
|
|
// https://github.com/graydon/rust/issues/424
|
2012-06-29 18:26:56 -05:00
|
|
|
fn vec_to_str(v: ~[int]) -> str {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = 0u;
|
|
|
|
let s = "[";
|
|
|
|
while i < len(v) {
|
2011-08-19 17:16:48 -05:00
|
|
|
s += int::str(v[i]);
|
2011-09-20 13:58:33 -05:00
|
|
|
if i + 1u < len(v) { s += ", "; }
|
2011-07-08 04:16:46 -05:00
|
|
|
i += 1u;
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return s + "]";
|
2011-07-08 04:16:46 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn show_edits(a: ~[int], xs: ~[int]) {
|
2011-12-22 19:53:53 -06:00
|
|
|
log(error, "=== Edits of " + vec_to_str(a) + " ===");
|
2011-07-27 07:19:39 -05:00
|
|
|
let b = vec_edits(a, xs);
|
2011-12-22 19:53:53 -06:00
|
|
|
ix(0u, 1u, len(b)) {|i| log(error, vec_to_str(b[i])); }
|
2011-07-08 04:16:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn demo_edits() {
|
2012-06-29 18:26:56 -05:00
|
|
|
let xs = ~[7, 8];
|
|
|
|
show_edits(~[], xs);
|
|
|
|
show_edits(~[1], xs);
|
|
|
|
show_edits(~[1, 2], xs);
|
|
|
|
show_edits(~[1, 2, 3], xs);
|
|
|
|
show_edits(~[1, 2, 3, 4], xs);
|
2011-07-08 04:16:46 -05:00
|
|
|
}
|
|
|
|
|
2011-08-04 18:20:53 -05:00
|
|
|
fn main() { demo_edits(); }
|