| 271 | } |
| 272 | |
| 273 | SpeedInUnits MaxspeedConverter::ClosestValidMacro(SpeedInUnits const & speed) const |
| 274 | { |
| 275 | auto it = m_speedToMacro.lower_bound(speed); |
| 276 | if (it == m_speedToMacro.end()) |
| 277 | { |
| 278 | --it; |
| 279 | } |
| 280 | else if (speed == it->first) |
| 281 | { |
| 282 | return speed; |
| 283 | } |
| 284 | else if (it != m_speedToMacro.begin()) |
| 285 | { |
| 286 | auto it2 = it; |
| 287 | --it2; |
| 288 | |
| 289 | ASSERT(speed.GetUnits() == it->first.GetUnits() || speed.GetUnits() == it2->first.GetUnits(), ()); |
| 290 | auto const Diff = [&speed](SpeedInUnits const & rhs) |
| 291 | { |
| 292 | if (speed.GetUnits() != rhs.GetUnits()) |
| 293 | return std::numeric_limits<int>::max(); |
| 294 | return abs(int(rhs.GetSpeed()) - int(speed.GetSpeed())); |
| 295 | }; |
| 296 | |
| 297 | if (Diff(it2->first) < Diff(it->first)) |
| 298 | it = it2; |
| 299 | } |
| 300 | |
| 301 | return it->first; |
| 302 | } |
| 303 | |
| 304 | // static |
| 305 | MaxspeedConverter const & MaxspeedConverter::Instance() |