[PATCH 1/2] Prepare to merge non-overlapping dives

Linus Torvalds torvalds at linux-foundation.org
Sun Nov 11 01:39:31 PST 2012


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Sun, 11 Nov 2012 07:20:05 +0100
Subject: [PATCH 1/2] Prepare to merge non-overlapping dives

This just re-organizes the dive merging code so that we expose a new
"merge_dives(a, b, offset)" function that merges two dives together into
one with the samples (and events) of 'b' at the specified offset after
'a'.

We'll want to use this if a dive computer has decided that the dive
ended (due to a pause at the surface), but we really want to just turn
the two computer dives into one long one with an extended surface swim.

No functional changes, but some independent cleanups due to the trip
simplifications.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---
 dive.c | 25 ++++++++++++-------------
 dive.h |  3 ++-
 main.c |  6 +-----
 3 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/dive.c b/dive.c
index ffaed6f871bf..da1dd0952be7 100644
--- a/dive.c
+++ b/dive.c
@@ -809,7 +809,7 @@ static void merge_equipment(struct dive *res, struct dive *a, struct dive *b)
  * The 'next' dive is not involved in the dive merging, but is the dive
  * that will be the next dive after the merged dive.
  */
-static void pick_and_delete_trip(struct dive *res, struct dive *pick, struct dive *remove, struct dive *next)
+static void pick_and_delete_trip(struct dive *res, struct dive *pick, struct dive *remove)
 {
 	tripflag_t tripflag = pick->tripflag;
 	dive_trip_t *trip = pick->divetrip;
@@ -823,7 +823,7 @@ static void pick_and_delete_trip(struct dive *res, struct dive *pick, struct div
 /*
  * Pick a trip for a dive
  */
-static void merge_trip(struct dive *res, struct dive *a, struct dive *b, struct dive *next)
+static void merge_trip(struct dive *res, struct dive *a, struct dive *b)
 {
 	/*
 	 * The larger tripflag is more relevant: we prefer
@@ -858,10 +858,10 @@ static void merge_trip(struct dive *res, struct dive *a, struct dive *b, struct
 	goto pick_b;
 
 pick_a:
-	pick_and_delete_trip(res, a, b, next);
+	pick_and_delete_trip(res, a, b);
 	return;
 pick_b:
-	pick_and_delete_trip(res, b, a, next);
+	pick_and_delete_trip(res, b, a);
 }
 
 /*
@@ -1028,19 +1028,13 @@ static int find_sample_offset(struct dive *a, struct dive *b)
  * merges almost exact duplicates - something that happens easily
  * with overlapping dive downloads.
  */
-struct dive *try_to_merge(struct dive *a, struct dive *b, struct dive *next)
+struct dive *try_to_merge(struct dive *a, struct dive *b)
 {
-	struct dive *res;
 	int offset;
 
 	/*
 	 * This assumes that the clocks on the dive computers are
 	 * roughly synchronized.
-	 *
-	 * We'll probably have to move this into the caller, and
-	 * allow people to override this ("manual merge dives") if
-	 * they have computers that they forgot to change the time
-	 * zone on etc..
 	 */
 	if ((a->when >= b->when + 60) || (a->when <= b->when - 60))
 		return NULL;
@@ -1050,10 +1044,15 @@ struct dive *try_to_merge(struct dive *a, struct dive *b, struct dive *next)
 	if (offset > 120 || offset < -120)
 		return NULL;
 
-	res = alloc_dive();
+	return merge_dives(a, b, offset);
+}
+
+struct dive *merge_dives(struct dive *a, struct dive *b, int offset)
+{
+	struct dive *res = alloc_dive();
 
 	res->when = a->when;
-	merge_trip(res, a, b, next);
+	merge_trip(res, a, b);
 	MERGE_NONZERO(res, a, b, latitude);
 	MERGE_NONZERO(res, a, b, longitude);
 	MERGE_TXT(res, a, b, location);
diff --git a/dive.h b/dive.h
index a8bdf357070a..6431cb1471ce 100644
--- a/dive.h
+++ b/dive.h
@@ -395,7 +395,8 @@ extern void finish_sample(struct dive *dive);
 
 extern void report_dives(gboolean imported);
 extern struct dive *fixup_dive(struct dive *dive);
-extern struct dive *try_to_merge(struct dive *a, struct dive *b, struct dive *next);
+extern struct dive *merge_dives(struct dive *a, struct dive *b, int offset);
+extern struct dive *try_to_merge(struct dive *a, struct dive *b);
 
 extern void renumber_dives(int nr);
 
diff --git a/main.c b/main.c
index ff874834ce15..ca3722383d28 100644
--- a/main.c
+++ b/main.c
@@ -126,16 +126,12 @@ void report_dives(gboolean is_imported)
 		struct dive **pp = &dive_table.dives[i-1];
 		struct dive *prev = pp[0];
 		struct dive *dive = pp[1];
-		struct dive *next;
 		struct dive *merged;
 
 		if (prev->when + prev->duration.seconds < dive->when)
 			continue;
 
-		next = NULL;
-		if (i < dive_table.nr-1)
-			next = pp[2];
-		merged = try_to_merge(prev, dive, next);
+		merged = try_to_merge(prev, dive);
 		if (!merged)
 			continue;
 
-- 
1.8.0



More information about the subsurface mailing list