]> git.tdb.fi Git - r2c2.git/commitdiff
Refactor TrackIter::block_iter
authorMikko Rasa <tdb@tdb.fi>
Sat, 25 May 2013 22:54:24 +0000 (01:54 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 25 May 2013 22:54:24 +0000 (01:54 +0300)
The new implementation is slightly more efficient in certain cases and
won't choke at the end of a line.

source/libr2c2/trackiter.cpp

index c3bb7d4ae5db5e8ab16def585547dfec090cb101..31ebf08e1f1c59711208c0f51722480cbc529c46 100644 (file)
@@ -30,27 +30,29 @@ BlockIter TrackIter::block_iter() const
        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
+       // See if this track matches an 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);
+
+       if(!_track->get_type().is_turnout())
        {
                TrackIter rev = reverse();
+               TrackIter last;
                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);
-
+                       last = rev;
                        rev = rev.next();
+
+                       // If we ran out of tracks, return an empty iterator
+                       if(!rev)
+                               return BlockIter();
                }
+
+               TrackIter fwd = last.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);
        }
 
        throw logic_error("internal error (didn't find block entry endpoint)");