Skip to content
Snippets Groups Projects
Commit 1759eef3 authored by Loic Hausammann's avatar Loic Hausammann
Browse files

Logger: Implement a binary search for the time array

parent 91dcbc32
No related branches found
No related tags found
1 merge request!685Logger loader
...@@ -224,21 +224,31 @@ size_t time_array_get_index(const struct time_array *t, const size_t offset) { ...@@ -224,21 +224,31 @@ size_t time_array_get_index(const struct time_array *t, const size_t offset) {
#ifdef SWIFT_DEBUG_CHECKS #ifdef SWIFT_DEBUG_CHECKS
if (!t) error("NULL pointer."); if (!t) error("NULL pointer.");
if (offset < t->records[0].offset || offset > t->records[t->size].offset) if (offset < t->records[0].offset || offset > t->records[t->size - 1].offset)
error("Offset outside of range."); error("Offset outside of range.");
#endif #endif
/* Find the time_array with the correct offset. */ /* left will contain the index at the end of the loop */
for (size_t i = 1; i < t->size; i++) { size_t left = 0;
if (offset < t->records[i].offset) { size_t right = t->size - 1;
return i - 1;
/* Find the time_array with the correct offset through a bisection method. */
while (left <= right) {
size_t center = (left + right) / 2;
const size_t offset_center = t->records[center].offset;
if (offset > offset_center) {
left = center + 1;
}
else if (offset < offset_center) {
right = center - 1;
}
else {
return center;
} }
else if (offset == t->records[i].offset)
return i;
} }
error("Unable to find the required offset."); return right;
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment