12 TrackIter::TrackIter():
17 TrackIter::TrackIter(Track *t, unsigned e):
21 if(_track && _entry>_track->get_type().get_endpoints().size())
22 throw out_of_range("TrackIter::TrackIter");
25 BlockIter TrackIter::block_iter() const
30 Block &block = _track->get_block();
31 const vector<Block::Endpoint> &beps = block.get_endpoints();
33 // See if this track matches an endpoint in the block
34 for(unsigned i=0; i<beps.size(); ++i)
35 if(beps[i].track==_track && beps[i].track_ep==_entry)
36 return BlockIter(&block, i);
38 if(!_track->get_type().is_turnout())
40 TrackIter rev = reverse();
42 while(rev && &rev.track()->get_block()==&block)
47 // If we ran out of tracks, return an empty iterator
52 TrackIter fwd = last.reverse();
53 for(unsigned i=0; i<beps.size(); ++i)
54 if(beps[i].track==fwd.track() && beps[i].track_ep==fwd.entry())
55 return BlockIter(&block, i);
58 throw logic_error("internal error (didn't find block entry endpoint)");
61 const TrackType::Endpoint &TrackIter::endpoint() const
64 throw logic_error("null track");
66 return _track->get_type().get_endpoint(_entry);
69 int TrackIter::get_exit(unsigned path) const
71 const vector<TrackType::Endpoint> &eps = _track->get_type().get_endpoints();
73 // Find an endpoint that's connected to the entry and has the requested path
74 for(unsigned i=0; i<eps.size(); ++i)
75 if(i!=_entry && eps[i].has_path(path) && eps[i].has_common_paths(eps[_entry]))
81 TrackIter TrackIter::next() const
86 return next(_track->get_active_path());
89 TrackIter TrackIter::next(unsigned path) const
94 int exit = get_exit(path);
99 result._track = _track->get_link(exit);
100 result._entry = (result._track ? result._track->get_link_slot(*_track) : 0);
105 TrackIter TrackIter::reverse() const
110 return reverse(_track->get_active_path());
113 TrackIter TrackIter::reverse(unsigned path) const
118 int exit = get_exit(path);
122 return TrackIter(_track, exit);
125 TrackIter TrackIter::flip() const
131 result._track = _track->get_link(_entry);
132 result._entry = (result._track ? result._track->get_link_slot(*_track) : 0);
137 Track &TrackIter::operator*() const
140 throw logic_error("null track");
145 bool TrackIter::operator==(const TrackIter &other) const
147 return _track==other._track && _entry==other._entry;
151 TrackLoopIter::TrackLoopIter():
155 TrackLoopIter::TrackLoopIter(Track *t, unsigned e):
157 _visited(new TrackList()),
158 _last(_visited->insert(_visited->end(), track())),
162 TrackLoopIter::TrackLoopIter(const TrackIter &i):
164 _visited(new TrackList()),
165 _last(_visited->insert(_visited->end(), track())),
169 TrackLoopIter::TrackLoopIter(const TrackIter &i, RefPtr<TrackList> v, const TrackList::iterator &l):
177 _looped = (_visited && find(_visited->begin(), _last, track())!=_last);
180 if(_last!=_visited->end())
182 _visited = new TrackList(_visited->begin(), _last);
183 _last = _visited->end();
185 _visited->push_back(track());
190 TrackLoopIter TrackLoopIter::next() const
192 return TrackLoopIter(TrackIter::next(), _visited, _last);
195 TrackLoopIter TrackLoopIter::next(unsigned path) const
197 return TrackLoopIter(TrackIter::next(path), _visited, _last);