1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
pub type Index = usize;

// TODO: const generics
pub type Row = Vec<Index>;

struct Entry {
    left: Index,
    right: Index,
    up: Index,
    down: Index,
    // For headers, x1 is the column_number.
    // For data, x1 is the index of the header.
    x1: Index,
    // For headers, x2 is the size (1-count).
    // For data, x2 is 1 for the start of a row, 0 otherwise.
    x2: Index,
}

impl Default for Entry {
    fn default() -> Entry {
        Entry {
            left: 0,
            right: 0,
            up: 0,
            down: 0,
            x1: 0,
            x2: 0,
        }
    }
}

impl Entry {
    fn new() -> Entry {
        Default::default()
    }
}

pub struct Solver {
    es: Vec<Entry>,
    sol_rows: Vec<Index>,
    finished: bool,
}

impl Solver {
    pub fn new<I: Iterator<Item = Row>>(ncols: usize, rows: I) -> Solver {
        let mut es = Vec::new();
        Solver::add_headers(&mut es, ncols);
        // Bottoms keeps track of the index of the "bottom" entry in
        // each column, updated as rows are added.
        let mut bottoms = Vec::new();
        bottoms.extend(2..ncols + 2);
        for row in rows {
            Solver::add_row(&mut es, &row, &mut bottoms);
        }
        // Set the "tops" to point up at the "bottoms", and vice
        // versa.
        let mut idx = 2;
        while idx != 1 {
            es[idx].up = bottoms[idx - 2];
            es[bottoms[idx - 2]].down = idx;
            idx = es[idx].right;
        }
        Solver {
            es,
            sol_rows: Vec::new(),
            finished: false,
        }
    }

    fn add_headers(es: &mut Vec<Entry>, ncols: usize) {
        // Skip entry 0 since index 0 corresponds to a null pointer.
        es.push(Entry::new());
        // The first element is the root.
        es.push(Entry {
            right: 2,
            ..Default::default()
        });
        // The next ncols elements are the list headers.
        for i in 1..ncols + 1 {
            es.push(Entry {
                left: i,
                right: i + 2,
                x1: i - 1,
                ..Default::default()
            });
        }
        // Connect the rightmost header to the root.
        es[1].left = ncols + 1;
        es[ncols + 1].right = 1;
    }

    fn add_row(es: &mut Vec<Entry>, row: &[Index], bottoms: &mut Vec<Index>) {
        let row_start = es.len();
        for &col_num in row {
            let idx = es.len();
            // Add an entry linked to the correct column and pointing
            // up to the current bottom.
            es.push(Entry {
                x1: col_num + 2,
                left: idx - 1,
                right: idx + 1,
                up: bottoms[col_num],
                ..Default::default()
            });
            // Update the bottom of the column to point to the new
            // entry.
            es[bottoms[col_num]].down = idx;
            bottoms[col_num] = idx;
            // Increment the count of non-zero entries in the column.
            es[col_num + 2].x2 += 1;
        }
        // Connect the leftmost and rightmost entries in the row.
        let row_end = es.len() - 1;
        es[row_end].right = row_start;
        es[row_start].left = row_end;
        // Mark the leftmost entry as the start of a row.
        es[row_start].x2 = 1;
    }

    // Choose the column that results in the least amount of recursive
    // calls.
    fn choose_column(&self) -> Index {
        let mut min_size = Index::max_value();
        let mut min_hdr = 0;
        let mut hdr = self.es[1].right;
        while hdr != 1 {
            if self.es[hdr].x2 < min_size {
                min_hdr = hdr;
                min_size = self.es[hdr].x2;
            }
            hdr = self.es[hdr].right;
        }
        min_hdr
    }

    fn cover(&mut self, hdr: Index) {
        let hdr_right = self.es[hdr].right;
        self.es[hdr_right].left = self.es[hdr].left;
        let hdr_left = self.es[hdr].left;
        self.es[hdr_left].right = self.es[hdr].right;
        let mut row = self.es[hdr].down;
        while row != hdr {
            let mut col = self.es[row].right;
            while col != row {
                let col_hdr = self.es[col].x1;
                self.es[col_hdr].x2 -= 1;
                let col_up = self.es[col].up;
                self.es[col_up].down = self.es[col].down;
                let col_down = self.es[col].down;
                self.es[col_down].up = self.es[col].up;
                col = self.es[col].right;
            }
            row = self.es[row].down;
        }
    }

    fn uncover(&mut self, hdr: Index) {
        let mut row = self.es[hdr].up;
        while row != hdr {
            let mut col = self.es[row].left;
            while col != row {
                let col_hdr = self.es[col].x1;
                self.es[col_hdr].x2 += 1;
                let col_up = self.es[col].up;
                self.es[col_up].down = col;
                let col_down = self.es[col].down;
                self.es[col_down].up = col;
                col = self.es[col].left;
            }
            row = self.es[row].up;
        }
        let hdr_right = self.es[hdr].right;
        self.es[hdr_right].left = hdr;
        let hdr_left = self.es[hdr].left;
        self.es[hdr_left].right = hdr;
    }

    pub fn solve(&mut self, partial: Vec<Row>, sols: &mut Solutions) {
        self.finished = false;
        self.sol_rows.clear();

        for row in &partial {
            for col_num in row {
                self.cover(col_num + 2);
            }
        }

        self.solveception(&partial, sols);

        for row in partial.iter().rev() {
            for col_num in row.iter().rev() {
                self.uncover(col_num + 2);
            }
        }
    }

    fn solveception(&mut self, partial: &[Row], sols: &mut Solutions) {
        // We have a solution if we've successfully covered all the
        // columns.
        if self.es[1].right == 1 {
            self.finished = {
                let sol = Solution {
                    solver: self,
                    rows: self.sol_rows.iter(),
                    partial: partial.iter(),
                };
                !sols.push(sol)
            };
            return;
        }

        let hdr = self.choose_column();
        self.cover(hdr);

        // For each row with an entry in the chosen column:
        let mut row_idx = self.es[hdr].down;
        while row_idx != hdr {
            // add it to the partial solution,
            self.sol_rows.push(row_idx);
            // cover all the other rows with overlapping entries,
            let mut col_idx = self.es[row_idx].right;
            while col_idx != row_idx {
                let col_hdr = self.es[col_idx].x1;
                let col_num = self.es[col_hdr].x1;
                self.cover(col_num + 2);
                col_idx = self.es[col_idx].right;
            }
            // recurse,
            self.solveception(partial, sols);
            // then undo.
            col_idx = self.es[row_idx].left;
            while col_idx != row_idx {
                let col_hdr = self.es[col_idx].x1;
                let col_num = self.es[col_hdr].x1;
                self.uncover(col_num + 2);
                col_idx = self.es[col_idx].left;
            }
            self.sol_rows.pop();
            if self.finished {
                break;
            }
            row_idx = self.es[row_idx].down;
        }
        self.uncover(hdr);
    }
}

/// An iterator over the rows of a solution.
pub struct Solution<'a, 'b> {
    solver: &'a Solver,
    partial: ::std::slice::Iter<'b, Row>,
    rows: ::std::slice::Iter<'a, Index>,
}

impl<'a, 'b> Iterator for Solution<'a, 'b> {
    type Item = Row;
    // First returns each row from the inital partial solution, then
    // returns each row from the rest of the solution.
    fn next(&mut self) -> Option<Self::Item> {
        match self.partial.next() {
            Some(row) => Some(row.clone()),
            None => match self.rows.next() {
                None => None,
                Some(&row_idx) => Some(self.get_row(row_idx)),
            },
        }
    }
}

impl<'a, 'b> Solution<'a, 'b> {
    // Returns the indexes for all entries in the same row as the one
    // at row_idx.
    fn get_row(&self, row_idx: Index) -> Row {
        let mut row_idx = row_idx;
        let es = &self.solver.es;
        while es[row_idx].x2 == 0 {
            row_idx = es[row_idx].left;
        }
        let mut row = vec![es[es[row_idx].x1].x1];
        let mut col_idx = es[row_idx].right;
        while col_idx != row_idx {
            row.push(es[es[col_idx].x1].x1);
            col_idx = es[col_idx].right;
        }
        row
    }
}

pub trait Solutions {
    /// Handle a solution.
    ///
    /// Return true to keep looking for more solutions, or false to
    /// quit.
    fn push(&mut self, sol: Solution) -> bool;
}