]> git.tdb.fi Git - r2c2.git/commitdiff
Add a function to get a BlockIter from a TrackIter
authorMikko Rasa <tdb@tdb.fi>
Sat, 26 Jan 2013 20:35:15 +0000 (22:35 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 26 Jan 2013 20:35:15 +0000 (22:35 +0200)
source/libr2c2/blockiter.h
source/libr2c2/trackiter.cpp
source/libr2c2/trackiter.h

index 1e8a38378d3e74a2377de695f76df175767b2875..362c918e1f1c5c6f52893399204bda49c4c6f6e3 100644 (file)
@@ -1,9 +1,10 @@
 #ifndef LIBR2C2_BLOCKITER_H_
 #define LIBR2C2_BLOCKITER_H_
 
+#include "block.h"
+
 namespace R2C2 {
 
-class Block;
 class Route;
 class TrackIter;
 
index c636d27c84cef8a30d6c700bb6fd454c9d49c9e0..64d223e21cc3ce6142061ab7efc670e3fbd715f5 100644 (file)
@@ -1,4 +1,5 @@
 #include <algorithm>
+#include "blockiter.h"
 #include "track.h"
 #include "trackiter.h"
 #include "tracktype.h"
@@ -21,6 +22,40 @@ TrackIter::TrackIter(Track *t, unsigned e):
                throw out_of_range("TrackIter::TrackIter");
 }
 
+BlockIter TrackIter::block_iter() const
+{
+       if(!_track)
+               return BlockIter();
+
+       Block &block = _track->get_block();
+       const vector<Block::Endpoint> &beps = block.get_endpoints();
+
+       if(_track->get_type().is_turnout())
+       {
+               /* A turnouts is the only track in its block.  Go ahead and find the
+               matching endpoint in the block. */
+               for(unsigned i=0; i<beps.size(); ++i)
+                       if(beps[i].track==_track && beps[i].track_ep==_entry)
+                               return BlockIter(&block, i);
+       }
+       else
+       {
+               TrackIter rev = reverse();
+               while(rev && &rev.track()->get_block()==&block)
+               {
+                       TrackIter fwd = rev.reverse();
+
+                       for(unsigned i=0; i<beps.size(); ++i)
+                               if(beps[i].track==fwd.track() && beps[i].track_ep==fwd.entry())
+                                       return BlockIter(&block, i);
+
+                       rev = rev.next();
+               }
+       }
+
+       throw logic_error("internal error (didn't find block entry endpoint)");
+}
+
 const TrackType::Endpoint &TrackIter::endpoint() const
 {
        if(!_track)
index 63d98e20c2e5e950bcbee51f3b7500a07a8f24b0..3a633867738b6aa52664130b524eef7a7efc42cf 100644 (file)
@@ -7,6 +7,7 @@
 
 namespace R2C2 {
 
+class BlockIter;
 class Track;
 
 /**
@@ -24,6 +25,7 @@ public:
 
        Track *track() const { return _track; }
        unsigned entry() const { return _entry; }
+       BlockIter block_iter() const;
        const TrackType::Endpoint &endpoint() const;
 
 private: