offset += distance;
back_offset += distance;
- OccupiedTrack *last_occ = occupied_tracks;
- for(unsigned n=occupied_tracks->n_tracks; n>1; --n)
- last_occ = last_occ->next;
+ unsigned count_to_free = 0;
+ unsigned last_sensor_addr = 0;
+ float distance_after_sensor = 0;
+ OccupiedTrack *occ = occupied_tracks;
+ for(unsigned n=occupied_tracks->n_tracks; n>0; --n)
+ {
+ if(unsigned saddr = occ->track->get_sensor_address())
+ {
+ if(saddr!=last_sensor_addr)
+ {
+ count_to_free = 0;
+ distance_after_sensor = 0;
+ }
+ last_sensor_addr = saddr;
+ }
+
+ ++count_to_free;
+ distance_after_sensor += occ->path_length;
+
+ occ = occ->next;
+ }
- // XXX What if there's multiple tracks to remove?
- if(back_offset>last_occ->path_length)
+ if(count_to_free && back_offset>distance_after_sensor)
{
- back_offset -= last_occ->path_length;
+ back_offset -= distance_after_sensor;
if(occupied_tracks->refcount>1)
{
--occupied_tracks->refcount;
occupied_tracks = new OccupiedTrack(*occupied_tracks);
}
- --occupied_tracks->n_tracks;
+ occupied_tracks->n_tracks -= count_to_free;
}
distance_traveled += distance;