core: replace dive_site::dives by an std::vector<>

Since this is now in C++, we don't have to use our crazy
TABLE_* macros.

This contains a logic change: the dives associated to a
dive site are now unsorted.

The old code was subtly buggy: dives were added in a sorted
manner, but when the dive was edited the list was not
resorted. Very unlikely that this leads to a serious
problem, still not good.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-05-04 14:41:04 +02:00 committed by bstoeger
parent 3f8b4604be
commit 801b5d50b2
12 changed files with 39 additions and 45 deletions

View file

@ -1150,7 +1150,7 @@ void process_imported_dives(struct divelog *import_log, int flags,
if (!old_ds) {
/* Dive site doesn't exist. Add it to list of dive sites to be added. */
new_ds->dives.nr = 0; /* Caller is responsible for adding dives to site */
new_ds->dives.clear(); /* Caller is responsible for adding dives to site */
add_dive_site_to_table(new_ds, sites_to_add);
continue;
}

View file

@ -180,7 +180,6 @@ dive_site::~dive_site()
free(name);
free(notes);
free(description);
free(dives.dives);
}
/* when parsing, dive sites are identified by uuid */
@ -199,20 +198,15 @@ struct dive_site *alloc_or_get_dive_site(uint32_t uuid, struct dive_site_table *
return ds;
}
int nr_of_dives_at_dive_site(struct dive_site *ds)
size_t nr_of_dives_at_dive_site(const dive_site &ds)
{
return ds->dives.nr;
return ds.dives.size();
}
bool is_dive_site_selected(struct dive_site *ds)
bool is_dive_site_selected(const struct dive_site &ds)
{
int i;
for (i = 0; i < ds->dives.nr; i++) {
if (ds->dives.dives[i]->selected)
return true;
}
return false;
return any_of(ds.dives.begin(), ds.dives.end(),
[](dive *dive) { return dive->selected; });
}
int unregister_dive_site(struct dive_site *ds)
@ -353,7 +347,6 @@ void purge_empty_dive_sites(struct dive_site_table *ds_table)
void add_dive_to_dive_site(struct dive *d, struct dive_site *ds)
{
int idx;
if (!d) {
report_info("Warning: add_dive_to_dive_site called with NULL dive");
return;
@ -368,8 +361,7 @@ void add_dive_to_dive_site(struct dive *d, struct dive_site *ds)
report_info("Warning: adding dive that already belongs to a dive site to a different site");
unregister_dive_from_dive_site(d);
}
idx = dive_table_get_insertion_index(&ds->dives, d);
add_to_dive_table(&ds->dives, idx, d);
ds->dives.push_back(d);
d->dive_site = ds;
}
@ -377,8 +369,12 @@ struct dive_site *unregister_dive_from_dive_site(struct dive *d)
{
struct dive_site *ds = d->dive_site;
if (!ds)
return NULL;
remove_dive(d, &ds->dives);
d->dive_site = NULL;
return nullptr;
auto it = std::find(ds->dives.begin(), ds->dives.end(), d);
if (it != ds->dives.end())
ds->dives.erase(it);
else
report_info("Warning: dive not found in divesite table, even though it should be registered there.");
d->dive_site = nullptr;
return ds;
}

View file

@ -15,7 +15,7 @@ struct dive_site
{
uint32_t uuid = 0;
char *name = nullptr;
struct dive_table dives = { 0, 0, nullptr };
std::vector<dive *> dives;
location_t location = { { 9 }, { 0 } };
char *description = nullptr;
char *notes = nullptr;
@ -50,8 +50,8 @@ void sort_dive_site_table(struct dive_site_table *ds_table);
int add_dive_site_to_table(struct dive_site *ds, struct dive_site_table *ds_table);
struct dive_site *alloc_or_get_dive_site(uint32_t uuid, struct dive_site_table *ds_table);
struct dive_site *alloc_dive_site();
int nr_of_dives_at_dive_site(struct dive_site *ds);
bool is_dive_site_selected(struct dive_site *ds);
size_t nr_of_dives_at_dive_site(const struct dive_site &ds);
bool is_dive_site_selected(const struct dive_site &ds);
int unregister_dive_site(struct dive_site *ds);
int register_dive_site(struct dive_site *ds);
void delete_dive_site(struct dive_site *ds, struct dive_site_table *ds_table);

View file

@ -707,7 +707,7 @@ static void save_dives_buffer(struct membuffer *b, bool select_only, bool anonym
if (dive_site_is_empty(ds))
continue;
/* Only write used dive sites when exporting selected dives */
if (select_only && !is_dive_site_selected(ds))
if (select_only && !is_dive_site_selected(*ds))
continue;
put_format(b, "<site uuid='%8x'", ds->uuid);