+std::shared_ptr<State> DFSExplorer::best_opened_state()
+{
+ int best_prio = 0; // cache the value for the best priority found so far (initialized to silence gcc)
+ auto best = end(opened_states_); // iterator to the state to explore having the best priority
+ auto valid = begin(opened_states_); // iterator marking the limit between states still to explore, and already
+ // explored ones
+
+ // Keep only still non-explored states (aid != -1), and record the one with the best (greater) priority.
+ for (auto current = begin(opened_states_); current != end(opened_states_); ++current) {
+ auto [aid, prio] = (*current)->next_transition_guided();
+ if (aid == -1)
+ continue;
+ if (valid != current)
+ *valid = std::move(*current);
+ if (best == end(opened_states_) || prio > best_prio) {
+ best_prio = prio;
+ best = valid;
+ }
+ ++valid;
+ }
+
+ std::shared_ptr<State> best_state;
+ if (best < valid) {
+ // There are non-explored states, and one of them has the best priority. Remove it from opened_states_ before
+ // returning.
+ best_state = std::move(*best);
+ --valid;
+ if (best != valid)
+ *best = std::move(*valid);
+ }
+ opened_states_.erase(valid, end(opened_states_));
+
+ return best_state;
+}
+