[PATCH 2/5] Associate each dive trip with the dives in that trip

Linus Torvalds torvalds at linux-foundation.org
Sun Nov 25 20:27:18 PST 2012


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Sun, 25 Nov 2012 18:53:15 -0800
Subject: [PATCH 2/5] Associate each dive trip with the dives in that trip

We already kept a count of dives per trip in order to figure out when
there are no more dives left and the trip needs to be freed.  Now we
explicitly keep track of the list of dives associated with the trip too,
which simplifies the "find the time of the trip" logic.

We may want to sort it in time, but for now this is mainly about trying
to keep track of the divetrip relationships explicitly.  I want to move
away from the whole "use the gtk tree model to keep track of things"
approach.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

This isn't really important, and I suspect you can skip this patch if you 
want to. But I like the notion of maintaining the full mapping of dive 
trips to dives (and back), and in particular, this makes the dive trip 
date handling code independent of the dive list code (that tie-in caused 
problems in the past)

 dive.h     |  2 ++
 divelist.c | 31 ++++++++++++++++++++++++-------
 2 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/dive.h b/dive.h
index 44023b7054b5..43b7b8d591b0 100644
--- a/dive.h
+++ b/dive.h
@@ -278,6 +278,7 @@ typedef struct dive_trip {
 	timestamp_t when_from_file;
 	char *location;
 	char *notes;
+	struct dive *dives;
 	int nrdives;
 	int expanded:1, selected:1;
 } dive_trip_t;
@@ -286,6 +287,7 @@ struct dive {
 	int number;
 	tripflag_t tripflag;
 	dive_trip_t *divetrip;
+	struct dive *next, **pprev;
 	int selected;
 	gboolean downloaded;
 	int start, end;
diff --git a/divelist.c b/divelist.c
index cc301a8b0247..28c2e8d5326f 100644
--- a/divelist.c
+++ b/divelist.c
@@ -1053,6 +1053,7 @@ static void delete_trip(dive_trip_t *trip)
 {
 	GList *trip_list = find_trip(trip);
 
+	assert(!trip->dives);
 	/*
 	 * The trip may not be on the list, if it had the
 	 * same time as another trip.
@@ -1066,23 +1067,31 @@ static void delete_trip(dive_trip_t *trip)
 
 static void find_new_trip_start_time(dive_trip_t *trip)
 {
-	int i;
-	struct dive *dive;
+	struct dive *dive = trip->dives;
+	timestamp_t when = dive->when;
 
-	for_each_dive(i, dive) {
-		if (dive->divetrip != trip)
-			continue;
-		trip->when = dive->when;
-		break;
+	while ((dive = dive->next) != NULL) {
+		if (dive->when < when)
+			when = dive->when;
 	}
+	trip->when = when;
 }
 
 void remove_dive_from_trip(struct dive *dive)
 {
+	struct dive *next, **pprev;
 	dive_trip_t *trip = dive->divetrip;
 
 	if (!trip)
 		return;
+
+	/* Remove the dive from the trip's list of dives */
+	next = dive->next;
+	pprev = dive->pprev;
+	*pprev = next;
+	if (next)
+		next->pprev = pprev;
+
 	dive->divetrip = NULL;
 	assert(trip->nrdives > 0);
 	if (!--trip->nrdives)
@@ -1099,6 +1108,14 @@ void add_dive_to_trip(struct dive *dive, dive_trip_t *trip)
 	remove_dive_from_trip(dive);
 	trip->nrdives++;
 	dive->divetrip = trip;
+
+	/* Add it to the trip's list of dives*/
+	dive->next = trip->dives;
+	if (dive->next)
+		dive->next->pprev = &dive->next;
+	trip->dives = dive;
+	dive->pprev = &trip->dives;
+
 	if (dive->when && trip->when > dive->when)
 		trip->when = dive->when;
 }
-- 
1.8.0.dirty



More information about the subsurface mailing list