Undo: sort list of dives to add and delete

In 5729f93e1f, the dive addition /
deletion code was simplified in that indexes were calculated on
the fly. This made it, in principle, possible to pass in dives
in any order.

But there was a small oversight: the recipients of the dives-added
and dives-deleted signals expect the dives to be sorted as in
the core list. Only then will the lists be consistent.

Therefore, sort the lists before adding / deleting dives.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-05-15 11:40:39 +02:00 committed by Dirk Hohndel
parent a969d1dd45
commit 9bb5833848

View file

@ -100,6 +100,11 @@ DivesAndTripsToAdd DiveListBase::removeDives(DivesAndSitesToRemove &divesAndSite
divesToAdd.reserve(divesAndSitesToDelete.dives.size());
sitesToAdd.reserve(divesAndSitesToDelete.sites.size());
// Make sure that the dive list is sorted. The added dives will be sent in a signal
// and the recipients assume that the dives are sorted the same way as they are
// in the core list.
std::sort(divesAndSitesToDelete.dives.begin(), divesAndSitesToDelete.dives.end(), dive_less_than);
for (dive *d: divesAndSitesToDelete.dives)
divesToAdd.push_back(removeDive(d, tripsToAdd));
divesAndSitesToDelete.dives.clear();
@ -140,6 +145,13 @@ DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd)
res.resize(toAdd.dives.size());
sites.reserve(toAdd.sites.size());
// Make sure that the dive list is sorted. The added dives will be sent in a signal
// and the recipients assume that the dives are sorted the same way as they are
// in the core list.
std::sort(toAdd.dives.begin(), toAdd.dives.end(),
[](const DiveToAdd &d, const DiveToAdd &d2)
{ return dive_less_than(d.dive.get(), d2.dive.get()); });
// Now, add the dives
// Note: the idiomatic STL-way would be std::transform, but let's use a loop since
// that is closer to classical C-style.