diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-11-08 14:02:36 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-11-18 16:50:09 -0800 |
commit | bc7afebc23477ef23f2b9741bf45b2180cbafe15 (patch) | |
tree | fbb52713f0d9b74f6db70b50ead1fa781a61f7e4 | |
parent | 0618aa737fde54e634d16273ec0246ced9c454aa (diff) | |
download | subsurface-bc7afebc23477ef23f2b9741bf45b2180cbafe15.tar.gz |
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>
-rw-r--r-- | core/dive.h | 2 | ||||
-rw-r--r-- | core/divelist.c | 41 | ||||
-rw-r--r-- | core/divelist.h | 5 | ||||
-rw-r--r-- | desktop-widgets/command_divelist.cpp | 2 |
4 files changed, 30 insertions, 20 deletions
diff --git a/core/dive.h b/core/dive.h index aa37eeb46..07223322f 100644 --- a/core/dive.h +++ b/core/dive.h @@ -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); diff --git a/core/divelist.c b/core/divelist.c index 52c724429..986257c4f 100644 --- a/core/divelist.c +++ b/core/divelist.c @@ -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; diff --git a/core/divelist.h b/core/divelist.h index 94774616e..f98587cff 100644 --- a/core/divelist.h +++ b/core/divelist.h @@ -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); diff --git a/desktop-widgets/command_divelist.cpp b/desktop-widgets/command_divelist.cpp index 9f4ea8cd2..3d1aae2e7 100644 --- a/desktop-widgets/command_divelist.cpp +++ b/desktop-widgets/command_divelist.cpp @@ -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); |