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
std::vector<PictureEntry> 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());

View file

@ -206,8 +206,8 @@ static QPixmap &getPhotoIcon(int idx)
if (!icons) {
const IconMetrics &im = defaultIconMetrics();
icons = std::make_unique<QPixmap[]>(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<dive *> &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<dive *> visibleDives(const QVector<dive *> &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<dive *>
// 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<dive *>
// 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<dive *> &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;