Codechange: Use map.emplace() instead of map.insert(std::pair).

This avoids a copy of the pair into the map.
This commit is contained in:
Peter Nelson 2024-04-20 02:46:39 +01:00 committed by Peter Nelson
parent 57d7359b1a
commit ed2db80990
7 changed files with 9 additions and 13 deletions

View File

@ -71,8 +71,7 @@ static int32_t GetAmount(CargoMonitorMap &monitor_map, CargoMonitorID monitor, b
CargoMonitorMap::iterator iter = monitor_map.find(monitor);
if (iter == monitor_map.end()) {
if (keep_monitoring) {
std::pair<CargoMonitorID, uint32_t> p(monitor, 0);
monitor_map.insert(p);
monitor_map.emplace(monitor, 0);
}
return 0;
} else {

View File

@ -99,7 +99,7 @@ void BuildCargoLabelMap()
/* Label already exists, don't addd again. */
if (CargoSpec::label_map.count(cs.label) != 0) continue;
CargoSpec::label_map.insert(std::make_pair(cs.label, cs.Index()));
CargoSpec::label_map.emplace(cs.label, cs.Index());
}
}

View File

@ -72,7 +72,7 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet &p)
for (uint i = 0; i < dependency_count; i++) {
ContentID dependency_cid = (ContentID)p.Recv_uint32();
ci->dependencies.push_back(dependency_cid);
this->reverse_dependency_map.insert({ dependency_cid, ci->id });
this->reverse_dependency_map.emplace(dependency_cid, ci->id);
}
uint tag_count = p.Recv_uint8();

View File

@ -591,8 +591,7 @@ typedef std::map<uint32_t, const GRFConfig *> GrfIdMap; ///< Map of grfid to the
static void FillGrfidMap(const GRFConfig *c, GrfIdMap *grfid_map)
{
while (c != nullptr) {
std::pair<uint32_t, const GRFConfig *> p(c->ident.grfid, c);
grfid_map->insert(p);
grfid_map->emplace(c->ident.grfid, c);
c = c->next;
}
}

View File

@ -81,8 +81,7 @@ struct CMDLChunkHandler : ChunkHandler {
if (fix) storage.number = FixupCargoMonitor(storage.number);
std::pair<CargoMonitorID, uint32_t> p(storage.number, storage.amount);
_cargo_deliveries.insert(p);
_cargo_deliveries.emplace(storage.number, storage.amount);
}
}
};
@ -125,8 +124,7 @@ struct CMPUChunkHandler : ChunkHandler {
if (fix) storage.number = FixupCargoMonitor(storage.number);
std::pair<CargoMonitorID, uint32_t> p(storage.number, storage.amount);
_cargo_pickups.insert(p);
_cargo_pickups.emplace(storage.number, storage.amount);
}
}
};

View File

@ -318,7 +318,7 @@ public:
for (uint32_t j = 0; j < num_flows; ++j) {
SlObject(&flow, this->GetLoadDescription());
if (fs == nullptr || prev_source != flow.source) {
fs = &(ge->flows.insert(std::make_pair(flow.source, FlowStat(flow.via, flow.share, flow.restricted))).first->second);
fs = &(ge->flows.emplace(flow.source, FlowStat(flow.via, flow.share, flow.restricted))).first->second;
} else {
fs->AppendShare(flow.via, flow.share, flow.restricted);
}

View File

@ -4810,7 +4810,7 @@ void FlowStatMap::AddFlow(StationID origin, StationID via, uint flow)
{
FlowStatMap::iterator origin_it = this->find(origin);
if (origin_it == this->end()) {
this->insert(std::make_pair(origin, FlowStat(via, flow)));
this->emplace(origin, FlowStat(via, flow));
} else {
origin_it->second.ChangeShare(via, flow);
assert(!origin_it->second.GetShares()->empty());
@ -4831,7 +4831,7 @@ void FlowStatMap::PassOnFlow(StationID origin, StationID via, uint flow)
if (prev_it == this->end()) {
FlowStat fs(via, flow);
fs.AppendShare(INVALID_STATION, flow);
this->insert(std::make_pair(origin, fs));
this->emplace(origin, fs);
} else {
prev_it->second.ChangeShare(via, flow);
prev_it->second.ChangeShare(INVALID_STATION, flow);