From: Mikko Rasa Date: Fri, 7 Jun 2013 16:18:04 +0000 (+0300) Subject: Streamline various block-related algorithms X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=cb4e53ba1b8ea41b978190de6668e9862b9cfea8;p=r2c2.git Streamline various block-related algorithms --- diff --git a/source/libr2c2/block.cpp b/source/libr2c2/block.cpp index bf77433..f681816 100644 --- a/source/libr2c2/block.cpp +++ b/source/libr2c2/block.cpp @@ -20,41 +20,29 @@ Block::Block(Layout &l, Track &start): turnout_id(start.get_turnout_id()), train(0) { - tracks.insert(&start); - start.set_block(this); + add_track(start); - list queue; - queue.push_back(&start); - - while(!queue.empty()) + if(start.get_type().is_turnout()) { - Track *track = queue.front(); - queue.erase(queue.begin()); - - const vector &links = track->get_links(); - for(unsigned i=0; iget_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id) - { - queue.push_back(links[i]); - add_track(*links[i]); - links[i]->set_block(this); - } - else - endpoints.push_back(Endpoint(track, i)); - } + unsigned nls = start.get_n_link_slots(); + for(unsigned i=0; i>i; ++i) - if(mask&(1<::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() { if(sensor_id) @@ -218,8 +190,7 @@ void Block::determine_id() Block::Endpoint::Endpoint(Track *t, unsigned e): track(t), track_ep(e), - link(0), - paths(0) + link(0) { } TrackIter Block::Endpoint::track_iter() const diff --git a/source/libr2c2/block.h b/source/libr2c2/block.h index 7d6211b..4309290 100644 --- a/source/libr2c2/block.h +++ b/source/libr2c2/block.h @@ -22,7 +22,6 @@ public: Track *track; unsigned track_ep; Block *link; - unsigned paths; Endpoint(Track *, unsigned); @@ -44,6 +43,7 @@ public: ~Block(); private: + virtual void on_track_added(Track &); virtual Validity check_validity(Track &) const; public: @@ -61,7 +61,6 @@ public: bool reserve(Train *); Train *get_train() const { return train; } private: - void find_paths(const TrackIter &, unsigned); void determine_id(); }; diff --git a/source/libr2c2/blockiter.cpp b/source/libr2c2/blockiter.cpp index 038600b..a3f6d87 100644 --- a/source/libr2c2/blockiter.cpp +++ b/source/libr2c2/blockiter.cpp @@ -40,24 +40,18 @@ const Block::Endpoint &BlockIter::endpoint() const int BlockIter::get_exit(const Route *route) const { const vector &eps = _block->get_endpoints(); - TrackIter t_iter = track_iter(); - - while(t_iter) + if(_block->get_turnout_id()) { - if(!_block->has_track(*t_iter)) - throw logic_error("internal error (block traversal escaped the block)"); - + /* The endpoints of a turnout block exactly correspond to the endpoints + of the track. */ + TrackIter t_iter = track_iter(); unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path()); - TrackIter t_exit = t_iter.reverse(path); - - for(unsigned i=0; iget_type().is_turnout()) { - TrackIter rev = reverse(); + /* Since there was no endpoint match, the preceding track can't be in a + different block. */ + TrackIter rev = flip(); TrackIter last; while(rev && &rev.track()->get_block()==&block) {