summaryrefslogtreecommitdiffstats
path: root/core/divelist.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-11-09 16:28:54 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-11-18 16:50:09 -0800
commit9d3a5fa99769d4381f622b97f7776f747a7e562e (patch)
treeb2c002a6d57b33b1e9361f33ad6ec8c5fca01e04 /core/divelist.c
parent6b283e598a3a08c6133d66b5d5617370296e7d0e (diff)
downloadsubsurface-9d3a5fa99769d4381f622b97f7776f747a7e562e.tar.gz
Dive list: implement trip_less_than function
As a step towards proper sorting, introduce a trip_less_than() function in core. It simply sorts by the first dive, which should be unique as dives may belong to only one trip. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/divelist.c')
-rw-r--r--core/divelist.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/core/divelist.c b/core/divelist.c
index 48b54ea66..f9010b958 100644
--- a/core/divelist.c
+++ b/core/divelist.c
@@ -39,6 +39,7 @@
* int unsaved_changes()
* void remove_autogen_trips()
* bool dive_less_than(const struct dive *a, const struct dive *b)
+ * bool trip_less_than(const struct dive_trip *a, const struct dive_trip *b)
* void sort_table(struct dive_table *table)
* bool is_trip_before_after(const struct dive *dive, bool before)
* void delete_dive_from_table(struct dive_table *table, int idx)
@@ -758,7 +759,7 @@ void insert_trip(dive_trip_t *dive_trip)
dive_trip_t *trip;
/* Walk the dive trip list looking for the right location.. */
- while ((trip = *p) != NULL && trip->when < dive_trip->when)
+ while ((trip = *p) != NULL && trip_less_than(trip, dive_trip))
p = &trip->next;
dive_trip->next = trip;
@@ -1741,6 +1742,25 @@ bool dive_less_than(const struct dive *a, const struct dive *b)
return comp_dives(a, b) < 0;
}
+/* Trips are compared according to the first dive in the trip.
+ * Even though it shouldn't happen, take care about "empty" trips.
+ * Since a dive can only belong to one trip, no two trips should
+ * compare as equal
+ */
+static int comp_trips(const struct dive_trip *a, const struct dive_trip *b)
+{
+ if (a->dives.nr <= 0)
+ return b->dives.nr <= 0 ? 0 : -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;
+}
+
static int sortfn(const void *_a, const void *_b)
{
const struct dive *a = (const struct dive *)*(const void **)_a;