pub fn next_solution<'s>(&'s mut self) -> Option<Vec<&'e E::Possibility>>
    where
        'e: 's,
    {
        enum StackOp<T> {
            Push(T),
            Pop,
            None,
        }

        while !self.stack.is_empty() {
            let curr_frame = self.stack.last_mut().unwrap();

            let (stack_op, possible_solution) = match curr_frame.state {
                // for the current row of this frame, cover the selected columns and add the row
                // to the solution.
                FrameState::Cover => {
                    let (row_index, columns) = curr_frame.selected_rows.front().unwrap();

                    self.partial_solution.push(row_index - 1);
                    for column_ptr in columns {
                        Column::cover(*column_ptr);
                    }

                    // This is where the recursion happens, but we also have to check for the
                    // solution here.
                    let stack_op = if Self::solution_test(&self.grid, self.problem) {
                        (StackOp::None, Some(self.partial_solution.clone()))
                    } else {
                        let min_column = Self::choose_column(&mut self.grid, self.problem);
                        let selected_rows = Self::select_rows_from_column(min_column);

                        if selected_rows.is_empty() {
                            (StackOp::None, None)
                        } else {
                            (
                                StackOp::Push(Frame {
                                    state: FrameState::Cover,
                                    min_column,
                                    selected_rows,
                                }),
                                None,
                            )
                        }
                    };

                    curr_frame.state = FrameState::Uncover;
                    stack_op
                }
                // Cleanup the current row, uncover the selected columns, remove the row from
                // the solution.
                FrameState::Uncover => {
                    let (_row_index, columns) = curr_frame.selected_rows.pop_front().unwrap();

                    for column_ptr in columns {
                        Column::uncover(column_ptr);
                    }
                    self.partial_solution.pop();

                    if curr_frame.selected_rows.is_empty() {
                        (StackOp::Pop, None)
                    } else {
                        curr_frame.state = FrameState::Cover;
                        (StackOp::None, None)
                    }
                }
            };

            match stack_op {
                StackOp::Push(val) => {
                    self.stack.push(val);
                }
                StackOp::Pop => {
                    self.stack.pop();
                }
                StackOp::None => {}
            }

            if let Some(solution) = possible_solution {
                return Some(
                    solution
                        .into_iter()
                        .map(|row_index| &self.problem.possibilities()[row_index])
                        .collect(),
                );
            }
        }

        None
    }
/// Compute up to the next solution, returning `None` if there are no more.
Compute up to the next solution, returning `None` if there are no more.
Compute up to the next solution returning ` None ` if there are no more .
pub fn next_solution<'s>(&'s mut self) -> Option<Vec<&'e E::Possibility>>
    where
        'e: 's,
    {
        enum StackOp<T> {
            Push(T),
            Pop,
            None,
        }
        while !self.stack.is_empty() {
            let curr_frame = self.stack.last_mut().unwrap();
            let (stack_op, possible_solution) = match curr_frame.state {
                // for the current row of this frame, cover the selected columns and add the row
                // to the solution.
                FrameState::Cover => {
                    let (row_index, columns) = curr_frame.selected_rows.front().unwrap();
                    self.partial_solution.push(row_index - 1);
                    for column_ptr in columns {
                        Column::cover(*column_ptr);
                    }
                    // This is where the recursion happens, but we also have to check for the
                    // solution here.
                    let stack_op = if Self::solution_test(&self.grid, self.problem) {
                        (StackOp::None, Some(self.partial_solution.clone()))
                    } else {
                        let min_column = Self::choose_column(&mut self.grid, self.problem);
                        let selected_rows = Self::select_rows_from_column(min_column);
                        if selected_rows.is_empty() {
                            (StackOp::None, None)
                        } else {
                            (
                                StackOp::Push(Frame {
                                    state: FrameState::Cover,
                                    min_column,
                                    selected_rows,
                                }),
                                None,
                            )
                        }
                    };
                    curr_frame.state = FrameState::Uncover;
                    stack_op
                }
                // Cleanup the current row, uncover the selected columns, remove the row from
                // the solution.
                FrameState::Uncover => {
                    let (_row_index, columns) = curr_frame.selected_rows.pop_front().unwrap();
                    for column_ptr in columns {
                        Column::uncover(column_ptr);
                    }
                    self.partial_solution.pop();
                    if curr_frame.selected_rows.is_empty() {
                        (StackOp::Pop, None)
                    } else {
                        curr_frame.state = FrameState::Cover;
                        (StackOp::None, None)
                    }
                }
            };
            match stack_op {
                StackOp::Push(val) => {
                    self.stack.push(val);
                }
                StackOp::Pop => {
                    self.stack.pop();
                }
                StackOp::None => {}
            }
            if let Some(solution) = possible_solution {
                return Some(
                    solution
                        .into_iter()
                        .map(|row_index| &self.problem.possibilities()[row_index])
                        .collect(),
                );
            }
        }
        None
    }
pub fn next_solution < ' s > ( & ' s mut self ) -> Option < Vec < & ' e E :: Possibility > > where ' e : ' s , { enum StackOp < T > { Push ( T ) , Pop , None , } while ! self . stack . is_empty ( ) { let curr_frame = self . stack . last_mut ( ) . unwrap ( ) ; let ( stack_op , possible_solution ) = match curr_frame . state { // for the current row of this frame, cover the selected columns and add the row // to the solution. FrameState :: Cover => { let ( row_index , columns ) = curr_frame . selected_rows . front ( ) . unwrap ( ) ; self . partial_solution . push ( row_index - 1 ) ; for column_ptr in columns { Column :: cover ( * column_ptr ) ; } // This is where the recursion happens, but we also have to check for the // solution here. let stack_op = if Self :: solution_test ( & self . grid , self . problem ) { ( StackOp :: None , Some ( self . partial_solution . clone ( ) ) ) } else { let min_column = Self :: choose_column ( & mut self . grid , self . problem ) ; let selected_rows = Self :: select_rows_from_column ( min_column ) ; if selected_rows . is_empty ( ) { ( StackOp :: None , None ) } else { ( StackOp :: Push ( Frame { state : FrameState :: Cover , min_column , selected_rows , } ) , None , ) } } ; curr_frame . state = FrameState :: Uncover ; stack_op } // Cleanup the current row, uncover the selected columns, remove the row from // the solution. FrameState :: Uncover => { let ( _row_index , columns ) = curr_frame . selected_rows . pop_front ( ) . unwrap ( ) ; for column_ptr in columns { Column :: uncover ( column_ptr ) ; } self . partial_solution . pop ( ) ; if curr_frame . selected_rows . is_empty ( ) { ( StackOp :: Pop , None ) } else { curr_frame . state = FrameState :: Cover ; ( StackOp :: None , None ) } } } ; match stack_op { StackOp :: Push ( val ) => { self . stack . push ( val ) ; } StackOp :: Pop => { self . stack . pop ( ) ; } StackOp :: None => { } } if let Some ( solution ) = possible_solution { return Some ( solution . into_iter ( ) . map ( | row_index | & self . problem . possibilities ( ) [ row_index ] ) . collect ( ) , ) ; } } None }
Compute up to the next solution, returning `None` if there are no more.
Compute up to the next solution returning ` None ` if there are no more .
&'s mut self
&'s mut self