3 Idea: provide functions for 'exhaustive' and 'random' modification of vecs.
5 two functions, "return all edits" and "return a random edit" <-- leaning toward this model
7 two functions, "return the number of possible edits" and "return edit #n"
9 It would be nice if this could be data-driven, so the two functions could share information:
10 type vec_modifier = rec(fn (<T> v, uint i) -> [T] fun, uint lo, uint di);
11 const [vec_modifier] vec_modifiers = ~[rec(fun=vec_omit, 0u, 1u), ...];
12 But that gives me "error: internal compiler error unimplemented consts that's not a plain literal".
13 https://github.com/graydon/rust/issues/570
15 vec_edits is not an iter because iters might go away.
25 fn vec_omit<T: copy>(v: [T], i: uint) -> [T] {
26 slice(v, 0u, i) + slice(v, i + 1u, len(v))
28 fn vec_dup<T: copy>(v: [T], i: uint) -> [T] {
29 slice(v, 0u, i) + [v[i]] + slice(v, i, len(v))
31 fn vec_swadj<T: copy>(v: [T], i: uint) -> [T] {
32 slice(v, 0u, i) + [v[i + 1u], v[i]] + slice(v, i + 2u, len(v))
34 fn vec_prefix<T: copy>(v: [T], i: uint) -> [T] { slice(v, 0u, i) }
35 fn vec_suffix<T: copy>(v: [T], i: uint) -> [T] { slice(v, i, len(v)) }
37 fn vec_poke<T: copy>(v: [T], i: uint, x: T) -> [T] {
38 slice(v, 0u, i) + [x] + slice(v, i + 1u, len(v))
40 fn vec_insert<T: copy>(v: [T], i: uint, x: T) -> [T] {
41 slice(v, 0u, i) + [x] + slice(v, i, len(v))
44 // Iterates over 0...length, skipping the specified number on each side.
45 fn ix(skip_low: uint, skip_high: uint, length: uint, it: block(uint)) {
46 let i: uint = skip_low;
47 while i + skip_high <= length { it(i); i += 1u; }
50 // Returns a bunch of modified versions of v, some of which introduce new elements (borrowed from xs).
51 fn vec_edits<T: copy>(v: [T], xs: [T]) -> [[T]] {
52 let edits: [[T]] = [];
53 let Lv: uint = len(v);
56 // When Lv == 1u, this is redundant with omit.
60 // When Lv == 2u, this is redundant with swap.
61 edits += [vec::reversed(v)];
63 ix(0u, 1u, Lv) {|i| edits += [vec_omit(v, i)]; }
64 ix(0u, 1u, Lv) {|i| edits += [vec_dup(v, i)]; }
65 ix(0u, 2u, Lv) {|i| edits += [vec_swadj(v, i)]; }
66 ix(1u, 2u, Lv) {|i| edits += [vec_prefix(v, i)]; }
67 ix(2u, 1u, Lv) {|i| edits += [vec_suffix(v, i)]; }
69 ix(0u, 1u, len(xs)) {|j|
71 edits += [vec_poke(v, i, xs[j])];
74 edits += [vec_insert(v, i, xs[j])];
81 // Would be nice if this were built in: https://github.com/graydon/rust/issues/424
82 fn vec_to_str(v: [int]) -> str {
87 if i + 1u < len(v) { s += ", "; }
93 fn show_edits(a: [int], xs: [int]) {
94 log(error, "=== Edits of " + vec_to_str(a) + " ===");
95 let b = vec_edits(a, xs);
96 ix(0u, 1u, len(b)) {|i| log(error, vec_to_str(b[i])); }
103 show_edits([1, 2], xs);
104 show_edits([1, 2, 3], xs);
105 show_edits([1, 2, 3, 4], xs);
108 fn main() { demo_edits(); }