- int nb_processes = ref->nb_processes;
- int heap_bytes_used = ref->heap_bytes_used;
-
- int cursor = 0;
- int start = 0;
- int end = count - 1;
- while (start <= end) {
- cursor = (start + end) / 2;
- int nb_processes_test = list[cursor]->nb_processes;
- int heap_bytes_used_test = list[cursor]->heap_bytes_used;
-
- if (nb_processes_test < nb_processes)
- start = cursor + 1;
- else if (nb_processes_test > nb_processes)
- end = cursor - 1;
- else if (heap_bytes_used_test < heap_bytes_used)
- start = cursor + 1;
- else if (heap_bytes_used_test > heap_bytes_used)
- end = cursor - 1;
- else {
- *min = *max = cursor;
- int previous_cursor = cursor - 1;
- while (previous_cursor >= 0) {
- nb_processes_test = list[previous_cursor]->nb_processes;
- heap_bytes_used_test = list[previous_cursor]->heap_bytes_used;
- if (nb_processes_test != nb_processes || heap_bytes_used_test != heap_bytes_used)
- break;
- *min = previous_cursor;
- previous_cursor--;
- }
- size_t next_cursor = cursor + 1;
- while (next_cursor < count) {
- nb_processes_test = list[next_cursor]->nb_processes;
- heap_bytes_used_test = list[next_cursor]->heap_bytes_used;
- if (nb_processes_test != nb_processes || heap_bytes_used_test != heap_bytes_used)
- break;
- *max = next_cursor;
- next_cursor++;
- }
- return -1;
- }
+ auto res = std::equal_range(
+ list, list + count, ref, DerefAndCompareByNbProcessesAndUsedHead());
+
+ // Not found, but we have the insertion point:
+ if (res.first == res.second)
+ return res.first - list;
+
+ // Found, we have the whole matching range:
+ else {
+ *min = res.first - list;
+ *max = (res.second - 1) - list;
+ return -1;