mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
core: turn trip-table into our own sorted_owning_table
Since the sorted_owning_table depends on the fact that different elements never compare as equal, make the comparison function safer in that respect. If all failes, compare the pointers. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
1cebafb08f
commit
eacad89531
21 changed files with 217 additions and 305 deletions
|
@ -638,6 +638,8 @@ static int comp_dc(const struct dive *d1, const struct dive *d2)
|
|||
int comp_dives(const struct dive *a, const struct dive *b)
|
||||
{
|
||||
int cmp;
|
||||
if (a == b)
|
||||
return 0; /* reflexivity */
|
||||
if (a->when < b->when)
|
||||
return -1;
|
||||
if (a->when > b->when)
|
||||
|
@ -647,9 +649,9 @@ int comp_dives(const struct dive *a, const struct dive *b)
|
|||
return -1;
|
||||
if (!a->divetrip)
|
||||
return 1;
|
||||
if (trip_date(a->divetrip) < trip_date(b->divetrip))
|
||||
if (trip_date(*a->divetrip) < trip_date(*b->divetrip))
|
||||
return -1;
|
||||
if (trip_date(a->divetrip) > trip_date(b->divetrip))
|
||||
if (trip_date(*a->divetrip) > trip_date(*b->divetrip))
|
||||
return 1;
|
||||
}
|
||||
if (a->number < b->number)
|
||||
|
@ -662,7 +664,7 @@ int comp_dives(const struct dive *a, const struct dive *b)
|
|||
return -1;
|
||||
if (a->id > b->id)
|
||||
return 1;
|
||||
return 0; /* this should not happen for a != b */
|
||||
return a < b ? -1 : 1; /* give up. */
|
||||
}
|
||||
|
||||
/* Dive table functions */
|
||||
|
@ -690,24 +692,19 @@ void insert_dive(struct dive_table *table, struct dive *d)
|
|||
* Walk the dives from the oldest dive in the given table, and see if we
|
||||
* can autogroup them. But only do this when the user selected autogrouping.
|
||||
*/
|
||||
static void autogroup_dives(struct dive_table *table, struct trip_table *trip_table_arg)
|
||||
static void autogroup_dives(struct dive_table *table, struct trip_table &trip_table)
|
||||
{
|
||||
int from, to;
|
||||
dive_trip *trip;
|
||||
int i, j;
|
||||
bool alloc;
|
||||
|
||||
if (!divelog.autogroup)
|
||||
return;
|
||||
|
||||
for (i = 0; (trip = get_dives_to_autogroup(table, i, &from, &to, &alloc)) != NULL; i = to) {
|
||||
for (j = from; j < to; ++j)
|
||||
add_dive_to_trip(table->dives[j], trip);
|
||||
for (auto &entry: get_dives_to_autogroup(table)) {
|
||||
for (int i = entry.from; i < entry.to; ++i)
|
||||
add_dive_to_trip(table->dives[i], entry.trip);
|
||||
/* If this was newly allocated, add trip to list */
|
||||
if (alloc)
|
||||
insert_trip(trip, trip_table_arg);
|
||||
if (entry.created_trip)
|
||||
trip_table.put(std::move(entry.created_trip));
|
||||
}
|
||||
sort_trip_table(trip_table_arg);
|
||||
trip_table.sort(); // Necessary?
|
||||
#ifdef DEBUG_TRIP
|
||||
dump_trip_list();
|
||||
#endif
|
||||
|
@ -751,10 +748,10 @@ struct dive *unregister_dive(int idx)
|
|||
void process_loaded_dives()
|
||||
{
|
||||
sort_dive_table(divelog.dives.get());
|
||||
sort_trip_table(divelog.trips.get());
|
||||
divelog.trips->sort();
|
||||
|
||||
/* Autogroup dives if desired by user. */
|
||||
autogroup_dives(divelog.dives.get(), divelog.trips.get());
|
||||
autogroup_dives(divelog.dives.get(), *divelog.trips);
|
||||
|
||||
fulltext_populate();
|
||||
|
||||
|
@ -932,13 +929,13 @@ void add_imported_dives(struct divelog *import_log, int flags)
|
|||
int i, idx;
|
||||
struct dive_table dives_to_add = empty_dive_table;
|
||||
struct dive_table dives_to_remove = empty_dive_table;
|
||||
struct trip_table trips_to_add = empty_trip_table;
|
||||
struct trip_table trips_to_add;
|
||||
dive_site_table dive_sites_to_add;
|
||||
device_table devices_to_add;
|
||||
|
||||
/* Process imported dives and generate lists of dives
|
||||
* to-be-added and to-be-removed */
|
||||
process_imported_dives(import_log, flags, &dives_to_add, &dives_to_remove, &trips_to_add,
|
||||
process_imported_dives(import_log, flags, &dives_to_add, &dives_to_remove, trips_to_add,
|
||||
dive_sites_to_add, devices_to_add);
|
||||
|
||||
/* Start by deselecting all dives, so that we don't end up with an invalid selection */
|
||||
|
@ -969,9 +966,9 @@ void add_imported_dives(struct divelog *import_log, int flags)
|
|||
dives_to_add.nr = 0;
|
||||
|
||||
/* Add new trips */
|
||||
for (i = 0; i < trips_to_add.nr; i++)
|
||||
insert_trip(trips_to_add.trips[i], divelog.trips.get());
|
||||
trips_to_add.nr = 0;
|
||||
for (auto &trip: trips_to_add)
|
||||
divelog.trips->put(std::move(trip));
|
||||
trips_to_add.clear();
|
||||
|
||||
/* Add new dive sites */
|
||||
for (auto &ds: dive_sites_to_add)
|
||||
|
@ -987,7 +984,6 @@ void add_imported_dives(struct divelog *import_log, int flags)
|
|||
|
||||
free(dives_to_add.dives);
|
||||
free(dives_to_remove.dives);
|
||||
free(trips_to_add.trips);
|
||||
|
||||
/* Inform frontend of reset data. This should reset all the models. */
|
||||
emit_reset_signal();
|
||||
|
@ -1003,22 +999,17 @@ void add_imported_dives(struct divelog *import_log, int flags)
|
|||
* Returns true if trip was merged. In this case, the trip will be
|
||||
* freed.
|
||||
*/
|
||||
static bool try_to_merge_trip(dive_trip *trip_import, struct dive_table *import_table, bool prefer_imported,
|
||||
static bool try_to_merge_trip(dive_trip &trip_import, struct dive_table *import_table, bool prefer_imported,
|
||||
/* output parameters: */
|
||||
struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
|
||||
bool *sequence_changed, int *start_renumbering_at)
|
||||
{
|
||||
int i;
|
||||
struct dive_trip *trip_old;
|
||||
|
||||
for (i = 0; i < divelog.trips->nr; i++) {
|
||||
trip_old = divelog.trips->trips[i];
|
||||
if (trips_overlap(trip_import, trip_old)) {
|
||||
*sequence_changed |= merge_dive_tables(&trip_import->dives, import_table, &trip_old->dives,
|
||||
prefer_imported, trip_old,
|
||||
for (auto &trip_old: *divelog.trips) {
|
||||
if (trips_overlap(trip_import, *trip_old)) {
|
||||
*sequence_changed |= merge_dive_tables(&trip_import.dives, import_table, &trip_old->dives,
|
||||
prefer_imported, trip_old.get(),
|
||||
dives_to_add, dives_to_remove,
|
||||
start_renumbering_at);
|
||||
free_trip(trip_import); /* All dives in trip have been consumed -> free */
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1062,11 +1053,10 @@ static bool try_to_merge_trip(dive_trip *trip_import, struct dive_table *import_
|
|||
void process_imported_dives(struct divelog *import_log, int flags,
|
||||
/* output parameters: */
|
||||
struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
|
||||
struct trip_table *trips_to_add, dive_site_table &sites_to_add,
|
||||
struct trip_table &trips_to_add, dive_site_table &sites_to_add,
|
||||
device_table &devices_to_add)
|
||||
{
|
||||
int i, j, nr, start_renumbering_at = 0;
|
||||
dive_trip *new_trip;
|
||||
bool sequence_changed = false;
|
||||
bool new_dive_has_number = false;
|
||||
bool last_old_dive_is_numbered;
|
||||
|
@ -1074,7 +1064,7 @@ void process_imported_dives(struct divelog *import_log, int flags,
|
|||
/* Make sure that output parameters don't contain garbage */
|
||||
clear_dive_table(dives_to_add);
|
||||
clear_dive_table(dives_to_remove);
|
||||
clear_trip_table(trips_to_add);
|
||||
trips_to_add.clear();
|
||||
sites_to_add.clear();
|
||||
devices_to_add.clear();
|
||||
|
||||
|
@ -1105,7 +1095,7 @@ void process_imported_dives(struct divelog *import_log, int flags,
|
|||
/* Autogroup tripless dives if desired by user. But don't autogroup
|
||||
* if tripless dives should be added to a new trip. */
|
||||
if (!(flags & IMPORT_ADD_TO_NEW_TRIP))
|
||||
autogroup_dives(import_log->dives.get(), import_log->trips.get());
|
||||
autogroup_dives(import_log->dives.get(), *import_log->trips);
|
||||
|
||||
/* If dive sites already exist, use the existing versions. */
|
||||
for (auto &new_ds: *import_log->sites) {
|
||||
|
@ -1140,10 +1130,9 @@ void process_imported_dives(struct divelog *import_log, int flags,
|
|||
* could be smarter here, but realistically not a whole lot of trips
|
||||
* will be imported so do a simple n*m loop until someone complains.
|
||||
*/
|
||||
for (i = 0; i < import_log->trips->nr; i++) {
|
||||
dive_trip *trip_import = import_log->trips->trips[i];
|
||||
for (auto &trip_import: *import_log->trips) {
|
||||
if ((flags & IMPORT_MERGE_ALL_TRIPS) || trip_import->autogen) {
|
||||
if (try_to_merge_trip(trip_import, import_log->dives.get(), flags & IMPORT_PREFER_IMPORTED, dives_to_add, dives_to_remove,
|
||||
if (try_to_merge_trip(*trip_import, import_log->dives.get(), flags & IMPORT_PREFER_IMPORTED, dives_to_add, dives_to_remove,
|
||||
&sequence_changed, &start_renumbering_at))
|
||||
continue;
|
||||
}
|
||||
|
@ -1160,16 +1149,18 @@ void process_imported_dives(struct divelog *import_log, int flags,
|
|||
remove_dive(d, import_log->dives.get());
|
||||
}
|
||||
|
||||
/* Then, add trip to list of trips to add */
|
||||
insert_trip(trip_import, trips_to_add);
|
||||
trip_import->dives.nr = 0; /* Caller is responsible for adding dives to trip */
|
||||
|
||||
/* Finally, add trip to list of trips to add */
|
||||
trips_to_add.put(std::move(trip_import));
|
||||
}
|
||||
import_log->trips->nr = 0; /* All trips were consumed */
|
||||
import_log->trips->clear(); /* All trips were consumed */
|
||||
|
||||
if ((flags & IMPORT_ADD_TO_NEW_TRIP) && import_log->dives->nr > 0) {
|
||||
/* Create a new trip for unassigned dives, if desired. */
|
||||
new_trip = create_trip_from_dive(import_log->dives->dives[0]);
|
||||
insert_trip(new_trip, trips_to_add);
|
||||
auto [new_trip, idx] = trips_to_add.put(
|
||||
create_trip_from_dive(import_log->dives->dives[0])
|
||||
);
|
||||
|
||||
/* Add all remaining dives to this trip */
|
||||
for (i = 0; i < import_log->dives->nr; i++) {
|
||||
|
@ -1296,7 +1287,7 @@ static int comp_dive_or_trip(struct dive_or_trip a, struct dive_or_trip b)
|
|||
if (a.dive && b.dive)
|
||||
return comp_dives(a.dive, b.dive);
|
||||
if (a.trip && b.trip)
|
||||
return comp_trips(a.trip, b.trip);
|
||||
return comp_trips(*a.trip, *b.trip);
|
||||
if (a.dive)
|
||||
return comp_dive_to_trip(a.dive, b.trip);
|
||||
else
|
||||
|
|
|
@ -35,7 +35,7 @@ extern void process_loaded_dives();
|
|||
extern void add_imported_dives(struct divelog *log, int flags);
|
||||
extern void process_imported_dives(struct divelog *import_log, int flags,
|
||||
struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
|
||||
struct trip_table *trips_to_add, dive_site_table &sites_to_add,
|
||||
struct trip_table &trips_to_add, dive_site_table &sites_to_add,
|
||||
std::vector<device> &devices_to_add);
|
||||
|
||||
extern int dive_table_get_insertion_index(struct dive_table *table, struct dive *dive);
|
||||
|
|
|
@ -17,15 +17,12 @@ divelog::divelog() :
|
|||
autogroup(false)
|
||||
{
|
||||
*dives = empty_dive_table;
|
||||
*trips = empty_trip_table;
|
||||
}
|
||||
|
||||
divelog::~divelog()
|
||||
{
|
||||
if (dives)
|
||||
clear_dive_table(dives.get());
|
||||
if (trips)
|
||||
clear_trip_table(trips.get());
|
||||
}
|
||||
|
||||
divelog::divelog(divelog &&) = default;
|
||||
|
@ -40,7 +37,14 @@ void divelog::delete_single_dive(int idx)
|
|||
return;
|
||||
}
|
||||
struct dive *dive = dives->dives[idx];
|
||||
remove_dive_from_trip(dive, trips.get());
|
||||
struct dive_trip *trip = unregister_dive_from_trip(dive);
|
||||
|
||||
// Deleting a dive may change the order of trips!
|
||||
if (trip)
|
||||
trips->sort();
|
||||
|
||||
if (trip && trip->dives.nr == 0)
|
||||
trips->pull(trip);
|
||||
unregister_dive_from_dive_site(dive);
|
||||
delete_dive_from_table(dives.get(), idx);
|
||||
}
|
||||
|
@ -48,12 +52,9 @@ void divelog::delete_single_dive(int idx)
|
|||
void divelog::clear()
|
||||
{
|
||||
while (dives->nr > 0)
|
||||
delete_single_dive(dives->nr - 1);
|
||||
delete_dive_from_table(dives.get(), dives->nr - 1);
|
||||
sites->clear();
|
||||
if (trips->nr != 0) {
|
||||
report_info("Warning: trip table not empty in divelog::clear()!");
|
||||
trips->nr = 0;
|
||||
}
|
||||
trips->clear();
|
||||
devices.clear();
|
||||
filter_presets->clear();
|
||||
}
|
||||
|
|
|
@ -1384,7 +1384,7 @@ static void finish_active_trip(struct git_parser_state *state)
|
|||
auto &trip = state->active_trip;
|
||||
|
||||
if (trip)
|
||||
insert_trip(trip.release(), state->log->trips.get());
|
||||
state->log->trips->put(std::move(trip));
|
||||
}
|
||||
|
||||
static void finish_active_dive(struct git_parser_state *state)
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
// SPDX-License-Identifier: GPL-2.0
|
||||
// Convenience classes defining owning pointers to C-objects that
|
||||
// automatically clean up the objects if the pointers go out of
|
||||
// scope. Based on unique_ptr<>.
|
||||
// In the future, we should replace these by real destructors.
|
||||
#ifndef OWNING_PTR_H
|
||||
#define OWNING_PTR_H
|
||||
|
||||
#include <memory>
|
||||
#include <cstdlib>
|
||||
|
||||
#endif
|
|
@ -140,6 +140,10 @@ public:
|
|||
}
|
||||
return it - this->begin();
|
||||
}
|
||||
// Note: this is silly - finding the pointer by a linear search
|
||||
// is probably significantly faster than doing a binary search.
|
||||
// But it helps finding consistency problems for now. Remove in
|
||||
// due course.
|
||||
pull_result pull(const T *item) {
|
||||
size_t idx = get_idx(item);
|
||||
if (idx == std::string::npos) {
|
||||
|
@ -148,6 +152,9 @@ public:
|
|||
}
|
||||
return { this->pull_at(idx), idx };
|
||||
}
|
||||
void sort() {
|
||||
std::sort(this->begin(), this->end(), [](const auto &a, const auto &b) { return CMP(*a, *b) < 0; });
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -291,7 +291,7 @@ void trip_end(struct parser_state *state)
|
|||
{
|
||||
if (!state->cur_trip)
|
||||
return;
|
||||
insert_trip(state->cur_trip.release(), state->log->trips.get());
|
||||
state->log->trips->put(std::move(state->cur_trip));
|
||||
}
|
||||
|
||||
void picture_start(struct parser_state *state)
|
||||
|
|
|
@ -983,7 +983,6 @@ static int create_git_tree(git_repository *repo, struct dir *root, bool select_o
|
|||
{
|
||||
int i;
|
||||
struct dive *dive;
|
||||
dive_trip *trip;
|
||||
|
||||
git_storage_update_progress(translate("gettextFromC", "Start saving data"));
|
||||
save_settings(repo, root);
|
||||
|
@ -991,8 +990,8 @@ static int create_git_tree(git_repository *repo, struct dir *root, bool select_o
|
|||
save_divesites(repo, root);
|
||||
save_filter_presets(repo, root);
|
||||
|
||||
for (i = 0; i < divelog.trips->nr; ++i)
|
||||
divelog.trips->trips[i]->saved = 0;
|
||||
for (auto &trip: *divelog.trips)
|
||||
trip->saved = false;
|
||||
|
||||
/* save the dives */
|
||||
git_storage_update_progress(translate("gettextFromC", "Start saving dives"));
|
||||
|
@ -1000,7 +999,7 @@ static int create_git_tree(git_repository *repo, struct dir *root, bool select_o
|
|||
struct tm tm;
|
||||
struct dir *tree;
|
||||
|
||||
trip = dive->divetrip;
|
||||
dive_trip *trip = dive->divetrip;
|
||||
|
||||
if (select_only) {
|
||||
if (!dive->selected)
|
||||
|
@ -1010,7 +1009,7 @@ static int create_git_tree(git_repository *repo, struct dir *root, bool select_o
|
|||
}
|
||||
|
||||
/* Create the date-based hierarchy */
|
||||
utc_mkdate(trip ? trip_date(trip) : dive->when, &tm);
|
||||
utc_mkdate(trip ? trip_date(*trip) : dive->when, &tm);
|
||||
tree = mktree(repo, root, "%04d", tm.tm_year);
|
||||
tree = mktree(repo, tree, "%02d", tm.tm_mon + 1);
|
||||
|
||||
|
|
|
@ -438,15 +438,14 @@ static void write_trips(struct membuffer *b, const char *photos_dir, bool select
|
|||
{
|
||||
int i, dive_no = 0;
|
||||
const struct dive *dive;
|
||||
dive_trip *trip;
|
||||
char sep_ = ' ';
|
||||
char *sep = &sep_;
|
||||
|
||||
for (i = 0; i < divelog.trips->nr; ++i)
|
||||
divelog.trips->trips[i]->saved = 0;
|
||||
for (auto &trip: *divelog.trips)
|
||||
trip->saved = 0;
|
||||
|
||||
for_each_dive (i, dive) {
|
||||
trip = dive->divetrip;
|
||||
dive_trip *trip = dive->divetrip;
|
||||
|
||||
/*Continue if the dive have no trips or we have seen this trip before*/
|
||||
if (!trip || trip->saved)
|
||||
|
|
|
@ -543,16 +543,16 @@ int save_dive(FILE *f, struct dive *dive, bool anonymize)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void save_trip(struct membuffer *b, dive_trip *trip, bool anonymize)
|
||||
static void save_trip(struct membuffer *b, dive_trip &trip, bool anonymize)
|
||||
{
|
||||
int i;
|
||||
struct dive *dive;
|
||||
|
||||
put_format(b, "<trip");
|
||||
show_date(b, trip_date(trip));
|
||||
show_utf8(b, trip->location.c_str(), " location=\'", "\'", 1);
|
||||
show_utf8(b, trip.location.c_str(), " location=\'", "\'", 1);
|
||||
put_format(b, ">\n");
|
||||
show_utf8(b, trip->notes.c_str(), "<notes>", "</notes>\n", 0);
|
||||
show_utf8(b, trip.notes.c_str(), "<notes>", "</notes>\n", 0);
|
||||
|
||||
/*
|
||||
* Incredibly cheesy: we want to save the dives sorted, and they
|
||||
|
@ -561,7 +561,7 @@ static void save_trip(struct membuffer *b, dive_trip *trip, bool anonymize)
|
|||
* check the divetrip pointer..
|
||||
*/
|
||||
for_each_dive(i, dive) {
|
||||
if (dive->divetrip == trip)
|
||||
if (dive->divetrip == &trip)
|
||||
save_one_dive_to_mb(b, dive, anonymize);
|
||||
}
|
||||
|
||||
|
@ -642,7 +642,6 @@ static void save_dives_buffer(struct membuffer *b, bool select_only, bool anonym
|
|||
{
|
||||
int i;
|
||||
struct dive *dive;
|
||||
dive_trip *trip;
|
||||
|
||||
put_format(b, "<divelog program='subsurface' version='%d'>\n<settings>\n", DATAFORMAT_VERSION);
|
||||
|
||||
|
@ -686,8 +685,8 @@ static void save_dives_buffer(struct membuffer *b, bool select_only, bool anonym
|
|||
put_format(b, "</site>\n");
|
||||
}
|
||||
put_format(b, "</divesites>\n<dives>\n");
|
||||
for (i = 0; i < divelog.trips->nr; ++i)
|
||||
divelog.trips->trips[i]->saved = 0;
|
||||
for (auto &trip: *divelog.trips)
|
||||
trip->saved = 0;
|
||||
|
||||
/* save the filter presets */
|
||||
save_filter_presets(b);
|
||||
|
@ -701,7 +700,7 @@ static void save_dives_buffer(struct membuffer *b, bool select_only, bool anonym
|
|||
save_one_dive_to_mb(b, dive, anonymize);
|
||||
|
||||
} else {
|
||||
trip = dive->divetrip;
|
||||
dive_trip *trip = dive->divetrip;
|
||||
|
||||
/* Bare dive without a trip? */
|
||||
if (!trip) {
|
||||
|
@ -715,7 +714,7 @@ static void save_dives_buffer(struct membuffer *b, bool select_only, bool anonym
|
|||
|
||||
/* We haven't seen this trip before - save it and all dives */
|
||||
trip->saved = 1;
|
||||
save_trip(b, trip, anonymize);
|
||||
save_trip(b, *trip, anonymize);
|
||||
}
|
||||
}
|
||||
put_format(b, "</dives>\n</divelog>\n");
|
||||
|
|
|
@ -173,8 +173,8 @@ QVector<dive *> setSelectionCore(const std::vector<dive *> &selection, dive *cur
|
|||
static void clear_trip_selection()
|
||||
{
|
||||
amount_trips_selected = 0;
|
||||
for (int i = 0; i < divelog.trips->nr; ++i)
|
||||
divelog.trips->trips[i]->selected = false;
|
||||
for (auto &trip: *divelog.trips)
|
||||
trip->selected = false;
|
||||
}
|
||||
|
||||
// Reset the selection to the dives of the "selection" vector and send the appropriate signals.
|
||||
|
@ -221,10 +221,8 @@ void setTripSelection(dive_trip *trip, dive *currentDive)
|
|||
dive &d = *divelog.dives->dives[i];
|
||||
d.selected = d.divetrip == trip;
|
||||
}
|
||||
for (int i = 0; i < divelog.trips->nr; ++i) {
|
||||
dive_trip *t = divelog.trips->trips[i];
|
||||
t->selected = t == trip;
|
||||
}
|
||||
for (auto &t: *divelog.trips)
|
||||
t->selected = t.get() == trip;
|
||||
|
||||
amount_selected = trip->dives.nr;
|
||||
amount_trips_selected = 1;
|
||||
|
@ -315,9 +313,9 @@ struct dive_trip *single_selected_trip()
|
|||
{
|
||||
if (amount_trips_selected != 1)
|
||||
return NULL;
|
||||
for (int i = 0; i < divelog.trips->nr; ++i) {
|
||||
if (divelog.trips->trips[i]->selected)
|
||||
return divelog.trips->trips[i];
|
||||
for (auto &trip: *divelog.trips) {
|
||||
if (trip->selected)
|
||||
return trip.get();
|
||||
}
|
||||
report_info("warning: found no selected trip even though one should be selected");
|
||||
return NULL; // shouldn't happen
|
||||
|
|
|
@ -299,26 +299,23 @@ QString formatMinutes(int seconds)
|
|||
return QString::asprintf("%d:%.2d", FRACTION_TUPLE(seconds, 60));
|
||||
}
|
||||
|
||||
QString formatTripTitle(const dive_trip *trip)
|
||||
QString formatTripTitle(const dive_trip &trip)
|
||||
{
|
||||
if (!trip)
|
||||
return QString();
|
||||
|
||||
timestamp_t when = trip_date(trip);
|
||||
bool getday = trip_is_single_day(trip);
|
||||
|
||||
QDateTime localTime = timestampToDateTime(when);
|
||||
|
||||
QString prefix = !trip->location.empty() ? QString::fromStdString(trip->location) + ", " : QString();
|
||||
QString prefix = !trip.location.empty() ? QString::fromStdString(trip.location) + ", " : QString();
|
||||
if (getday)
|
||||
return prefix + loc.toString(localTime, prefs.date_format);
|
||||
else
|
||||
return prefix + loc.toString(localTime, "MMM yyyy");
|
||||
}
|
||||
|
||||
QString formatTripTitleWithDives(const dive_trip *trip)
|
||||
QString formatTripTitleWithDives(const dive_trip &trip)
|
||||
{
|
||||
int nr = trip->dives.nr;
|
||||
int nr = trip.dives.nr;
|
||||
return formatTripTitle(trip) + " " +
|
||||
gettextFromC::tr("(%n dive(s))", "", nr);
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ QString formatDiveDateTime(const dive *d);
|
|||
QString formatDiveGasString(const dive *d);
|
||||
QString formatDayOfWeek(int day);
|
||||
QString formatMinutes(int seconds);
|
||||
QString formatTripTitle(const dive_trip *trip);
|
||||
QString formatTripTitleWithDives(const dive_trip *trip);
|
||||
QString formatTripTitle(const dive_trip &trip);
|
||||
QString formatTripTitleWithDives(const dive_trip &trip);
|
||||
|
||||
#endif
|
||||
|
|
177
core/trip.cpp
177
core/trip.cpp
|
@ -12,22 +12,19 @@
|
|||
#ifdef DEBUG_TRIP
|
||||
void dump_trip_list()
|
||||
{
|
||||
dive_trip *trip;
|
||||
int i = 0;
|
||||
timestamp_t last_time = 0;
|
||||
|
||||
for (i = 0; i < divelog.trips->nr; ++i) {
|
||||
for (auto &trip: divelog.trips) {
|
||||
struct tm tm;
|
||||
trip = divelog.trips->trips[i];
|
||||
utc_mkdate(trip_date(trip), &tm);
|
||||
if (trip_date(trip) < last_time)
|
||||
utc_mkdate(trip_date(*trip), &tm);
|
||||
if (trip_date(*trip) < last_time)
|
||||
printf("\n\ntrip_table OUT OF ORDER!!!\n\n\n");
|
||||
printf("%s trip %d to \"%s\" on %04u-%02u-%02u %02u:%02u:%02u (%d dives - %p)\n",
|
||||
trip->autogen ? "autogen " : "",
|
||||
i + 1, trip->location.c_str(),
|
||||
tm.tm_year, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
|
||||
trip->dives.nr, trip);
|
||||
last_time = trip_date(trip);
|
||||
trip->dives.nr, trip.get());
|
||||
last_time = trip_date(*trip);
|
||||
}
|
||||
printf("-----\n");
|
||||
}
|
||||
|
@ -42,35 +39,18 @@ dive_trip::~dive_trip()
|
|||
free(dives.dives);
|
||||
}
|
||||
|
||||
/* free resources associated with a trip structure */
|
||||
void free_trip(dive_trip *trip)
|
||||
timestamp_t trip_date(const struct dive_trip &trip)
|
||||
{
|
||||
delete trip;
|
||||
if (trip.dives.nr == 0)
|
||||
return 0;
|
||||
return trip.dives.dives[0]->when;
|
||||
}
|
||||
|
||||
/* Trip table functions */
|
||||
static MAKE_GET_IDX(trip_table, struct dive_trip *, trips)
|
||||
static MAKE_GROW_TABLE(trip_table, struct dive_trip *, trips)
|
||||
static MAKE_GET_INSERTION_INDEX(trip_table, struct dive_trip *, trips, trip_less_than)
|
||||
static MAKE_ADD_TO(trip_table, struct dive_trip *, trips)
|
||||
static MAKE_REMOVE_FROM(trip_table, trips)
|
||||
MAKE_SORT(trip_table, struct dive_trip *, trips, comp_trips)
|
||||
MAKE_REMOVE(trip_table, struct dive_trip *, trip)
|
||||
MAKE_CLEAR_TABLE(trip_table, trips, trip)
|
||||
MAKE_MOVE_TABLE(trip_table, trips)
|
||||
|
||||
timestamp_t trip_date(const struct dive_trip *trip)
|
||||
static timestamp_t trip_enddate(const struct dive_trip &trip)
|
||||
{
|
||||
if (!trip || trip->dives.nr == 0)
|
||||
if (trip.dives.nr == 0)
|
||||
return 0;
|
||||
return trip->dives.dives[0]->when;
|
||||
}
|
||||
|
||||
timestamp_t trip_enddate(const struct dive_trip *trip)
|
||||
{
|
||||
if (!trip || trip->dives.nr == 0)
|
||||
return 0;
|
||||
return dive_endtime(trip->dives.dives[trip->dives.nr - 1]);
|
||||
return dive_endtime(trip.dives.dives[trip.dives.nr - 1]);
|
||||
}
|
||||
|
||||
/* check if we have a trip right before / after this dive */
|
||||
|
@ -117,39 +97,9 @@ struct dive_trip *unregister_dive_from_trip(struct dive *dive)
|
|||
return trip;
|
||||
}
|
||||
|
||||
static void delete_trip(dive_trip *trip, struct trip_table *trip_table_arg)
|
||||
std::unique_ptr<dive_trip> create_trip_from_dive(const struct dive *dive)
|
||||
{
|
||||
remove_trip(trip, trip_table_arg);
|
||||
free_trip(trip);
|
||||
}
|
||||
|
||||
void remove_dive_from_trip(struct dive *dive, struct trip_table *trip_table_arg)
|
||||
{
|
||||
struct dive_trip *trip = unregister_dive_from_trip(dive);
|
||||
if (trip && trip->dives.nr == 0)
|
||||
delete_trip(trip, trip_table_arg);
|
||||
}
|
||||
|
||||
dive_trip *alloc_trip()
|
||||
{
|
||||
return new dive_trip;
|
||||
}
|
||||
|
||||
/* insert the trip into the trip table */
|
||||
void insert_trip(struct dive_trip *dive_trip, struct trip_table *trip_table_arg)
|
||||
{
|
||||
int idx = trip_table_get_insertion_index(trip_table_arg, dive_trip);
|
||||
add_to_trip_table(trip_table_arg, idx, dive_trip);
|
||||
#ifdef DEBUG_TRIP
|
||||
dump_trip_list();
|
||||
#endif
|
||||
}
|
||||
|
||||
dive_trip *create_trip_from_dive(struct dive *dive)
|
||||
{
|
||||
dive_trip *trip;
|
||||
|
||||
trip = alloc_trip();
|
||||
auto trip = std::make_unique<dive_trip>();
|
||||
trip->location = get_dive_location(dive);
|
||||
|
||||
return trip;
|
||||
|
@ -162,13 +112,12 @@ dive_trip *create_trip_from_dive(struct dive *dive)
|
|||
|
||||
/*
|
||||
* Find a trip a new dive should be autogrouped with. If no such trips
|
||||
* exist, allocate a new trip. The bool "*allocated" is set to true
|
||||
* if a new trip was allocated.
|
||||
* exist, allocate a new trip. A unique_ptr is returned if a new trip
|
||||
* was allocated. The caller has to store it.
|
||||
*/
|
||||
dive_trip *get_trip_for_new_dive(struct dive *new_dive, bool *allocated)
|
||||
std::pair<dive_trip *, std::unique_ptr<dive_trip>> get_trip_for_new_dive(const struct dive *new_dive)
|
||||
{
|
||||
struct dive *d;
|
||||
dive_trip *trip;
|
||||
dive *d;
|
||||
int i;
|
||||
|
||||
/* Find dive that is within TRIP_THRESHOLD of current dive */
|
||||
|
@ -177,35 +126,30 @@ dive_trip *get_trip_for_new_dive(struct dive *new_dive, bool *allocated)
|
|||
if (d->when >= new_dive->when + TRIP_THRESHOLD)
|
||||
break;
|
||||
|
||||
if (d->when + TRIP_THRESHOLD >= new_dive->when && d->divetrip) {
|
||||
/* Found a dive with trip in the range */
|
||||
*allocated = false;
|
||||
return d->divetrip;
|
||||
}
|
||||
if (d->when + TRIP_THRESHOLD >= new_dive->when && d->divetrip)
|
||||
return { d->divetrip, nullptr }; /* Found a dive with trip in the range */
|
||||
}
|
||||
|
||||
/* Didn't find a trip -> allocate a new one */
|
||||
trip = create_trip_from_dive(new_dive);
|
||||
auto trip = create_trip_from_dive(new_dive);
|
||||
trip->autogen = true;
|
||||
*allocated = true;
|
||||
return trip;
|
||||
auto t = trip.get();
|
||||
return { t, std::move(trip) };
|
||||
}
|
||||
|
||||
/* lookup of trip in main trip_table based on its id */
|
||||
dive_trip *get_trip_by_uniq_id(int tripId)
|
||||
{
|
||||
for (int i = 0; i < divelog.trips->nr; i++) {
|
||||
if (divelog.trips->trips[i]->id == tripId)
|
||||
return divelog.trips->trips[i];
|
||||
}
|
||||
return NULL;
|
||||
auto it = std::find_if(divelog.trips->begin(), divelog.trips->end(),
|
||||
[tripId](auto &t) { return t->id == tripId; });
|
||||
return it != divelog.trips->end() ? it->get() : nullptr;
|
||||
}
|
||||
|
||||
/* Check if two trips overlap time-wise up to trip threshold. */
|
||||
bool trips_overlap(const struct dive_trip *t1, const struct dive_trip *t2)
|
||||
bool trips_overlap(const struct dive_trip &t1, const struct dive_trip &t2)
|
||||
{
|
||||
/* First, handle the empty-trip cases. */
|
||||
if (t1->dives.nr == 0 || t2->dives.nr == 0)
|
||||
if (t1.dives.nr == 0 || t2.dives.nr == 0)
|
||||
return 0;
|
||||
|
||||
if (trip_date(t1) < trip_date(t2))
|
||||
|
@ -217,21 +161,22 @@ bool trips_overlap(const struct dive_trip *t1, const struct dive_trip *t2)
|
|||
/*
|
||||
* Collect dives for auto-grouping. Pass in first dive which should be checked.
|
||||
* Returns range of dives that should be autogrouped and trip it should be
|
||||
* associated to. If the returned trip was newly allocated, the last bool
|
||||
* associated to. If the returned trip was newly allocated, a std::unique_ptr<>
|
||||
* to the trip is returned.
|
||||
* is set to true. Caller still has to register it in the system. Note
|
||||
* whereas this looks complicated - it is needed by the undo-system, which
|
||||
* manually injects the new trips. If there are no dives to be autogrouped,
|
||||
* return NULL.
|
||||
*/
|
||||
dive_trip *get_dives_to_autogroup(struct dive_table *table, int start, int *from, int *to, bool *allocated)
|
||||
std::vector<dives_to_autogroup_result> get_dives_to_autogroup(struct dive_table *table)
|
||||
{
|
||||
int i;
|
||||
std::vector<dives_to_autogroup_result> res;
|
||||
struct dive *lastdive = NULL;
|
||||
|
||||
/* Find first dive that should be merged and remember any previous
|
||||
* dive that could be merged into.
|
||||
*/
|
||||
for (i = start; i < table->nr; i++) {
|
||||
for (int i = 0; i < table->nr; ++i) {
|
||||
struct dive *dive = table->dives[i];
|
||||
dive_trip *trip;
|
||||
|
||||
|
@ -248,22 +193,22 @@ dive_trip *get_dives_to_autogroup(struct dive_table *table, int start, int *from
|
|||
}
|
||||
|
||||
/* We found a dive, let's see if we have to allocate a new trip */
|
||||
std::unique_ptr<dive_trip> allocated;
|
||||
if (!lastdive || dive->when >= lastdive->when + TRIP_THRESHOLD) {
|
||||
/* allocate new trip */
|
||||
trip = create_trip_from_dive(dive);
|
||||
trip->autogen = true;
|
||||
*allocated = true;
|
||||
allocated = create_trip_from_dive(dive);
|
||||
allocated->autogen = true;
|
||||
trip = allocated.get();
|
||||
} else {
|
||||
/* use trip of previous dive */
|
||||
trip = lastdive->divetrip;
|
||||
*allocated = false;
|
||||
}
|
||||
|
||||
// Now, find all dives that will be added to this trip
|
||||
lastdive = dive;
|
||||
*from = i;
|
||||
for (*to = *from + 1; *to < table->nr; (*to)++) {
|
||||
dive = table->dives[*to];
|
||||
int to;
|
||||
for (to = i + 1; to < table->nr; to++) {
|
||||
dive = table->dives[to];
|
||||
if (dive->divetrip || dive->notrip ||
|
||||
dive->when >= lastdive->when + TRIP_THRESHOLD)
|
||||
break;
|
||||
|
@ -271,11 +216,11 @@ dive_trip *get_dives_to_autogroup(struct dive_table *table, int start, int *from
|
|||
trip->location = get_dive_location(dive);
|
||||
lastdive = dive;
|
||||
}
|
||||
return trip;
|
||||
res.push_back({ i, to, trip, std::move(allocated) });
|
||||
i = to - 1;
|
||||
}
|
||||
|
||||
/* Did not find anyhting - mark as end */
|
||||
return NULL;
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Out of two strings, get the string that is not empty (if any). */
|
||||
|
@ -286,11 +231,10 @@ static std::string non_empty_string(const std::string &a, const std::string &b)
|
|||
|
||||
/* This combines the information of two trips, generating a
|
||||
* new trip. To support undo, we have to preserve the old trips. */
|
||||
dive_trip *combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b)
|
||||
std::unique_ptr<dive_trip> combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b)
|
||||
{
|
||||
dive_trip *trip;
|
||||
auto trip = std::make_unique<dive_trip>();
|
||||
|
||||
trip = alloc_trip();
|
||||
trip->location = non_empty_string(trip_a->location, trip_b->location);
|
||||
trip->notes = non_empty_string(trip_a->notes, trip_b->notes);
|
||||
|
||||
|
@ -298,20 +242,19 @@ dive_trip *combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b)
|
|||
}
|
||||
|
||||
/* Trips are compared according to the first dive in the trip. */
|
||||
int comp_trips(const struct dive_trip *a, const struct dive_trip *b)
|
||||
int comp_trips(const struct dive_trip &a, const struct dive_trip &b)
|
||||
{
|
||||
/* This should never happen, nevertheless don't crash on trips
|
||||
* with no (or worse a negative number of) dives. */
|
||||
if (a->dives.nr <= 0)
|
||||
return b->dives.nr <= 0 ? 0 : -1;
|
||||
if (b->dives.nr <= 0)
|
||||
// To make sure that trips never compare equal, compare by
|
||||
// address if both are empty.
|
||||
if (&a == &b)
|
||||
return 0; // reflexivity. shouldn't happen.
|
||||
if (a.dives.nr <= 0 && b.dives.nr <= 0)
|
||||
return &a < &b ? -1 : 1;
|
||||
if (a.dives.nr <= 0)
|
||||
return -1;
|
||||
if (b.dives.nr <= 0)
|
||||
return 1;
|
||||
return comp_dives(a->dives.dives[0], b->dives.dives[0]);
|
||||
}
|
||||
|
||||
bool trip_less_than(const struct dive_trip *a, const struct dive_trip *b)
|
||||
{
|
||||
return comp_trips(a, b) < 0;
|
||||
return comp_dives(a.dives.dives[0], b.dives.dives[0]);
|
||||
}
|
||||
|
||||
static bool is_same_day(timestamp_t trip_when, timestamp_t dive_when)
|
||||
|
@ -330,12 +273,12 @@ static bool is_same_day(timestamp_t trip_when, timestamp_t dive_when)
|
|||
return (tmd.tm_mday == tmt.tm_mday) && (tmd.tm_mon == tmt.tm_mon) && (tmd.tm_year == tmt.tm_year);
|
||||
}
|
||||
|
||||
bool trip_is_single_day(const struct dive_trip *trip)
|
||||
bool trip_is_single_day(const struct dive_trip &trip)
|
||||
{
|
||||
if (trip->dives.nr <= 1)
|
||||
if (trip.dives.nr <= 1)
|
||||
return true;
|
||||
return is_same_day(trip->dives.dives[0]->when,
|
||||
trip->dives.dives[trip->dives.nr - 1]->when);
|
||||
return is_same_day(trip.dives.dives[0]->when,
|
||||
trip.dives.dives[trip.dives.nr - 1]->when);
|
||||
}
|
||||
|
||||
int trip_shown_dives(const struct dive_trip *trip)
|
||||
|
|
46
core/trip.h
46
core/trip.h
|
@ -3,6 +3,7 @@
|
|||
#define TRIP_H
|
||||
|
||||
#include "divelist.h"
|
||||
#include "owning_table.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
|
@ -21,42 +22,37 @@ struct dive_trip
|
|||
~dive_trip();
|
||||
};
|
||||
|
||||
typedef struct trip_table {
|
||||
int nr, allocated;
|
||||
struct dive_trip **trips;
|
||||
} trip_table_t;
|
||||
int comp_trips(const dive_trip &t1, const dive_trip &t2);
|
||||
|
||||
static const trip_table_t empty_trip_table = { 0, 0, (struct dive_trip **)0 };
|
||||
struct trip_table : public sorted_owning_table<dive_trip, &comp_trips> {
|
||||
};
|
||||
|
||||
extern void add_dive_to_trip(struct dive *, dive_trip *);
|
||||
extern struct dive_trip *unregister_dive_from_trip(struct dive *dive);
|
||||
extern void remove_dive_from_trip(struct dive *dive, struct trip_table *trip_table_arg);
|
||||
|
||||
extern void insert_trip(dive_trip *trip, struct trip_table *trip_table_arg);
|
||||
extern int remove_trip(const dive_trip *trip, struct trip_table *trip_table_arg);
|
||||
extern void free_trip(dive_trip *trip);
|
||||
extern timestamp_t trip_date(const struct dive_trip *trip);
|
||||
extern timestamp_t trip_enddate(const struct dive_trip *trip);
|
||||
extern timestamp_t trip_date(const struct dive_trip &trip);
|
||||
|
||||
extern bool trip_less_than(const struct dive_trip *a, const struct dive_trip *b);
|
||||
extern int comp_trips(const struct dive_trip *a, const struct dive_trip *b);
|
||||
extern void sort_trip_table(struct trip_table *table);
|
||||
extern std::unique_ptr<dive_trip> create_trip_from_dive(const struct dive *dive);
|
||||
extern dive_trip *create_and_hookup_trip_from_dive(const struct dive *dive, struct trip_table &trip_table_arg);
|
||||
|
||||
extern dive_trip *alloc_trip();
|
||||
extern dive_trip *create_trip_from_dive(struct dive *dive);
|
||||
extern dive_trip *get_dives_to_autogroup(struct dive_table *table, int start, int *from, int *to, bool *allocated);
|
||||
extern dive_trip *get_trip_for_new_dive(struct dive *new_dive, bool *allocated);
|
||||
// Result item of get_dives_to_autogroup()
|
||||
struct dives_to_autogroup_result {
|
||||
int from, to; // Group dives in the range [from, to)
|
||||
dive_trip *trip; // Pointer to trip
|
||||
std::unique_ptr<dive_trip> created_trip;
|
||||
// Is set if the trip was newly created - caller has to store it.
|
||||
};
|
||||
|
||||
extern std::vector<dives_to_autogroup_result> get_dives_to_autogroup(struct dive_table *table);
|
||||
extern std::pair<dive_trip *, std::unique_ptr<dive_trip>> get_trip_for_new_dive(const struct dive *new_dive);
|
||||
extern dive_trip *get_trip_by_uniq_id(int tripId);
|
||||
extern bool trips_overlap(const struct dive_trip *t1, const struct dive_trip *t2);
|
||||
extern bool trips_overlap(const struct dive_trip &t1, const struct dive_trip &t2);
|
||||
|
||||
extern dive_trip *combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b);
|
||||
extern std::unique_ptr<dive_trip> combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b);
|
||||
extern bool is_trip_before_after(const struct dive *dive, bool before);
|
||||
extern bool trip_is_single_day(const struct dive_trip *trip);
|
||||
extern bool trip_is_single_day(const struct dive_trip &trip);
|
||||
extern int trip_shown_dives(const struct dive_trip *trip);
|
||||
|
||||
void move_trip_table(struct trip_table *src, struct trip_table *dst);
|
||||
void clear_trip_table(struct trip_table *table);
|
||||
|
||||
#ifdef DEBUG_TRIP
|
||||
extern void dump_trip_list();
|
||||
#endif
|
||||
|
@ -65,6 +61,6 @@ extern void dump_trip_list();
|
|||
* passed through QVariants and through QML. See comment in dive.h. */
|
||||
#include <QObject>
|
||||
Q_DECLARE_METATYPE(struct dive_trip *);
|
||||
Q_DECLARE_METATYPE(trip_table_t *);
|
||||
Q_DECLARE_METATYPE(trip_table *);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue