summaryrefslogtreecommitdiffstats
path: root/core/divelist.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-08-26 14:42:38 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-10-11 16:22:27 -0700
commitf836b9ae97bc8109e1acdd1859bd41a5dc53f7f3 (patch)
treed7e0ce6ca0265ebf01103273384a4594f99e0838 /core/divelist.c
parent0cca36377b01fc10063ecc67f5471ce80d133991 (diff)
downloadsubsurface-f836b9ae97bc8109e1acdd1859bd41a5dc53f7f3.tar.gz
Dive list: unify sorting in core and Qt-model
Ultimately, we want to use a single dive-list and not replicate it in the Qt-model code. To this goal, let's start with using the same sort function. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/divelist.c')
-rw-r--r--core/divelist.c37
1 files changed, 32 insertions, 5 deletions
diff --git a/core/divelist.c b/core/divelist.c
index 72685b40f..0aa77ecac 100644
--- a/core/divelist.c
+++ b/core/divelist.c
@@ -37,6 +37,7 @@
* void mark_divelist_changed(int changed)
* int unsaved_changes()
* void remove_autogen_trips()
+ * bool dive_less_than(const struct dive *a, const struct dive *b)
* void sort_table(struct dive_table *table)
* bool is_trip_before_after(const struct dive *dive, bool before)
<<<<<<< HEAD
@@ -1682,16 +1683,42 @@ void clear_dive_file_data()
saved_git_id = "";
}
-static int sortfn(const void *_a, const void *_b)
+/* This function defines the sort ordering of dives. The core
+ * and the UI models should use the same sort function, which
+ * should be stable. This is not crucial at the moment, as the
+ * indices in core and UI are independent, but ultimately we
+ * probably want to unify the models.
+ * After editing a key used in this sort-function, the order of
+ * the dives must be re-astablished.
+ * Currently, this does a lexicographic sort on the (start-time, id)
+ * tuple. "id" is a stable, strictly increasing unique number, that
+ * is handed out when a dive is added to the system.
+ * We might also consider sorting by end-time and other criteria,
+ * but see the caveat above (editing means rearrangement of the dives).
+ */
+static int comp_dives(const struct dive *a, const struct dive *b)
{
- const struct dive *a = (const struct dive *)*(void **)_a;
- const struct dive *b = (const struct dive *)*(void **)_b;
-
if (a->when < b->when)
return -1;
if (a->when > b->when)
return 1;
- return 0;
+ if (a->id < b->id)
+ return -1;
+ if (a->id > b->id)
+ return 1;
+ return 0; /* this should not happen for a != b */
+}
+
+bool dive_less_than(const struct dive *a, const struct dive *b)
+{
+ return comp_dives(a, b) < 0;
+}
+
+static int sortfn(const void *_a, const void *_b)
+{
+ const struct dive *a = (const struct dive *)*(const void **)_a;
+ const struct dive *b = (const struct dive *)*(const void **)_b;
+ return comp_dives(a, b);
}
void sort_table(struct dive_table *table)