[PATCH] Renumber dive list after manual dive merging

Salvador Cuñat salvador.cunat at gmail.com
Sat Jan 9 07:21:08 PST 2016


As Linus pointed out in mail list, user is forced to manually renumber
his dives after doing a merge, unless the merged dives were those at
the list tail.

This patch try to manage the more usual cases, letting the user to deal
with those more complex, based on some assumptions:

1.- We are working on an time ordered list of type:
      dive_table.nr           ... 100 -- 101 -- 102 -- 103 -- 104 ...
      dive_table.dives.number ... 234 -- 235 -- 236 -- 245 -- 246 ...
2.- It's unlikely to merge no consecutive dives, as merging is time
    based.
3.- It's unlikely (although possible) to find consecutive dives with
    no consecutive numbers.
4.- It would be rather bizarre to find that newer dive,of those to
    merge, has lower number than older.
5.- It can be found that one (or both) dives to merge are zero
    numbered.
6.- There is only need to renumber from merged dives in advance.

A variable, "factor", is fixed before reworking the dive table. This
number will be substracted from the original dive number.
If we are in point 5.- case, "factor" will be set to zero, meaning
that dive numbers will not change (if older dive is zero, merged one
will be numbered zero too and will let the user to manage this; if
newer dive is zero there won't be need of renumbering as following
dives will be correctly numbered, e.g. after splitting a dive which
is not at the tail of the table).

In most cases, "factor" *should* be set to 1.

While renumbering it can be found a dive with it's number set to zero,
this won't be changed and will remain zeroed to avoid negative
numbers. It, mostly, means that the user has pending work on his
dives.

I don't know why I've written such a big explanation for such a tiny
patch :-)

Signed-off-by: Salvador Cuñat <salvador.cunat at gmail.com>
---
 subsurface-core/divelist.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/subsurface-core/divelist.c b/subsurface-core/divelist.c
index a2e94c0..7ae5e83 100644
--- a/subsurface-core/divelist.c
+++ b/subsurface-core/divelist.c
@@ -827,7 +827,7 @@ bool consecutive_selected()
 struct dive *merge_two_dives(struct dive *a, struct dive *b)
 {
 	struct dive *res;
-	int i, j;
+	int i, j, factor;
 	int id;
 
 	if (!a || !b)
@@ -843,6 +843,7 @@ struct dive *merge_two_dives(struct dive *a, struct dive *b)
 	if (!res)
 		return NULL;
 
+	factor = (a->number == 0 || b->number == 0) ? 0 : abs(b->number - a->number);
 	add_single_dive(i, res);
 	delete_single_dive(i + 1);
 	delete_single_dive(j);
@@ -850,6 +851,13 @@ struct dive *merge_two_dives(struct dive *a, struct dive *b)
 	// why?
 	// because this way one of the previously selected ids is still around
 	res->id = id;
+
+	// renumber dives from merged one in advance by difference between
+	// merged dives numbers. Do not renumber if actual number is zero.
+	for (j; j < dive_table.nr; j++)
+		if (!dive_table.dives[j]->number == 0)
+			dive_table.dives[j]->number -= factor;
+
 	mark_divelist_changed(true);
 	return res;
 }
-- 
2.7.0.rc3



More information about the subsurface mailing list