Move sort_cells to operator< of Cell class

This commit is contained in:
karl 2020-12-02 15:57:43 +01:00
parent 8d50ac79d0
commit b5dfd9743a

View File

@ -67,6 +67,10 @@ public:
possible_values = possibilities;
}
bool operator<(const Cell &other) {
return possible_values.size() < other.possible_values.size();
}
};
class Sudoku {
@ -101,12 +105,7 @@ public:
cell.update_possible_values();
}
std::make_heap(heap.begin(), heap.end(), sort_cells);
}
// TODO: Turn this into a member of Cell as opterator<
static bool sort_cells(const Cell& a, const Cell& b) {
return a.possible_values.size() < b.possible_values.size();
std::make_heap(heap.begin(), heap.end());
}
// Update the possible values which a cell can take for every cell in the heap
@ -115,12 +114,12 @@ public:
cell.update_possible_values();
}
std::sort_heap(heap.begin(), heap.end(), sort_cells);
std::sort_heap(heap.begin(), heap.end());
}
// Returns the best cell in the heap (the one with the most obvious choice)
Cell get_next_best_empty_cell() {
std::pop_heap(heap.begin(), heap.end(), sort_cells);
std::pop_heap(heap.begin(), heap.end());
Cell c = heap.back();
heap.pop_back();
@ -179,7 +178,7 @@ public:
// Put the cell back into the heap -- it couldn't be used for any valid solution, so we'll need it somewhere else later
heap.push_back(cell);
std::push_heap(heap.begin(), heap.end(), sort_cells);
std::push_heap(heap.begin(), heap.end());
return false;
}