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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-10-30 21:24:13 +01:00
parent 727d519046
commit b63073e203
2 changed files with 22 additions and 20 deletions

View file

@ -206,8 +206,8 @@ void DivePictureModel::picturesAdded(dive *d, QVector<PictureObj> picsIn)
// Convert the picture-data into our own format // Convert the picture-data into our own format
std::vector<PictureEntry> pics; std::vector<PictureEntry> pics;
pics.reserve(picsIn.size()); pics.reserve(picsIn.size());
for (int i = 0; i < picsIn.size(); ++i) for (const PictureObj &pic: picsIn)
pics.push_back(PictureEntry(d, picsIn[i])); pics.push_back(PictureEntry(d, pic));
// Insert batch-wise to avoid too many reloads // Insert batch-wise to avoid too many reloads
pictures.reserve(pictures.size() + pics.size()); pictures.reserve(pictures.size() + pics.size());

View file

@ -206,8 +206,8 @@ static QPixmap &getPhotoIcon(int idx)
if (!icons) { if (!icons) {
const IconMetrics &im = defaultIconMetrics(); const IconMetrics &im = defaultIconMetrics();
icons = std::make_unique<QPixmap[]>(std::size(icon_names)); icons = std::make_unique<QPixmap[]>(std::size(icon_names));
for (size_t i = 0; i < std::size(icon_names); ++i) for (auto [i, name]: enumerated_range(icon_names))
icons[i] = QIcon(icon_names[i]).pixmap(im.sz_small, im.sz_small); icons[i] = QIcon(name).pixmap(im.sz_small, im.sz_small);
} }
return icons[idx]; return icons[idx];
} }
@ -1008,25 +1008,27 @@ 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 (int i = 0; i < (int)items.size(); ++i) for (auto [i, item]: enumerated_range(items)) {
if (items[i].d_or_t.trip == trip) if (item.d_or_t.trip == trip)
return i; return i;
}
return -1; return -1;
} }
int DiveTripModelTree::findDiveIdx(const dive *d) const int DiveTripModelTree::findDiveIdx(const dive *d) const
{ {
for (int i = 0; i < (int)items.size(); ++i) for (auto [i, item]: enumerated_range(items)) {
if (items[i].isDive(d)) if (item.isDive(d))
return i; return i;
}
return -1; 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]; const Item &item = items[tripIdx];
for (int i = 0; i < (int)item.dives.size(); ++i) for (auto [i, dive]: enumerated_range(item.dives))
if (item.dives[i] == d) if (dive == d)
return i; return i;
return -1; return -1;
} }
@ -1034,8 +1036,8 @@ int DiveTripModelTree::findDiveInTrip(int tripIdx, const dive *d) const
int DiveTripModelTree::findInsertionIndex(const dive_trip *trip) const int DiveTripModelTree::findInsertionIndex(const dive_trip *trip) const
{ {
dive_or_trip d_or_t{ nullptr, (dive_trip *)trip }; dive_or_trip d_or_t{ nullptr, (dive_trip *)trip };
for (int i = 0; i < (int)items.size(); ++i) { for (auto [i, item]: enumerated_range(items)) {
if (dive_or_trip_less_than(d_or_t, items[i].d_or_t)) if (dive_or_trip_less_than(d_or_t, item.d_or_t))
return i; return i;
} }
return items.size(); return items.size();
@ -1122,8 +1124,8 @@ static QVector<dive *> visibleDives(const QVector<dive *> &dives)
#ifdef SUBSURFACE_MOBILE #ifdef SUBSURFACE_MOBILE
int DiveTripModelTree::tripInDirection(const struct dive *d, int direction) const int DiveTripModelTree::tripInDirection(const struct dive *d, int direction) const
{ {
for (int i = 0; i < (int)items.size(); ++i) { for (auto [i, item]: enumerated_range(items)) {
if (items[i].d_or_t.dive == d || (items[i].d_or_t.trip && findDiveInTrip(i, d) != -1)) { 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 // now walk in the direction given to find a trip
int offset = direction; int offset = direction;
while (i + offset >= 0 && i + offset < (int)items.size()) { while (i + offset >= 0 && i + offset < (int)items.size()) {
@ -1406,8 +1408,8 @@ void DiveTripModelTree::divesSelectedTrip(dive_trip *trip, const QVector<dive *>
// Since both lists are sorted, we can do this linearly. Perhaps a binary search // Since both lists are sorted, we can do this linearly. Perhaps a binary search
// would be better? // would be better?
int j = 0; // Index in items array int j = 0; // Index in items array
for (int i = 0; i < dives.size(); ++i) { for (struct dive *dive: dives) {
while (j < (int)items.size() && !items[j].isDive(dives[i])) while (j < (int)items.size() && !items[j].isDive(dive))
++j; ++j;
if (j >= (int)items.size()) if (j >= (int)items.size())
break; break;
@ -1427,8 +1429,8 @@ void DiveTripModelTree::divesSelectedTrip(dive_trip *trip, const QVector<dive *>
// would be better? // would be better?
int j = 0; // Index in items array int j = 0; // Index in items array
const Item &entry = items[idx]; const Item &entry = items[idx];
for (int i = 0; i < dives.size(); ++i) { for (struct dive *dive: dives) {
while (j < (int)entry.dives.size() && entry.dives[j] != dives[i]) while (j < (int)entry.dives.size() && entry.dives[j] != dive)
++j; ++j;
if (j >= (int)entry.dives.size()) if (j >= (int)entry.dives.size())
break; break;
@ -1663,8 +1665,8 @@ void DiveTripModelList::divesSelected(const QVector<dive *> &divesIn)
// Since both lists are sorted, we can do this linearly. Perhaps a binary search // Since both lists are sorted, we can do this linearly. Perhaps a binary search
// would be better? // would be better?
int j = 0; // Index in items array int j = 0; // Index in items array
for (int i = 0; i < dives.size(); ++i) { for (struct dive *dive: dives) {
while (j < (int)items.size() && items[j] != dives[i]) while (j < (int)items.size() && items[j] != dive)
++j; ++j;
if (j >= (int)items.size()) if (j >= (int)items.size())
break; break;