Skip to content

Commit

Permalink
Codechange: Replaced SmallVector::Append() with std::vector::[push|em…
Browse files Browse the repository at this point in the history
…place]_back()
  • Loading branch information
M3Henry committed Feb 19, 2019
1 parent 7229434 commit aec6277
Show file tree
Hide file tree
Showing 79 changed files with 397 additions and 395 deletions.
2 changes: 1 addition & 1 deletion src/ai/ai_gui.cpp
Expand Up @@ -481,7 +481,7 @@ struct AISettingsWindow : public Window {

DropDownList *list = new DropDownList();
for (int i = config_item.min_value; i <= config_item.max_value; i++) {
*list->Append() = new DropDownListCharStringItem(config_item.labels->Find(i)->second, i, false);
list->push_back(new DropDownListCharStringItem(config_item.labels->Find(i)->second, i, false));
}

ShowDropDownListAt(this, list, old_val, -1, wi_rect, COLOUR_ORANGE, true);
Expand Down
2 changes: 1 addition & 1 deletion src/airport_gui.cpp
Expand Up @@ -219,7 +219,7 @@ class BuildAirportWindow : public PickerWindowBase {
DropDownList *list = new DropDownList();

for (uint i = 0; i < AirportClass::GetClassCount(); i++) {
*list->Append() = new DropDownListStringItem(AirportClass::Get((AirportClassID)i)->name, i, false);
list->push_back(new DropDownListStringItem(AirportClass::Get((AirportClassID)i)->name, i, false));
}

return list;
Expand Down
6 changes: 3 additions & 3 deletions src/autoreplace_gui.cpp
Expand Up @@ -140,7 +140,7 @@ class ReplaceVehicleWindow : public Window {
if (!CheckAutoreplaceValidity(this->sel_engine[0], eid, _local_company)) continue;
}

*list->Append() = eid;
list->push_back(eid);
if (eid == this->sel_engine[side]) selected_engine = eid; // The selected engine is still in the list
}
this->sel_engine[side] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
Expand Down Expand Up @@ -461,8 +461,8 @@ class ReplaceVehicleWindow : public Window {

case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: {
DropDownList *list = new DropDownList();
*list->Append() = new DropDownListStringItem(STR_REPLACE_ENGINES, 1, false);
*list->Append() = new DropDownListStringItem(STR_REPLACE_WAGONS, 0, false);
list->push_back(new DropDownListStringItem(STR_REPLACE_ENGINES, 1, false));
list->push_back(new DropDownListStringItem(STR_REPLACE_WAGONS, 0, false));
ShowDropDownList(this, list, this->replace_engines ? 1 : 0, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN);
break;
}
Expand Down
9 changes: 5 additions & 4 deletions src/bridge_gui.cpp
Expand Up @@ -416,12 +416,13 @@ void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transpo
for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
if (CheckBridgeAvailability(brd_type, bridge_len).Succeeded()) {
/* bridge is accepted, add to list */
BuildBridgeData *item = bl->Append();
item->index = brd_type;
item->spec = GetBridgeSpec(brd_type);
/*C++17: BuildBridgeData &item = */ bl->emplace_back();
BuildBridgeData &item = bl->back();
item.index = brd_type;
item.spec = GetBridgeSpec(brd_type);
/* Add to terraforming & bulldozing costs the cost of the
* bridge itself (not computed with DC_QUERY_COST) */
item->cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price[PR_BUILD_BRIDGE] * item->spec->price) >> 8) + infra_cost;
item.cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price[PR_BUILD_BRIDGE] * item.spec->price) >> 8) + infra_cost;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/build_vehicle_gui.cpp
Expand Up @@ -1150,7 +1150,7 @@ struct BuildVehicleWindow : Window {
/* Filter now! So num_engines and num_wagons is valid */
if (!FilterSingleEngine(eid)) continue;

*this->eng_list.Append() = eid;
this->eng_list.push_back(eid);

if (rvi->railveh_type != RAILVEH_WAGON) {
num_engines++;
Expand Down Expand Up @@ -1188,7 +1188,7 @@ struct BuildVehicleWindow : Window {
EngineID eid = e->index;
if (!IsEngineBuildable(eid, VEH_ROAD, _local_company)) continue;
if (!HasBit(this->filter.roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD)) continue;
*this->eng_list.Append() = eid;
this->eng_list.push_back(eid);

if (eid == this->sel_engine) sel_id = eid;
}
Expand All @@ -1206,7 +1206,7 @@ struct BuildVehicleWindow : Window {
if (!this->show_hidden_engines && e->IsHidden(_local_company)) continue;
EngineID eid = e->index;
if (!IsEngineBuildable(eid, VEH_SHIP, _local_company)) continue;
*this->eng_list.Append() = eid;
this->eng_list.push_back(eid);

if (eid == this->sel_engine) sel_id = eid;
}
Expand Down Expand Up @@ -1234,7 +1234,7 @@ struct BuildVehicleWindow : Window {
/* First VEH_END window_numbers are fake to allow a window open for all different types at once */
if (!this->listview_mode && !CanVehicleUseStation(eid, st)) continue;

*this->eng_list.Append() = eid;
this->eng_list.push_back(eid);
if (eid == this->sel_engine) sel_id = eid;
}

Expand Down
10 changes: 5 additions & 5 deletions src/company_gui.cpp
Expand Up @@ -608,10 +608,10 @@ struct SelectCompanyLiveryWindow : public Window {
if (default_livery != NULL) {
/* Add COLOUR_END to put the colour out of range, but also allow us to show what the default is */
default_col = (primary ? default_livery->colour1 : default_livery->colour2) + COLOUR_END;
*list->Append() = new DropDownListColourItem(default_col, false);
list->push_back(new DropDownListColourItem(default_col, false));
}
for (uint i = 0; i < lengthof(_colour_dropdown); i++) {
*list->Append() = new DropDownListColourItem(i, HasBit(used_colours, i));
list->push_back(new DropDownListColourItem(i, HasBit(used_colours, i)));
}

byte sel = (default_livery == NULL || HasBit(livery->in_use, primary ? 0 : 1)) ? (primary ? livery->colour1 : livery->colour2) : default_col;
Expand Down Expand Up @@ -644,8 +644,8 @@ struct SelectCompanyLiveryWindow : public Window {
{
for (const Group **g = source->Begin(); g != source->End(); g++) {
if ((*g)->parent != parent) continue;
*this->groups.Append() = *g;
*this->indents.Append() = indent;
this->groups.push_back(*g);
this->indents.push_back(indent);
AddChildren(source, (*g)->index, indent + 1);
}
}
Expand All @@ -664,7 +664,7 @@ struct SelectCompanyLiveryWindow : public Window {
const Group *g;
FOR_ALL_GROUPS(g) {
if (g->owner == owner && g->vehicle_type == vtype) {
*list.Append() = g;
list.push_back(g);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/pool_type.hpp
Expand Up @@ -50,7 +50,7 @@ struct PoolBase {
*/
PoolBase(PoolType pt) : type(pt)
{
*PoolBase::GetPools()->Append() = this;
PoolBase::GetPools()->push_back(this);
}

virtual ~PoolBase();
Expand Down
11 changes: 5 additions & 6 deletions src/core/smallmap_type.hpp
Expand Up @@ -134,9 +134,7 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
inline bool Insert(const T &key, const U &data)
{
if (this->Contains(key)) return false;
Pair *n = this->Append();
n->first = key;
n->second = data;
std::vector<Pair>::emplace_back(key, data);
return true;
}

Expand All @@ -151,9 +149,10 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
for (uint i = 0; i < std::vector<Pair>::size(); i++) {
if (key == std::vector<Pair>::operator[](i).first) return std::vector<Pair>::operator[](i).second;
}
Pair *n = this->Append();
n->first = key;
return n->second;
/*C++17: Pair &n = */ std::vector<Pair>::emplace_back();
Pair &n = std::vector<Pair>::back();
n.first = key;
return n.second;
}

inline void SortByKey()
Expand Down
13 changes: 1 addition & 12 deletions src/core/smallvec_type.hpp
Expand Up @@ -66,17 +66,6 @@ class SmallVector : public std::vector<T> {

~SmallVector() = default;

/**
* Append an item and return it.
* @param to_add the number of items to append
* @return pointer to newly allocated item
*/
inline T *Append(uint to_add = 1)
{
std::vector<T>::resize(std::vector<T>::size() + to_add);
return this->End() - to_add;
}

/**
* Insert a new item at a specific position into the vector, moving all following items.
* @param item Position at which the new item should be inserted
Expand Down Expand Up @@ -112,7 +101,7 @@ class SmallVector : public std::vector<T> {
inline bool Include(const T &item)
{
bool is_member = std::find(std::vector<T>::begin(), std::vector<T>::end(), item) != std::vector<T>::end();
if (!is_member) *this->Append() = item;
if (!is_member) std::vector<T>::emplace_back(item);
return is_member;
}

Expand Down
6 changes: 3 additions & 3 deletions src/date_gui.cpp
Expand Up @@ -75,14 +75,14 @@ struct SetDateWindow : Window {

case WID_SD_DAY:
for (uint i = 0; i < 31; i++) {
*list->Append() = new DropDownListStringItem(STR_DAY_NUMBER_1ST + i, i + 1, false);
list->push_back(new DropDownListStringItem(STR_DAY_NUMBER_1ST + i, i + 1, false));
}
selected = this->date.day;
break;

case WID_SD_MONTH:
for (uint i = 0; i < 12; i++) {
*list->Append() = new DropDownListStringItem(STR_MONTH_JAN + i, i, false);
list->push_back(new DropDownListStringItem(STR_MONTH_JAN + i, i, false));
}
selected = this->date.month;
break;
Expand All @@ -91,7 +91,7 @@ struct SetDateWindow : Window {
for (Year i = this->min_year; i <= this->max_year; i++) {
DropDownListParamStringItem *item = new DropDownListParamStringItem(STR_JUST_INT, i, false);
item->SetParam(0, i);
*list->Append() = item;
list->push_back(item);
}
selected = this->date.year;
break;
Expand Down
11 changes: 6 additions & 5 deletions src/engine.cpp
Expand Up @@ -490,11 +490,12 @@ void EngineOverrideManager::ResetToDefaultMapping()
this->clear();
for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) {
for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++) {
EngineIDMapping *eid = this->Append();
eid->type = type;
eid->grfid = INVALID_GRFID;
eid->internal_id = internal_id;
eid->substitute_id = internal_id;
/*C++17: EngineIDMapping &eid = */ this->emplace_back();
EngineIDMapping &eid = this->back();
eid.type = type;
eid.grfid = INVALID_GRFID;
eid.internal_id = internal_id;
eid.substitute_id = internal_id;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/fios.h
Expand Up @@ -120,7 +120,8 @@ class FileList {
*/
inline FiosItem *Append()
{
return this->files.Append();
this->files.emplace_back();
return this->files.data() + this->files.size();
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/game/game_text.cpp
Expand Up @@ -115,7 +115,7 @@ LanguageStrings *ReadRawLanguageStrings(const char *file)
while (i > 0 && (buffer[i - 1] == '\r' || buffer[i - 1] == '\n' || buffer[i - 1] == ' ')) i--;
buffer[i] = '\0';

*ret->lines.Append() = stredup(buffer, buffer + to_read - 1);
ret->lines.push_back(stredup(buffer, buffer + to_read - 1));

if (len > to_read) {
to_read = 0;
Expand Down Expand Up @@ -194,7 +194,7 @@ struct TranslationWriter : LanguageWriter {
char *dest = MallocT<char>(length + 1);
memcpy(dest, buffer, length);
dest[length] = '\0';
*this->strings->Append() = dest;
this->strings->push_back(dest);
}
};

Expand All @@ -212,7 +212,7 @@ struct StringNameWriter : HeaderWriter {

void WriteStringID(const char *name, int stringid)
{
if (stringid == (int)this->strings->size()) *this->strings->Append() = stredup(name);
if (stringid == (int)this->strings->size()) this->strings->push_back(stredup(name));
}

void Finalise(const StringData &data)
Expand Down Expand Up @@ -246,7 +246,7 @@ class LanguageScanner : protected FileScanner {
{
if (strcmp(filename, exclude) == 0) return true;

*gs->raw_strings.Append() = ReadRawLanguageStrings(filename);
gs->raw_strings.push_back(ReadRawLanguageStrings(filename));
return true;
}
};
Expand All @@ -269,7 +269,7 @@ GameStrings *LoadTranslations()

GameStrings *gs = new GameStrings();
try {
*gs->raw_strings.Append() = ReadRawLanguageStrings(filename);
gs->raw_strings.push_back(ReadRawLanguageStrings(filename));

/* Scan for other language files */
LanguageScanner scanner(gs, filename);
Expand Down Expand Up @@ -324,8 +324,8 @@ void GameStrings::Compile()
translation_reader.ParseFile();
if (_errors != 0) throw std::exception();

LanguageStrings *compiled = *this->compiled_strings.Append() = new LanguageStrings((*p)->language);
TranslationWriter writer(&compiled->lines);
this->compiled_strings.push_back(new LanguageStrings((*p)->language));
TranslationWriter writer(&this->compiled_strings.back()->lines);
writer.WriteLang(data);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/genworld_gui.cpp
Expand Up @@ -289,7 +289,7 @@ static DropDownList *BuildMapsizeDropDown()
for (uint i = MIN_MAP_SIZE_BITS; i <= MAX_MAP_SIZE_BITS; i++) {
DropDownListParamStringItem *item = new DropDownListParamStringItem(STR_JUST_INT, i, false);
item->SetParam(0, 1LL << i);
*list->Append() = item;
list->push_back(item);
}

return list;
Expand Down
12 changes: 6 additions & 6 deletions src/gfx_layout.cpp
Expand Up @@ -150,7 +150,7 @@ class ICUParagraphLayout : public AutoDeleteSmallVector<ParagraphLayouter::Line
ICULine(icu::ParagraphLayout::Line *l) : l(l)
{
for (int i = 0; i < l->countRuns(); i++) {
*this->Append() = new ICUVisualRun(l->getVisualRun(i));
this->push_back(new ICUVisualRun(l->getVisualRun(i)));
}
}
~ICULine() { delete l; }
Expand Down Expand Up @@ -498,7 +498,7 @@ const ParagraphLayouter::Line *FallbackParagraphLayout::NextLine(int max_width)
if (*this->buffer == '\0') {
/* Only a newline. */
this->buffer = NULL;
*l->Append() = new FallbackVisualRun(this->runs.Begin()->second, this->buffer, 0, 0);
l->push_back(new FallbackVisualRun(this->runs.Begin()->second, this->buffer, 0, 0));
return l;
}

Expand Down Expand Up @@ -527,7 +527,7 @@ const ParagraphLayouter::Line *FallbackParagraphLayout::NextLine(int max_width)

if (this->buffer == next_run) {
int w = l->GetWidth();
*l->Append() = new FallbackVisualRun(iter->second, begin, this->buffer - begin, w);
l->push_back(new FallbackVisualRun(iter->second, begin, this->buffer - begin, w));
iter++;
assert(iter != this->runs.End());

Expand Down Expand Up @@ -574,7 +574,7 @@ const ParagraphLayouter::Line *FallbackParagraphLayout::NextLine(int max_width)

if (l->size() == 0 || last_char - begin != 0) {
int w = l->GetWidth();
*l->Append() = new FallbackVisualRun(iter->second, begin, last_char - begin, w);
l->push_back(new FallbackVisualRun(iter->second, begin, last_char - begin, w));
}
return l;
}
Expand Down Expand Up @@ -720,7 +720,7 @@ Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsi
/* Copy all lines into a local cache so we can reuse them later on more easily. */
const ParagraphLayouter::Line *l;
while ((l = line.layout->NextLine(maxw)) != NULL) {
*this->Append() = l;
this->push_back(l);
}

} while (c != '\0');
Expand Down Expand Up @@ -834,7 +834,7 @@ Font *Layouter::GetFont(FontSize size, TextColour colour)
if (it != fonts[size].End()) return it->second;

Font *f = new Font(size, colour);
*fonts[size].Append() = FontColourMap::Pair(colour, f);
fonts[size].emplace_back(colour, f);
return f;
}

Expand Down
2 changes: 1 addition & 1 deletion src/graph_gui.cpp
Expand Up @@ -1143,7 +1143,7 @@ class CompanyLeagueWindow : public Window {

const Company *c;
FOR_ALL_COMPANIES(c) {
*this->companies.Append() = c;
this->companies.push_back(c);
}

this->companies.shrink_to_fit();
Expand Down
6 changes: 3 additions & 3 deletions src/group_gui.cpp
Expand Up @@ -129,8 +129,8 @@ class VehicleGroupWindow : public BaseVehicleListWindow {
{
for (const Group **g = source->Begin(); g != source->End(); g++) {
if ((*g)->parent != parent) continue;
*this->groups.Append() = *g;
*this->indents.Append() = indent;
this->groups.push_back(*g);
this->indents.push_back(indent);
AddChildren(source, (*g)->index, indent + 1);
}
}
Expand Down Expand Up @@ -175,7 +175,7 @@ class VehicleGroupWindow : public BaseVehicleListWindow {
const Group *g;
FOR_ALL_GROUPS(g) {
if (g->owner == owner && g->vehicle_type == this->vli.vtype) {
*list.Append() = g;
list.push_back(g);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/hotkeys.cpp
Expand Up @@ -255,7 +255,7 @@ HotkeyList::HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandler
global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
{
if (_hotkey_lists == NULL) _hotkey_lists = new SmallVector<HotkeyList*, 16>();
*_hotkey_lists->Append() = this;
_hotkey_lists->push_back(this);
}

HotkeyList::~HotkeyList()
Expand Down

0 comments on commit aec6277

Please sign in to comment.