cleanup: implement index_of() and index_of_if() generics

Search the index of an item in a container. Compare by
equality or a lambda. The lack of these have annoyed me for a
long time. Return the index of the first found element or
-1 if no element found.

Currently, only supports random-access operators. Might be
trivially changed for forward iterators.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-10-31 19:35:15 +01:00
parent df5bf728f9
commit 691d9e86de
3 changed files with 23 additions and 19 deletions

View file

@ -82,4 +82,20 @@ public:
} }
}; };
// Find the index of an element in a range. Return -1 if not found
// Range must have a random access iterator.
template <typename Range, typename Element>
int index_of(const Range &range, const Element &e)
{
auto it = std::find(std::begin(range), std::end(range), e);
return it == std::end(range) ? -1 : it - std::begin(range);
}
template <typename Range, typename Func>
int index_of_if(const Range &range, Func f)
{
auto it = std::find_if(std::begin(range), std::end(range), f);
return it == std::end(range) ? -1 : it - std::begin(range);
}
#endif #endif

View file

@ -242,10 +242,8 @@ int DivePictureModel::rowCount(const QModelIndex&) const
int DivePictureModel::findPictureId(const std::string &filename) int DivePictureModel::findPictureId(const std::string &filename)
{ {
for (int i = 0; i < (int)pictures.size(); ++i) return index_of_if(pictures, [&filename](const PictureEntry &p)
if (pictures[i].filename == filename) { return p.filename == filename; });
return i;
return -1;
} }
static void addDurationToThumbnail(QImage &img, duration_t duration) static void addDurationToThumbnail(QImage &img, duration_t duration)

View file

@ -1008,29 +1008,19 @@ void DiveTripModelTree::addDivesToTrip(int trip, const QVector<dive *> &dives)
int DiveTripModelTree::findTripIdx(const dive_trip *trip) const int DiveTripModelTree::findTripIdx(const dive_trip *trip) const
{ {
for (auto [i, item]: enumerated_range(items)) { return index_of_if(items, [trip] (const Item &item)
if (item.d_or_t.trip == trip) { return item.d_or_t.trip == trip; });
return i;
}
return -1;
} }
int DiveTripModelTree::findDiveIdx(const dive *d) const int DiveTripModelTree::findDiveIdx(const dive *d) const
{ {
for (auto [i, item]: enumerated_range(items)) { return index_of_if(items, [d] (const Item &item)
if (item.isDive(d)) { return item.isDive(d); });
return i;
}
return -1;
} }
int DiveTripModelTree::findDiveInTrip(int tripIdx, const dive *d) const int DiveTripModelTree::findDiveInTrip(int tripIdx, const dive *d) const
{ {
const Item &item = items[tripIdx]; return index_of(items[tripIdx].dives, d);
for (auto [i, dive]: enumerated_range(item.dives))
if (dive == d)
return i;
return -1;
} }
int DiveTripModelTree::findInsertionIndex(const dive_trip *trip) const int DiveTripModelTree::findInsertionIndex(const dive_trip *trip) const