mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Dive information: fix surface interval calculation
The old surface interval calculation had fundamental issues: 1) process_all_dives(), which calculates the statistics over *all* dives was used to get the pointer to the previous dive. 2) If two dives in the table had the same time, one of those would have been considered the "previous" dive. 3) If the dive, for which the surface interval is calculated is not yet in the table, no previous dive would be determined. Fix all this by creating a get_surface_interval() function and removing the "get previous dive" functionality of process_all_dives(). Remove the process_all_dives() call from TabDiveInformation::updateData(). Reported-by: Jan Mulder <jlmulder@xs4all.nl> Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
cec0b70365
commit
c32e71e64d
7 changed files with 38 additions and 23 deletions
|
@ -1420,3 +1420,32 @@ void sort_table(struct dive_table *table)
|
|||
{
|
||||
qsort(table->dives, table->nr, sizeof(struct dive *), sortfn);
|
||||
}
|
||||
|
||||
/*
|
||||
* Calculate surface interval for dive starting at "when". Currently, we
|
||||
* might display dives which are not yet in the divelist, therefore the
|
||||
* input parameter is a timestamp.
|
||||
* If the given dive starts during a different dive, the surface interval
|
||||
* is 0. If we can't determine a surface interval (first dive), <0 is
|
||||
* returned. This does *not* consider pathological cases such as dives
|
||||
* that happened inside other dives. The interval will always be calculated
|
||||
* with respect to the dive that started previously.
|
||||
*/
|
||||
timestamp_t get_surface_interval(timestamp_t when)
|
||||
{
|
||||
int i;
|
||||
timestamp_t prev_end;
|
||||
|
||||
/* find previous dive. might want to use a binary search. */
|
||||
for (i = dive_table.nr - 1; i >= 0; --i) {
|
||||
if (dive_table.dives[i]->when < when)
|
||||
break;
|
||||
}
|
||||
if (i < 0)
|
||||
return -1;
|
||||
|
||||
prev_end = dive_endtime(dive_table.dives[i]);
|
||||
if (prev_end > when)
|
||||
return 0;
|
||||
return when - prev_end;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue