| 217 | } |
| 218 | |
| 219 | void BaseVehicleListWindow::BuildVehicleList() |
| 220 | { |
| 221 | if (!this->vehgroups.NeedRebuild()) return; |
| 222 | |
| 223 | Debug(misc, 3, "Building vehicle list type {} for company {} given index {}", this->vli.type, this->vli.company, this->vli.index); |
| 224 | |
| 225 | this->vehgroups.clear(); |
| 226 | |
| 227 | GenerateVehicleSortList(&this->vehicles, this->vli); |
| 228 | |
| 229 | CargoTypes used = 0; |
| 230 | for (const Vehicle *v : this->vehicles) { |
| 231 | for (const Vehicle *u = v; u != nullptr; u = u->Next()) { |
| 232 | if (u->cargo_cap > 0) SetBit(used, u->cargo_type); |
| 233 | } |
| 234 | } |
| 235 | this->used_cargoes = used; |
| 236 | |
| 237 | if (this->grouping == GB_NONE) { |
| 238 | uint max_unitnumber = 0; |
| 239 | for (auto it = this->vehicles.begin(); it != this->vehicles.end(); ++it) { |
| 240 | this->vehgroups.emplace_back(it, it + 1); |
| 241 | |
| 242 | max_unitnumber = std::max<uint>(max_unitnumber, (*it)->unitnumber); |
| 243 | } |
| 244 | this->unitnumber_digits = CountDigitsForAllocatingSpace(max_unitnumber); |
| 245 | } else { |
| 246 | /* Sort by the primary vehicle; we just want all vehicles that share the same orders to form a contiguous range. */ |
| 247 | std::stable_sort(this->vehicles.begin(), this->vehicles.end(), [](const Vehicle * const &u, const Vehicle * const &v) { |
| 248 | return u->FirstShared() < v->FirstShared(); |
| 249 | }); |
| 250 | |
| 251 | uint max_num_vehicles = 0; |
| 252 | |
| 253 | VehicleList::const_iterator begin = this->vehicles.begin(); |
| 254 | while (begin != this->vehicles.end()) { |
| 255 | VehicleList::const_iterator end = std::find_if_not(begin, this->vehicles.cend(), [first_shared = (*begin)->FirstShared()](const Vehicle * const &v) { |
| 256 | return v->FirstShared() == first_shared; |
| 257 | }); |
| 258 | |
| 259 | this->vehgroups.emplace_back(begin, end); |
| 260 | |
| 261 | max_num_vehicles = std::max<uint>(max_num_vehicles, static_cast<uint>(end - begin)); |
| 262 | |
| 263 | begin = end; |
| 264 | } |
| 265 | |
| 266 | this->unitnumber_digits = CountDigitsForAllocatingSpace(max_num_vehicles); |
| 267 | } |
| 268 | this->FilterVehicleList(); |
| 269 | |
| 270 | this->vehgroups.RebuildDone(); |
| 271 | this->vscroll->SetCount(this->vehgroups.size()); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Check whether a single vehicle should pass the filter. |
no test coverage detected