1
0
Fork 0

Codechange: Use sorted vector for NewGRF parameter value names. (#13326)

This replaces use of a std::map per GRF-parameter.
pull/13330/head
Peter Nelson 2025-01-17 19:33:11 +00:00 committed by GitHub
parent 610026ef17
commit 2f0b52d5b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 19 deletions

View File

@ -8470,14 +8470,11 @@ static bool ChangeGRFParamValueNames(ByteReader &buf)
uint8_t langid = buf.ReadByte(); uint8_t langid = buf.ReadByte();
std::string_view name_string = buf.ReadString(); std::string_view name_string = buf.ReadString();
auto val_name = _cur_parameter->value_names.find(id); auto it = std::ranges::lower_bound(_cur_parameter->value_names, id, std::less{}, &GRFParameterInfo::ValueName::first);
if (val_name != _cur_parameter->value_names.end()) { if (it == std::end(_cur_parameter->value_names) || it->first != id) {
AddGRFTextToList(val_name->second, langid, _cur.grfconfig->ident.grfid, false, name_string); it = _cur_parameter->value_names.emplace(it, id, GRFTextList{});
} else {
GRFTextList list;
AddGRFTextToList(list, langid, _cur.grfconfig->ident.grfid, false, name_string);
_cur_parameter->value_names[id] = list;
} }
AddGRFTextToList(it->second, langid, _cur.grfconfig->ident.grfid, false, name_string);
type = buf.ReadByte(); type = buf.ReadByte();
} }

View File

@ -211,13 +211,13 @@ void GRFConfig::SetValue(const GRFParameterInfo &info, uint32_t value)
*/ */
void GRFParameterInfo::Finalize() void GRFParameterInfo::Finalize()
{ {
this->complete_labels = true; /* Remove value names outside of the permitted range of values. */
for (uint32_t value = this->min_value; value <= this->max_value; value++) { auto it = std::remove_if(std::begin(this->value_names), std::end(this->value_names),
if (this->value_names.count(value) == 0) { [this](const ValueName &vn) { return vn.first < this->min_value || vn.first > this->max_value; });
this->complete_labels = false; this->value_names.erase(it, std::end(this->value_names));
break;
} /* Test if the number of named values matches the full ranges of values. -1 because the range is inclusive. */
} this->complete_labels = (this->max_value - this->min_value) == std::size(this->value_names) - 1;
} }
/** /**

View File

@ -145,7 +145,8 @@ struct GRFParameterInfo {
bool complete_labels = false; ///< True if all values have a label. bool complete_labels = false; ///< True if all values have a label.
std::map<uint32_t, GRFTextList> value_names = {}; ///< Names for each value. using ValueName = std::pair<uint32_t, GRFTextList>;
std::vector<ValueName> value_names; ///< Names for each value.
void Finalize(); void Finalize();
}; };

View File

@ -298,8 +298,8 @@ struct NewGRFParametersWindow : public Window {
} }
SetDParam(2, STR_JUST_INT); SetDParam(2, STR_JUST_INT);
SetDParam(3, current_value); SetDParam(3, current_value);
auto it = par_info.value_names.find(current_value); auto it = std::ranges::lower_bound(par_info.value_names, current_value, std::less{}, &GRFParameterInfo::ValueName::first);
if (it != par_info.value_names.end()) { if (it != std::end(par_info.value_names) && it->first == current_value) {
const char *label = GetGRFStringFromGRFText(it->second); const char *label = GetGRFStringFromGRFText(it->second);
if (label != nullptr) { if (label != nullptr) {
SetDParam(2, STR_JUST_RAW_STRING); SetDParam(2, STR_JUST_RAW_STRING);
@ -393,8 +393,8 @@ struct NewGRFParametersWindow : public Window {
this->closing_dropdown = false; this->closing_dropdown = false;
DropDownList list; DropDownList list;
for (uint32_t i = par_info.min_value; i <= par_info.max_value; i++) { for (const auto &[value, name] : par_info.value_names) {
list.push_back(MakeDropDownListStringItem(GetGRFStringFromGRFText(par_info.value_names.find(i)->second), i)); list.push_back(MakeDropDownListStringItem(GetGRFStringFromGRFText(name), value));
} }
ShowDropDownListAt(this, std::move(list), old_val, WID_NP_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE); ShowDropDownListAt(this, std::move(list), old_val, WID_NP_SETTING_DROPDOWN, wi_rect, COLOUR_ORANGE);