diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2013-02-09 07:12:30 -0800 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-02-09 07:19:29 -0800 |
commit | b9865d6bbc30ff4f82ffa17d4a2e3e74bc1054ed (patch) | |
tree | 0bb5aac0bd1f854ed3ee896b57d4ed73524ffad3 /dive.c | |
parent | c7efcb23a9a838f88c6691cb7b278126ae639c4b (diff) | |
download | subsurface-b9865d6bbc30ff4f82ffa17d4a2e3e74bc1054ed.tar.gz |
Move duration to dive structure and replace accessor function
When starting on this quest to stop using the first divecomputer instead
of data for the whole dive in commit eb73b5a528c8 ("Duration of a dive is
the maximum duration from all divecomputers") I introduced an accessor
function that calculates the dive duration on the fly as the maximum of
the durations in the divecomputers.
Since then Linus and I have added quite a few of the variables back to the
dive data structure and it makes perfect sense to do the same thing for
the duration as well and simply do the calculation once during fixup.
This commit also replaces accesses to the first divecomputer in
likely_same_dive to use the maxdepth and meandepth of the dive (those two
slipped through the cracks in the previous commits, it seems).
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'dive.c')
-rw-r--r-- | dive.c | 35 |
1 files changed, 18 insertions, 17 deletions
@@ -219,17 +219,6 @@ static void update_duration(duration_t *duration, int new) duration->seconds = new; } -int get_duration_in_sec(struct dive *dive) -{ - int duration = 0; - struct divecomputer *dc = &dive->dc; - do { - duration = MAX(duration, dc->duration.seconds); - dc = dc->next; - } while (dc); - return duration; -} - static void update_temperature(temperature_t *temperature, int new) { if (new) { @@ -469,6 +458,17 @@ static void fixup_meandepth(struct dive *dive) dive->meandepth.mm = (sum + nr / 2) / nr; } +static void fixup_duration(struct dive *dive) +{ + struct divecomputer *dc; + int duration = 0; + + for_each_dc(dive, dc) + duration = MAX(duration, dc->duration.seconds); + + dive->duration.seconds = duration; +} + /* * events are stored as a linked list, so the concept of * "consecutive, identical events" is somewhat hard to @@ -661,6 +661,7 @@ struct dive *fixup_dive(struct dive *dive) fixup_water_salinity(dive); fixup_surface_pressure(dive); fixup_meandepth(dive); + fixup_duration(dive); for_each_dc(dive, dc) fixup_dive_dc(dive, dc); @@ -1302,9 +1303,9 @@ static int likely_same_dive(struct dive *a, struct dive *b) * Do some basic sanity testing of the values we * have filled in during 'fixup_dive()' */ - if (!similar(a->dc.maxdepth.mm, b->dc.maxdepth.mm, 1000) || - !similar(a->dc.meandepth.mm, b->dc.meandepth.mm, 1000) || - !similar(get_duration_in_sec(a), get_duration_in_sec(b), 5*60)) + if (!similar(a->maxdepth.mm, b->maxdepth.mm, 1000) || + !similar(a->meandepth.mm, b->meandepth.mm, 1000) || + !similar(a->duration.seconds, b->duration.seconds, 5*60)) return 0; /* See if we can get an exact match on the dive computer */ @@ -1316,7 +1317,7 @@ static int likely_same_dive(struct dive *a, struct dive *b) * Allow a time difference due to dive computer time * setting etc. Check if they overlap. */ - fuzz = MAX(get_duration_in_sec(a), get_duration_in_sec(b)) / 2; + fuzz = MAX(a->duration.seconds, b->duration.seconds) / 2; if (fuzz < 60) fuzz = 60; @@ -1604,7 +1605,7 @@ struct dive *find_dive_including(timestamp_t when) * also we always use the duration from the first divecomputer * could this ever be a problem? */ for_each_dive(i, dive) { - if (dive->when <= when && when <= dive->when + get_duration_in_sec(dive)) + if (dive->when <= when && when <= dive->when + dive->duration.seconds) return dive; } return NULL; @@ -1612,7 +1613,7 @@ struct dive *find_dive_including(timestamp_t when) gboolean dive_within_time_range(struct dive *dive, timestamp_t when, timestamp_t offset) { - return when - offset <= dive->when && dive->when + get_duration_in_sec(dive) <= when + offset; + return when - offset <= dive->when && dive->when + dive->duration.seconds <= when + offset; } /* find the n-th dive that is part of a group of dives within the offset around 'when'. |