Core: add add_dive_to_table() function

Up to now, dives were added to the global dive table with
add_single_dive(). Split out the funtionality to add a dive to
an arbitrary dive in the add_dive_to_table_function(). The
difference compared to record_dive_to_table is that dives
are added at a specific position or the sort-criterion given
by dive_less_than(). This will allow to use a dive tabe for trips
instead of a linked list.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-11-08 14:02:36 +01:00 committed by Dirk Hohndel
parent 0618aa737f
commit bc7afebc23
4 changed files with 30 additions and 20 deletions

View file

@ -418,8 +418,6 @@ extern void add_dive_to_trip(struct dive *, dive_trip_t *);
struct dive *unregister_dive(int idx);
extern void delete_single_dive(int idx);
extern int dive_get_insertion_index(struct dive *dive);
extern void add_single_dive(int idx, struct dive *dive);
extern void insert_trip(dive_trip_t *trip);
extern void unregister_trip(dive_trip_t *trip);

View file

@ -30,6 +30,7 @@
* dive_trip_t *combine_trips_create(struct dive_trip *trip_a, struct dive_trip *trip_b)
* struct dive *unregister_dive(int idx)
* void delete_single_dive(int idx)
* void add_dive_to_table(struct dive_table *table, int idx, struct dive *dive)
* void add_single_dive(int idx, struct dive *dive)
* struct dive *merge_two_dives(struct dive *a, struct dive *b)
* void select_dive(struct dive *dive)
@ -1111,36 +1112,44 @@ struct dive **grow_dive_table(struct dive_table *table)
}
/* get the index where we want to insert the dive so that everything stays
* ordered reverse-chronologically */
int dive_get_insertion_index(struct dive *dive)
* ordered according to dive_less_than() */
int dive_get_insertion_index(struct dive_table *table, struct dive *dive)
{
/* we might want to use binary search here */
for (int i = 0; i < dive_table.nr; i++) {
if (dive->when <= dive_table.dives[i]->when)
for (int i = 0; i < table->nr; i++) {
if (dive_less_than(dive, table->dives[i]))
return i;
}
return dive_table.nr;
return table->nr;
}
/* add a dive at the given index. if the index is negative, the dive will
* be added according to reverse chronological order */
void add_single_dive(int idx, struct dive *dive)
/* add a dive at the given index to a dive table. if the index is negative,
* the dive will be added according to dive_less_than() order */
void add_dive_to_table(struct dive_table *table, int idx, struct dive *dive)
{
int i;
if (idx < 0)
idx = dive_get_insertion_index(dive);
grow_dive_table(&dive_table);
dive_table.nr++;
if (dive->selected)
amount_selected++;
idx = dive_get_insertion_index(table, dive);
grow_dive_table(table);
table->nr++;
for (i = idx; i < dive_table.nr; i++) {
struct dive *tmp = dive_table.dives[i];
dive_table.dives[i] = dive;
for (i = idx; i < table->nr; i++) {
struct dive *tmp = table->dives[i];
table->dives[i] = dive;
dive = tmp;
}
}
/* add a dive at the given index in the global dive table and keep track
* of the number of selected dives. if the index is negative, the dive will
* be added according to dive_less_than() order */
void add_single_dive(int idx, struct dive *dive)
{
add_dive_to_table(&dive_table, idx, dive);
if (dive->selected)
amount_selected++;
}
bool consecutive_selected()
{
struct dive *d;

View file

@ -22,7 +22,10 @@ extern void process_loaded_dives();
extern void process_imported_dives(struct dive_table *import_table, bool prefer_imported, bool downloaded);
extern char *get_dive_gas_string(const struct dive *dive);
struct dive **grow_dive_table(struct dive_table *table);
extern struct dive **grow_dive_table(struct dive_table *table);
extern int dive_get_insertion_index(struct dive_table *table, struct dive *dive);
extern void add_dive_to_table(struct dive_table *table, int idx, struct dive *dive);
extern void add_single_dive(int idx, struct dive *dive);
extern void get_dive_gas(const struct dive *dive, int *o2_p, int *he_p, int *o2low_p);
extern int get_divenr(const struct dive *dive);
extern int get_divesite_idx(const struct dive_site *ds);

View file

@ -511,7 +511,7 @@ AddDive::AddDive(dive *d, bool autogroup, bool newNumber)
allocTrip.reset(trip);
}
int idx = dive_get_insertion_index(divePtr.get());
int idx = dive_get_insertion_index(&dive_table, divePtr.get());
if (newNumber)
divePtr->number = get_dive_nr_at_idx(idx);