]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/block.cpp
Make use of the unified storage class for other parts of Layout
[r2c2.git] / source / libr2c2 / block.cpp
index bf77433491df7b210631dd886c0d7435ced23e93..2df313e27c1f8c53104c4314908be2391eec7b52 100644 (file)
@@ -20,44 +20,32 @@ 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<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);
+       layout.add(*this);
 }
 
 Block::~Block()
@@ -74,11 +62,16 @@ Block::~Block()
                        blk->break_link(*this);
                }
 
-       layout.remove_block(*this);
+       layout.remove(*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)
@@ -172,27 +165,6 @@ bool Block::reserve(Train *t)
                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)
@@ -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