Codechange: Replaced SmallVector::Find() with std::find()

pull/82/head
Henry Wilson 5 years ago committed by PeterN
parent e0c58bf5ee
commit 2bc2de9034

@ -66,18 +66,6 @@ public:
~SmallVector() = default;
/**
* Search for the first occurrence of an item.
* The '!=' operator of T is used for comparison.
* @param item Item to search for
* @return The position of the item, or -1 when not present
*/
inline int FindIndex(const T &item) const
{
auto const it = std::find(std::vector<T>::begin(), std::vector<T>::end(), item);
return it == std::vector<T>::end() ? -1 : it - std::vector<T>::begin();
}
/**
* Tests whether a item is present in the vector, and appends it to the end if not.
* The '!=' operator of T is used for comparison.
@ -133,7 +121,25 @@ public:
};
/**
* Helper function to extend a vector by more than one element
* Helper function to get the index of an item
* Consider using std::set, std::unordered_set or std::flat_set in new code
*
* @param vec A reference to the vector to be extended
* @param item Reference to the item to be search for
*
* @return Index of element if found, otherwise -1
*/
template <typename T>
int find_index(std::vector<T> const& vec, T const& item)
{
auto const it = std::find(vec.begin(), vec.end(), item);
if (it != vec.end()) return it - vec.begin();
return -1;
}
/**
* Helper function to append N default-constructed elements and get a pointer to the first new element
* Consider using std::back_inserter in new code
*
* @param vec A reference to the vector to be extended

@ -136,7 +136,7 @@ static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // Wi
memcpy(&address, &ifo[j].iiAddress.Address, sizeof(sockaddr));
((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
NetworkAddress addr(address, sizeof(sockaddr));
if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) *broadcast->Append() = addr;
if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const& elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
}
free(ifo);

@ -435,12 +435,8 @@ class NetworkContentListWindow : public Window, ContentCallback {
{
if (!this->content.Sort()) return;
for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
if (*iter == this->selected) {
this->list_pos = iter - this->content.Begin();
break;
}
}
int idx = find_index(this->content, this->selected);
if (idx >= 0) this->list_pos = idx;
}
/** Filter content by tags/name */
@ -478,11 +474,10 @@ class NetworkContentListWindow : public Window, ContentCallback {
if (!changed) return;
/* update list position */
for (ConstContentIterator iter = this->content.Begin(); iter != this->content.End(); iter++) {
if (*iter == this->selected) {
this->list_pos = iter - this->content.Begin();
return;
}
int idx = find_index(this->content, this->selected);
if (idx >= 0) {
this->list_pos = idx;
return;
}
/* previously selected item not in list anymore */

@ -8383,8 +8383,8 @@ static void BuildCargoTranslationMap()
_cur.grffile->cargo_map[c] = cs->bitnum;
} else {
/* Check the translation table for this cargo's label */
int index = _cur.grffile->cargo_list.FindIndex(cs->label);
if (index >= 0) _cur.grffile->cargo_map[c] = index;
int idx = find_index(_cur.grffile->cargo_list, {cs->label});
if (idx >= 0) _cur.grffile->cargo_map[c] = idx;
}
}
}
@ -9226,7 +9226,7 @@ static void FinalisePriceBaseMultipliers()
GRFFile *dest = GetFileByGRFID(override);
if (dest == NULL) continue;
grf_overrides[i] = _grf_files.FindIndex(dest);
grf_overrides[i] = find_index(_grf_files, dest);
assert(grf_overrides[i] >= 0);
}

@ -1245,8 +1245,8 @@ void CommitVehicleListOrderChanges()
EngineID target = _engine_mngr.GetID(id_source->type, local_target, id_source->grfid);
if (target == INVALID_ENGINE) continue;
int source_index = ordering.FindIndex(source);
int target_index = ordering.FindIndex(target);
int source_index = find_index(ordering, source);
int target_index = find_index(ordering, target);
assert(source_index >= 0 && target_index >= 0);
assert(source_index != target_index);

@ -1486,8 +1486,10 @@ private:
this->avails.Sort();
if (this->avail_sel != NULL) {
this->avail_pos = this->avails.FindIndex(this->avail_sel);
if (this->avail_pos < 0) this->avail_sel = NULL;
this->avail_pos = find_index(this->avails, this->avail_sel);
if (this->avail_pos == -1) {
this->avail_sel = NULL;
}
}
this->vscroll2->SetCount(this->avails.size()); // Update the scrollbar

@ -143,8 +143,9 @@ uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
/* Look for a matching rail type label in the table */
RailTypeLabel label = GetRailTypeInfo(railtype)->label;
int index = grffile->railtype_list.FindIndex(label);
if (index >= 0) return index;
int idx = find_index(grffile->railtype_list, label);
if (idx >= 0) return idx;
/* If not found, return as invalid */
return 0xFF;

@ -424,7 +424,7 @@ static std::vector<SCRIPT_ITEM> UniscribeItemizeString(UniscribeParagraphLayoutF
if (!UniscribeShapeRun(this->text_buffer, run)) return NULL;
}
*line->Append() = new UniscribeVisualRun(run, cur_pos);
line->push_back(new UniscribeVisualRun(run, cur_pos));
cur_pos += run.total_advance;
}

@ -298,10 +298,9 @@ CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1,
QSortT(vehs.Begin(), vehs.size(), &VehicleTimetableSorter);
}
int base = vehs.FindIndex(v);
int idx = vehs.begin() - std::find(vehs.begin(), vehs.end(), v);
for (Vehicle **viter = vehs.Begin(); viter != vehs.End(); viter++) {
int idx = (viter - vehs.Begin()) - base;
Vehicle *w = *viter;
w->lateness_counter = 0;
@ -309,6 +308,7 @@ CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1,
/* Do multiplication, then division to reduce rounding errors. */
w->timetable_start = start_date + idx * total_duration / num_vehs / DAY_TICKS;
SetWindowDirty(WC_VEHICLE_TIMETABLE, w->index);
++idx;
}
}

Loading…
Cancel
Save