summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/core/smallvec_type.hpp32
-rw-r--r--src/network/core/host.cpp2
-rw-r--r--src/network/network_content_gui.cpp17
-rw-r--r--src/newgrf.cpp6
-rw-r--r--src/newgrf_engine.cpp4
-rw-r--r--src/newgrf_gui.cpp6
-rw-r--r--src/newgrf_railtype.cpp5
-rw-r--r--src/os/windows/string_uniscribe.cpp2
-rw-r--r--src/timetable_cmd.cpp4
9 files changed, 41 insertions, 37 deletions
diff --git a/src/core/smallvec_type.hpp b/src/core/smallvec_type.hpp
index e0596e338..0ab82c4fc 100644
--- a/src/core/smallvec_type.hpp
+++ b/src/core/smallvec_type.hpp
@@ -67,18 +67,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.
* @param item Item to test for
@@ -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
diff --git a/src/network/core/host.cpp b/src/network/core/host.cpp
index 1ea5389d4..b1ed71e92 100644
--- a/src/network/core/host.cpp
+++ b/src/network/core/host.cpp
@@ -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);
diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp
index 19c075258..b7237cadd 100644
--- a/src/network/network_content_gui.cpp
+++ b/src/network/network_content_gui.cpp
@@ -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 */
diff --git a/src/newgrf.cpp b/src/newgrf.cpp
index 63296593d..36a13aaaa 100644
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -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);
}
diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp
index b2dd4d1a3..7a0bb04f3 100644
--- a/src/newgrf_engine.cpp
+++ b/src/newgrf_engine.cpp
@@ -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);
diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp
index 0db38e297..1473f1877 100644
--- a/src/newgrf_gui.cpp
+++ b/src/newgrf_gui.cpp
@@ -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
diff --git a/src/newgrf_railtype.cpp b/src/newgrf_railtype.cpp
index 60ddc1914..c57be5b6c 100644
--- a/src/newgrf_railtype.cpp
+++ b/src/newgrf_railtype.cpp
@@ -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;
diff --git a/src/os/windows/string_uniscribe.cpp b/src/os/windows/string_uniscribe.cpp
index f1c62c7d3..ec0e02639 100644
--- a/src/os/windows/string_uniscribe.cpp
+++ b/src/os/windows/string_uniscribe.cpp
@@ -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;
}
diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp
index 8107fc3d2..fcc8493e2 100644
--- a/src/timetable_cmd.cpp
+++ b/src/timetable_cmd.cpp
@@ -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;
}
}