diff --git a/src/cargotype.cpp b/src/cargotype.cpp index 6cc02f63a3..4a2dd6a32d 100644 --- a/src/cargotype.cpp +++ b/src/cargotype.cpp @@ -14,7 +14,7 @@ #include "newgrf_cargo.h" #include "string_func.h" #include "strings_func.h" -#include "core/sort_func.hpp" +#include #include "table/sprites.h" #include "table/strings.h" @@ -132,56 +132,54 @@ SpriteID CargoSpec::GetCargoIcon() const return sprite; } -const CargoSpec *_sorted_cargo_specs[NUM_CARGO]; ///< Cargo specifications sorted alphabetically by name. -uint8 _sorted_cargo_specs_size; ///< Number of cargo specifications stored at the _sorted_cargo_specs array (including special cargoes). -uint8 _sorted_standard_cargo_specs_size; ///< Number of standard cargo specifications stored at the _sorted_cargo_specs array. +std::vector _sorted_cargo_specs; ///< Cargo specifications sorted alphabetically by name. +uint8 _sorted_standard_cargo_specs_size; ///< Number of standard cargo specifications stored in the _sorted_cargo_specs array. /** Sort cargo specifications by their name. */ -static int CDECL CargoSpecNameSorter(const CargoSpec * const *a, const CargoSpec * const *b) +static bool CargoSpecNameSorter(const CargoSpec * const &a, const CargoSpec * const &b) { static char a_name[64]; static char b_name[64]; - GetString(a_name, (*a)->name, lastof(a_name)); - GetString(b_name, (*b)->name, lastof(b_name)); + GetString(a_name, a->name, lastof(a_name)); + GetString(b_name, b->name, lastof(b_name)); int res = strnatcmp(a_name, b_name); // Sort by name (natural sorting). /* If the names are equal, sort by cargo bitnum. */ - return (res != 0) ? res : ((*a)->bitnum - (*b)->bitnum); + return (res != 0) ? res < 0 : (a->bitnum < b->bitnum); } /** Sort cargo specifications by their cargo class. */ -static int CDECL CargoSpecClassSorter(const CargoSpec * const *a, const CargoSpec * const *b) +static bool CargoSpecClassSorter(const CargoSpec * const &a, const CargoSpec * const &b) { - int res = ((*b)->classes & CC_PASSENGERS) - ((*a)->classes & CC_PASSENGERS); + int res = (b->classes & CC_PASSENGERS) - (a->classes & CC_PASSENGERS); if (res == 0) { - res = ((*b)->classes & CC_MAIL) - ((*a)->classes & CC_MAIL); + res = (b->classes & CC_MAIL) - (a->classes & CC_MAIL); if (res == 0) { - res = ((*a)->classes & CC_SPECIAL) - ((*b)->classes & CC_SPECIAL); + res = (a->classes & CC_SPECIAL) - (b->classes & CC_SPECIAL); if (res == 0) { return CargoSpecNameSorter(a, b); } } } - return res; + return res < 0; } /** Initialize the list of sorted cargo specifications. */ void InitializeSortedCargoSpecs() { - _sorted_cargo_specs_size = 0; + _sorted_cargo_specs.clear(); const CargoSpec *cargo; /* Add each cargo spec to the list. */ FOR_ALL_CARGOSPECS(cargo) { - _sorted_cargo_specs[_sorted_cargo_specs_size] = cargo; - _sorted_cargo_specs_size++; + _sorted_cargo_specs.push_back(cargo); } /* Sort cargo specifications by cargo class and name. */ - QSortT(_sorted_cargo_specs, _sorted_cargo_specs_size, &CargoSpecClassSorter); + std::sort(_sorted_cargo_specs.begin(), _sorted_cargo_specs.end(), &CargoSpecClassSorter); _standard_cargo_mask = 0; diff --git a/src/cargotype.h b/src/cargotype.h index b35eef9659..aa67561d88 100644 --- a/src/cargotype.h +++ b/src/cargotype.h @@ -17,6 +17,7 @@ #include "gfx_type.h" #include "strings_type.h" #include "landscape_type.h" +#include /** Globally unique label of a cargo type. */ typedef uint32 CargoLabel; @@ -137,8 +138,7 @@ CargoID GetCargoIDByLabel(CargoLabel cl); CargoID GetCargoIDByBitnum(uint8 bitnum); void InitializeSortedCargoSpecs(); -extern const CargoSpec *_sorted_cargo_specs[NUM_CARGO]; -extern uint8 _sorted_cargo_specs_size; +extern std::vector _sorted_cargo_specs; extern uint8 _sorted_standard_cargo_specs_size; /** @@ -163,7 +163,7 @@ static inline bool IsCargoInClass(CargoID c, CargoClass cc) * @param var Reference getting the cargospec. * @see CargoSpec */ -#define FOR_ALL_SORTED_CARGOSPECS(var) for (uint8 index = 0; index < _sorted_cargo_specs_size && (var = _sorted_cargo_specs[index], true) ; index++) +#define FOR_ALL_SORTED_CARGOSPECS(var) for (uint8 index = 0; index < _sorted_cargo_specs.size() && (var = _sorted_cargo_specs[index], true) ; index++) /** * Loop header for iterating over 'real' cargoes, sorted by name. Phony cargoes like regearing cargoes are skipped. diff --git a/src/smallmap_gui.cpp b/src/smallmap_gui.cpp index 1249851735..69ce56d345 100644 --- a/src/smallmap_gui.cpp +++ b/src/smallmap_gui.cpp @@ -210,7 +210,7 @@ void BuildLinkStatsLegend() memset(_legend_linkstats, 0, sizeof(_legend_linkstats)); uint i = 0; - for (; i < _sorted_cargo_specs_size; ++i) { + for (; i < _sorted_cargo_specs.size(); ++i) { const CargoSpec *cs = _sorted_cargo_specs[i]; _legend_linkstats[i].legend = cs->name;