]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/trackiter.cpp
Streamline various block-related algorithms
[r2c2.git] / source / libr2c2 / trackiter.cpp
1 #include <algorithm>
2 #include "blockiter.h"
3 #include "track.h"
4 #include "trackiter.h"
5 #include "tracktype.h"
6
7 using namespace std;
8 using namespace Msp;
9
10 namespace R2C2 {
11
12 TrackIter::TrackIter():
13         _track(0),
14         _entry(0)
15 { }
16
17 TrackIter::TrackIter(Track *t, unsigned e):
18         _track(t),
19         _entry(t ? e : 0)
20 {
21         if(_track && _entry>_track->get_type().get_endpoints().size())
22                 throw out_of_range("TrackIter::TrackIter");
23 }
24
25 BlockIter TrackIter::block_iter() const
26 {
27         if(!_track)
28                 return BlockIter();
29
30         Block &block = _track->get_block();
31         const vector<Block::Endpoint> &beps = block.get_endpoints();
32
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);
37
38         if(!_track->get_type().is_turnout())
39         {
40                 /* Since there was no endpoint match, the preceding track can't be in a
41                 different block. */
42                 TrackIter rev = flip();
43                 TrackIter last;
44                 while(rev && &rev.track()->get_block()==&block)
45                 {
46                         last = rev;
47                         rev = rev.next();
48                 }
49
50                 // If we ran out of tracks, return an empty iterator
51                 if(!rev)
52                         return BlockIter();
53
54                 TrackIter fwd = last.reverse();
55                 for(unsigned i=0; i<beps.size(); ++i)
56                         if(beps[i].track==fwd.track() && beps[i].track_ep==fwd.entry())
57                                 return BlockIter(&block, i);
58         }
59
60         throw logic_error("internal error (didn't find block entry endpoint)");
61 }
62
63 const TrackType::Endpoint &TrackIter::endpoint() const
64 {
65         if(!_track)
66                 throw logic_error("null track");
67
68         return _track->get_type().get_endpoint(_entry);
69 }
70
71 int TrackIter::get_exit(unsigned path) const
72 {
73         const vector<TrackType::Endpoint> &eps = _track->get_type().get_endpoints();
74         
75         // Find an endpoint that's connected to the entry and has the requested path
76         for(unsigned i=0; i<eps.size(); ++i)
77                 if(i!=_entry && eps[i].has_path(path) && eps[i].has_common_paths(eps[_entry]))
78                         return i;
79
80         return -1;
81 }
82
83 TrackIter TrackIter::next() const
84 {
85         if(!_track)
86                 return TrackIter();
87
88         return next(_track->get_active_path());
89 }
90
91 TrackIter TrackIter::next(unsigned path) const
92 {
93         if(!_track)
94                 return TrackIter();
95
96         int exit = get_exit(path);
97         if(exit<0)
98                 return TrackIter();
99
100         TrackIter result;
101         result._track = _track->get_link(exit);
102         result._entry = (result._track ? result._track->get_link_slot(*_track) : 0);
103
104         return result;
105 }
106
107 TrackIter TrackIter::reverse() const
108 {
109         if(!_track)
110                 return TrackIter();
111
112         return reverse(_track->get_active_path());
113 }
114
115 TrackIter TrackIter::reverse(unsigned path) const
116 {
117         if(!_track)
118                 return TrackIter();
119
120         int exit = get_exit(path);
121         if(exit<0)
122                 return TrackIter();
123
124         return TrackIter(_track, exit);
125 }
126
127 TrackIter TrackIter::flip() const
128 {
129         if(!_track)
130                 return TrackIter();
131
132         TrackIter result;
133         result._track = _track->get_link(_entry);
134         result._entry = (result._track ? result._track->get_link_slot(*_track) : 0);
135
136         return result;
137 }
138
139 Track &TrackIter::operator*() const
140 {
141         if(!_track)
142                 throw logic_error("null track");
143
144         return *_track;
145 }
146
147 bool TrackIter::operator==(const TrackIter &other) const
148 {
149         return _track==other._track && _entry==other._entry;
150 }
151
152
153 TrackLoopIter::TrackLoopIter():
154         _looped(false)
155 { }
156
157 TrackLoopIter::TrackLoopIter(Track *t, unsigned e):
158         TrackIter(t, e),
159         _visited(new TrackList()),
160         _last(_visited->insert(_visited->end(), track())),
161         _looped(false)
162 { }
163
164 TrackLoopIter::TrackLoopIter(const TrackIter &i):
165         TrackIter(i),
166         _visited(new TrackList()),
167         _last(_visited->insert(_visited->end(), track())),
168         _looped(false)
169 { }
170
171 TrackLoopIter::TrackLoopIter(const TrackIter &i, RefPtr<TrackList> v, const TrackList::iterator &l):
172         TrackIter(i),
173         _looped(false)
174 {
175         if(track())
176         {
177                 _visited = v;
178                 _last = l;
179                 _looped = (_visited && find(_visited->begin(), _last, track())!=_last);
180
181                 ++_last;
182                 if(_last!=_visited->end())
183                 {
184                         _visited = new TrackList(_visited->begin(), _last);
185                         _last = _visited->end();
186                 }
187                 _visited->push_back(track());
188                 --_last;
189         }
190 }
191
192 TrackLoopIter TrackLoopIter::next() const
193 {
194         return TrackLoopIter(TrackIter::next(), _visited, _last);
195 }
196
197 TrackLoopIter TrackLoopIter::next(unsigned path) const
198 {
199         return TrackLoopIter(TrackIter::next(path), _visited, _last);
200 }
201
202 } // namespace R2C2