From b63073e203bf10621caac8f0aa2a405a8731abdf Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 30 Oct 2022 21:24:13 +0100 Subject: [PATCH] cleanup: use range based loops in model There were a number of classical "for (i = 0; i < size; ++i)" loops. Replace them either by plain range based loops if the index is not used or by enumerated_range() loops. Signed-off-by: Berthold Stoeger --- qt-models/divepicturemodel.cpp | 4 ++-- qt-models/divetripmodel.cpp | 38 ++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/qt-models/divepicturemodel.cpp b/qt-models/divepicturemodel.cpp index 867929911..bab837201 100644 --- a/qt-models/divepicturemodel.cpp +++ b/qt-models/divepicturemodel.cpp @@ -206,8 +206,8 @@ void DivePictureModel::picturesAdded(dive *d, QVector picsIn) // Convert the picture-data into our own format std::vector pics; pics.reserve(picsIn.size()); - for (int i = 0; i < picsIn.size(); ++i) - pics.push_back(PictureEntry(d, picsIn[i])); + for (const PictureObj &pic: picsIn) + pics.push_back(PictureEntry(d, pic)); // Insert batch-wise to avoid too many reloads pictures.reserve(pictures.size() + pics.size()); diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index a4dbbf25b..39a8a0f62 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -206,8 +206,8 @@ static QPixmap &getPhotoIcon(int idx) if (!icons) { const IconMetrics &im = defaultIconMetrics(); icons = std::make_unique(std::size(icon_names)); - for (size_t i = 0; i < std::size(icon_names); ++i) - icons[i] = QIcon(icon_names[i]).pixmap(im.sz_small, im.sz_small); + for (auto [i, name]: enumerated_range(icon_names)) + icons[i] = QIcon(name).pixmap(im.sz_small, im.sz_small); } return icons[idx]; } @@ -1008,25 +1008,27 @@ void DiveTripModelTree::addDivesToTrip(int trip, const QVector &dives) int DiveTripModelTree::findTripIdx(const dive_trip *trip) const { - for (int i = 0; i < (int)items.size(); ++i) - if (items[i].d_or_t.trip == trip) + for (auto [i, item]: enumerated_range(items)) { + if (item.d_or_t.trip == trip) return i; + } return -1; } int DiveTripModelTree::findDiveIdx(const dive *d) const { - for (int i = 0; i < (int)items.size(); ++i) - if (items[i].isDive(d)) + for (auto [i, item]: enumerated_range(items)) { + if (item.isDive(d)) return i; + } return -1; } int DiveTripModelTree::findDiveInTrip(int tripIdx, const dive *d) const { const Item &item = items[tripIdx]; - for (int i = 0; i < (int)item.dives.size(); ++i) - if (item.dives[i] == d) + for (auto [i, dive]: enumerated_range(item.dives)) + if (dive == d) return i; return -1; } @@ -1034,8 +1036,8 @@ int DiveTripModelTree::findDiveInTrip(int tripIdx, const dive *d) const int DiveTripModelTree::findInsertionIndex(const dive_trip *trip) const { dive_or_trip d_or_t{ nullptr, (dive_trip *)trip }; - for (int i = 0; i < (int)items.size(); ++i) { - if (dive_or_trip_less_than(d_or_t, items[i].d_or_t)) + for (auto [i, item]: enumerated_range(items)) { + if (dive_or_trip_less_than(d_or_t, item.d_or_t)) return i; } return items.size(); @@ -1122,8 +1124,8 @@ static QVector visibleDives(const QVector &dives) #ifdef SUBSURFACE_MOBILE int DiveTripModelTree::tripInDirection(const struct dive *d, int direction) const { - for (int i = 0; i < (int)items.size(); ++i) { - if (items[i].d_or_t.dive == d || (items[i].d_or_t.trip && findDiveInTrip(i, d) != -1)) { + for (auto [i, item]: enumerated_range(items)) { + if (item.d_or_t.dive == d || (item.d_or_t.trip && findDiveInTrip(i, d) != -1)) { // now walk in the direction given to find a trip int offset = direction; while (i + offset >= 0 && i + offset < (int)items.size()) { @@ -1406,8 +1408,8 @@ void DiveTripModelTree::divesSelectedTrip(dive_trip *trip, const QVector // Since both lists are sorted, we can do this linearly. Perhaps a binary search // would be better? int j = 0; // Index in items array - for (int i = 0; i < dives.size(); ++i) { - while (j < (int)items.size() && !items[j].isDive(dives[i])) + for (struct dive *dive: dives) { + while (j < (int)items.size() && !items[j].isDive(dive)) ++j; if (j >= (int)items.size()) break; @@ -1427,8 +1429,8 @@ void DiveTripModelTree::divesSelectedTrip(dive_trip *trip, const QVector // would be better? int j = 0; // Index in items array const Item &entry = items[idx]; - for (int i = 0; i < dives.size(); ++i) { - while (j < (int)entry.dives.size() && entry.dives[j] != dives[i]) + for (struct dive *dive: dives) { + while (j < (int)entry.dives.size() && entry.dives[j] != dive) ++j; if (j >= (int)entry.dives.size()) break; @@ -1663,8 +1665,8 @@ void DiveTripModelList::divesSelected(const QVector &divesIn) // Since both lists are sorted, we can do this linearly. Perhaps a binary search // would be better? int j = 0; // Index in items array - for (int i = 0; i < dives.size(); ++i) { - while (j < (int)items.size() && items[j] != dives[i]) + for (struct dive *dive: dives) { + while (j < (int)items.size() && items[j] != dive) ++j; if (j >= (int)items.size()) break;