diff --git a/constraint_solver/constraint_solver.cc b/constraint_solver/constraint_solver.cc index aef399f725..15ad102381 100644 --- a/constraint_solver/constraint_solver.cc +++ b/constraint_solver/constraint_solver.cc @@ -180,7 +180,7 @@ class FifoPriorityQueue : public SinglePriorityQueue { Cell* last_; Cell* free_cells_; }; -} // namespace +} // namespace class Queue { public: @@ -929,18 +929,15 @@ void Search::JumpBack() { CP_DO_FAIL(this); } -void Search::SetBranchSelector( - ResultCallback1* const bs) { - CHECK(bs == selector_ || selector_ == NULL || bs == NULL); - if (selector_ != bs) { - selector_.reset(bs); +Search* Solver::ActiveSearch() const { + switch (state_) { + case IN_SEARCH: + return searches_.back(); + default: + return NULL; } } -Search* LastSearch(Solver* const solver) { - return solver->searches_.back(); -} - namespace { class UndoBranchSelector : public Action { public: @@ -948,7 +945,7 @@ class UndoBranchSelector : public Action { virtual ~UndoBranchSelector() {} virtual void Run(Solver* const s) { if (s->SolveDepth() == depth_) { - LastSearch(s)->SetBranchSelector(NULL); + s->ActiveSearch()->SetBranchSelector(NULL); } } virtual string DebugString() const { @@ -978,6 +975,14 @@ class ApplyBranchSelector : public DecisionBuilder { }; } // namespace +void Search::SetBranchSelector( + ResultCallback1* const bs) { + CHECK(bs == selector_ || selector_ == NULL || bs == NULL); + if (selector_ != bs) { + selector_.reset(bs); + } +} + void Solver::SetBranchSelector( ResultCallback1* const bs) { bs->CheckIsRepeatable(); @@ -1036,6 +1041,7 @@ void Search::Clear() { monitors_.clear(); search_depth_ = 0; left_search_depth_ = 0; + selector_.reset(NULL); } void Search::EnterSearch() { @@ -1249,7 +1255,7 @@ class BalancingDecision : public Decision { virtual void Apply(Solver* const s) {} virtual void Refute(Solver* const s) {} }; -} +} // namespace Decision* Solver::MakeFailDecision() { return fail_decision_.get(); diff --git a/constraint_solver/constraint_solver.h b/constraint_solver/constraint_solver.h index 80cbafa4be..364350f6a1 100644 --- a/constraint_solver/constraint_solver.h +++ b/constraint_solver/constraint_solver.h @@ -2224,6 +2224,8 @@ class Solver { bool HasName(const PropagationBaseObject* object) const; // Adds a new demon and wraps it inside a DemonProfiler if necessary. Demon* RegisterDemon(Demon* const d); + // Returns the active search, NULL outside search. + Search* ActiveSearch() const; friend class BaseIntExpr; friend class Constraint; @@ -2239,7 +2241,6 @@ class Solver { #ifndef SWIG friend void InternalSaveBooleanVarValue(Solver* const, IntVar* const); friend void SetQueueCleanerOnFail(Solver* const, IntVar* const); - friend Search* LastSearch(Solver* const solver); template friend class SimpleRevFIFO; #endif