+unsigned TrackType::coerce_path(unsigned entry, unsigned path) const
+{
+ const Endpoint &ep = get_endpoint(entry);
+ if(ep.has_path(path))
+ return path;
+
+ unsigned paths = get_paths();
+ if(paths>>(1<<state_bits))
+ {
+ /* There are more paths than can be expressed with state_bits, so
+ multiple paths are set at once. See if one of the alternatives fits. */
+ unsigned step = 1<<state_bits;
+ for(unsigned p=path+step; paths>>p; p+=step)
+ if(ep.has_path(p))
+ return p;
+ }
+
+ // Find an endpoint that's connected to the entry and has the requested path
+ for(vector<Endpoint>::const_iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
+ if(i->has_path(path) && i->has_common_paths(ep))
+ {
+ unsigned p = 1;
+ for(unsigned m=i->paths&ep.paths; m>>p; ++p) ;
+ return p-1;
+ }
+
+ // TODO crossings fall here
+ throw logic_error("TrackType::coerce_path");
+}
+