diff --git a/commands/command_divelist.cpp b/commands/command_divelist.cpp index 6f2887056..fe8f6050f 100644 --- a/commands/command_divelist.cpp +++ b/commands/command_divelist.cpp @@ -419,7 +419,7 @@ AddDive::AddDive(dive *d, bool autogroup, bool newNumber) int idx = divelog.dives.get_insertion_index(divePtr.get()); if (newNumber) - divePtr->number = get_dive_nr_at_idx(idx); + divePtr->number = divelog.dives.get_dive_nr_at_idx(idx); divesToAdd.dives.push_back({ std::move(divePtr), trip, site }); if (allocTrip) diff --git a/core/divelist.cpp b/core/divelist.cpp index 4dfa8d022..13d3419fd 100644 --- a/core/divelist.cpp +++ b/core/divelist.cpp @@ -1142,13 +1142,6 @@ process_imported_dives_result process_imported_dives(struct divelog &import_log, return res; } -static struct dive *get_last_valid_dive() -{ - auto it = std::find_if(divelog.dives.rbegin(), divelog.dives.rend(), - [](auto &d) { return !d->invalid; }); - return it != divelog.dives.rend() ? it->get() : nullptr; -} - /* return the number a dive gets when inserted at the given index. * this function is supposed to be called *before* a dive was added. * this returns: @@ -1156,14 +1149,14 @@ static struct dive *get_last_valid_dive() * - last_nr+1 for addition at end of log (if last dive had a number) * - 0 for all other cases */ -int get_dive_nr_at_idx(int idx) +int dive_table::get_dive_nr_at_idx(int idx) const { - if (static_cast(idx) < divelog.dives.size()) + if (static_cast(idx) < size()) return 0; - struct dive *last_dive = get_last_valid_dive(); - if (!last_dive) + auto it = std::find_if(rbegin(), rend(), [](auto &d) { return !d->invalid; }); + if (it == rend()) return 1; - return last_dive->number ? last_dive->number + 1 : 0; + return (*it)->number ? (*it)->number + 1 : 0; } /* lookup of trip in main trip_table based on its id */ diff --git a/core/divelist.h b/core/divelist.h index 2942c9da1..e4b5dd303 100644 --- a/core/divelist.h +++ b/core/divelist.h @@ -21,6 +21,7 @@ struct dive_table : public sorted_owning_table { void record_dive(std::unique_ptr d); // call fixup_dive() before adding dive to table. std::unique_ptr unregister_dive(int idx); + int get_dive_nr_at_idx(int idx) const; timestamp_t get_surface_interval(timestamp_t when) const; struct dive *find_next_visible_dive(timestamp_t when); }; @@ -51,7 +52,6 @@ struct process_imported_dives_result { extern process_imported_dives_result process_imported_dives(struct divelog &import_log, int flags); extern void get_dive_gas(const struct dive *dive, int *o2_p, int *he_p, int *o2low_p); -extern int get_dive_nr_at_idx(int idx); int get_min_datafile_version(); void report_datafile_version(int version);