From: Mikko Rasa Date: Sun, 30 Mar 2014 17:13:53 +0000 (+0300) Subject: Maintain a separate list of pending RoutingStates X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b42a312323d36775ab550630b3ee818c3b948bf6;p=r2c2.git Maintain a separate list of pending RoutingStates --- diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index 3f87393..22ac540 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -21,36 +21,44 @@ TrainRoutePlanner::TrainRoutePlanner(Layout &layout) routed_trains.push_back(info); } - steps.push_back(RoutingStep()); - RoutingStep &start = steps.back(); + queue.push_back(RoutingStep()); + RoutingStep &start = queue.back(); for(vector::iterator i=routed_trains.begin(); i!=routed_trains.end(); ++i) start.trains.push_back(TrainRoutingState(*i)); + start.update_estimate(); } void TrainRoutePlanner::plan() { const RoutingStep *goal = 0; - for(list::iterator i=steps.begin(); i!=steps.end(); ++i) + while(!queue.empty()) { - if(i->is_goal()) + const RoutingStep &step = get_step(); + if(step.is_goal()) { - goal = &*i; + goal = &step; break; } - add_steps(*i); + add_steps(step); } if(goal) create_routes(*goal); } +const TrainRoutePlanner::RoutingStep &TrainRoutePlanner::get_step() +{ + steps.splice(steps.end(), queue, queue.begin()); + return steps.back(); +} + void TrainRoutePlanner::add_steps(const RoutingStep &step) { list new_steps; step.create_successors(new_steps); new_steps.sort(); - steps.merge(new_steps); + queue.merge(new_steps); } void TrainRoutePlanner::create_routes(const RoutingStep &goal) diff --git a/source/libr2c2/trainrouteplanner.h b/source/libr2c2/trainrouteplanner.h index 6dab635..7d1ff9a 100644 --- a/source/libr2c2/trainrouteplanner.h +++ b/source/libr2c2/trainrouteplanner.h @@ -98,12 +98,14 @@ private: std::vector routed_trains; std::list steps; + std::list queue; public: TrainRoutePlanner(Layout &); void plan(); private: + const RoutingStep &get_step(); void add_steps(const RoutingStep &); void create_routes(const RoutingStep &); };