turnout_id(start.get_turnout_id()),
train(0)
{
- tracks.insert(&start);
- start.set_block(this);
+ add_track(start);
- list<Track *> queue;
- queue.push_back(&start);
-
- while(!queue.empty())
+ if(start.get_type().is_turnout())
{
- Track *track = queue.front();
- queue.erase(queue.begin());
-
- const vector<Track *> &links = track->get_links();
- for(unsigned i=0; i<links.size(); ++i)
- if(links[i] && !tracks.count(links[i]))
- {
- if(links[i]->get_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<nls; ++i)
+ endpoints.push_back(Endpoint(&start, i));
}
-
- determine_id();
-
- for(unsigned i=0; i<endpoints.size(); ++i)
+ else
{
- unsigned path = 1<<i;
- endpoints[i].paths |= path;
- find_paths(endpoints[i].track_iter(), path);
+ unsigned nls = start.get_n_link_slots();
+ for(unsigned i=0; i<nls; ++i)
+ {
+ TrackIter iter = TrackIter(&start, i).flip();
+ for(; (iter && check_validity(*iter)==VALID); iter=iter.next())
+ add_track(*iter);
+ if((iter = iter.flip()))
+ endpoints.push_back(Endpoint(iter.track(), iter.entry()));
+ }
}
+ determine_id();
+
sensor = new TrackCircuit(layout, *this);
layout.add_block(*this);
delete sensor;
}
+void Block::on_track_added(Track &track)
+{
+ track.set_block(this);
+}
+
TrackChain::Validity Block::check_validity(Track &track) const
{
if(track.get_sensor_id()!=sensor_id || track.get_turnout_id()!=turnout_id)
return false;
}
-void Block::find_paths(const TrackIter &track, unsigned path)
-{
- unsigned mask = track.endpoint().paths;
- for(unsigned i=0; mask>>i; ++i)
- if(mask&(1<<i))
- {
- 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()
{
if(sensor_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
int BlockIter::get_exit(const Route *route) const
{
const vector<Block::Endpoint> &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; i<eps.size(); ++i)
- if(eps[i].track==t_exit.track() && eps[i].track_ep==t_exit.entry())
- return i;
-
- t_iter = t_exit.flip();
+ return t_iter.reverse(path).entry();
}
-
- return -1;
+ else if(eps.size()==2)
+ return 1-_entry;
+ else
+ return -1;
}
BlockIter BlockIter::next(const Route *route) const