]> git.tdb.fi Git - r2c2.git/commitdiff
Rewrite Block::find_paths using TrackIter
authorMikko Rasa <tdb@tdb.fi>
Wed, 27 Oct 2010 18:40:27 +0000 (18:40 +0000)
committerMikko Rasa <tdb@tdb.fi>
Wed, 27 Oct 2010 18:40:27 +0000 (18:40 +0000)
source/libmarklin/block.cpp
source/libmarklin/block.h

index 02b6da5c7203dffaf346e4ad3690a0c77b1d2bee..b9b65ad710dca9dd833177ee993b31562c3c4b5b 100644 (file)
@@ -56,8 +56,7 @@ Block::Block(Layout &l, Track &start):
        {
                unsigned path = 1<<i;
                endpoints[i].paths |= path;
-               set<Track *> visited;
-               find_paths(*endpoints[i].track, endpoints[i].track_ep, path, visited);
+               find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path);
        }
 
        layout.add_block(*this);
@@ -167,28 +166,25 @@ bool Block::reserve(Train *t)
                return false;
 }
 
-void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track *> &visited)
+void Block::find_paths(TrackIter track, unsigned path)
 {
-       visited.insert(&track);
-
-       const vector<TrackType::Endpoint> &eps = track.get_type().get_endpoints();
-       for(unsigned i=0; i<eps.size(); ++i)
-       {
-               if(i==track_ep) continue;
-               Track *link = track.get_link(i);
-               if(!link) continue;
-               if(visited.count(link)) continue;
-               if(!(eps[i].paths&eps[track_ep].paths)) continue;
-
-               if(tracks.count(link))
-                       find_paths(*link, link->get_endpoint_by_link(track), path, visited);
-               else
+       unsigned mask = track->get_type().get_endpoints()[track.entry()].paths;
+       for(unsigned i=0; mask>>i; ++i)
+               if(mask&(1<<i))
                {
-                       for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
-                               if(j->track==&track && j->track_ep==i)
-                                       j->paths |= path;
+                       TrackIter next = track.next(i);
+                       if(!next)
+                               continue;
+                       else if(has_track(*next))
+                               find_paths(track.next(i), path);
+                       else
+                       {
+                               next = next.flip();
+                               for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
+                                       if(j->track==next.track() && j->track_ep==next.entry())
+                                               j->paths |= path;
+                       }
                }
-       }
 }
 
 void Block::determine_id()
index cf5f3e784986f91ccc9010129a4d64213fb03bce..2aed9a40df680f3a769befd02cbe0357b30dd5fd 100644 (file)
@@ -16,6 +16,7 @@ namespace Marklin {
 
 class Layout;
 class Route;
+class TrackIter;
 class Train;
 
 class Block
@@ -59,7 +60,7 @@ public:
        Train *get_train() const { return train; }
        void print_debug();
 private:
-       void find_paths(Track &, unsigned, unsigned, std::set<Track *> &);
+       void find_paths(TrackIter, unsigned);
        void determine_id();
 };