]> git.tdb.fi Git - r2c2.git/commitdiff
Maintain a separate list of pending RoutingStates
authorMikko Rasa <tdb@tdb.fi>
Sun, 30 Mar 2014 17:13:53 +0000 (20:13 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 30 Mar 2014 17:15:11 +0000 (20:15 +0300)
source/libr2c2/trainrouteplanner.cpp
source/libr2c2/trainrouteplanner.h

index 3f8739347d455405f1f2a6fef03c6b2c02c5e3a9..22ac5400ea4fb050437ccdff14de77dbafc483e9 100644 (file)
@@ -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<TrainRoutingInfo>::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<RoutingStep>::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<RoutingStep> 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)
index 6dab635818f0c92163188f1e93e2a6ffa80a2119..7d1ff9a0709a61f4fdfe413687cf990790760caf 100644 (file)
@@ -98,12 +98,14 @@ private:
 
        std::vector<TrainRoutingInfo> routed_trains;
        std::list<RoutingStep> steps;
+       std::list<RoutingStep> queue;
 
 public:
        TrainRoutePlanner(Layout &);
 
        void plan();
 private:
+       const RoutingStep &get_step();
        void add_steps(const RoutingStep &);
        void create_routes(const RoutingStep &);
 };