Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | while ((end - begin) > 8) { | ||||
if (slot < bottom || slot >= top) { | if (slot < bottom || slot >= top) { | ||||
return NO_PEER; | return NO_PEER; | ||||
} | } | ||||
// Guesstimate the position of the slot. | // Guesstimate the position of the slot. | ||||
size_t i = begin + ((slot - bottom) * (end - begin) / (top - bottom)); | size_t i = begin + ((slot - bottom) * (end - begin) / (top - bottom)); | ||||
// We have a match. | // We have a match. | ||||
if (slots[i].start <= slot && slot < slots[i].stop) { | if (slots[i].contains(slot)) { | ||||
return i; | return i; | ||||
} | } | ||||
// We undershooted. | // We undershooted. | ||||
if (slot >= slots[i].stop) { | if (slots[i].precedes(slot)) { | ||||
begin = i + 1; | begin = i + 1; | ||||
bottom = slots[begin].start; | bottom = slots[begin].start; | ||||
continue; | continue; | ||||
} | } | ||||
// We overshooted. | // We overshooted. | ||||
if (slots[i].start > slot) { | if (slots[i].follows(slot)) { | ||||
end = i; | end = i; | ||||
top = slots[end].start; | top = slots[end].start; | ||||
continue; | continue; | ||||
} | } | ||||
// We have an unalocated slot. | // We have an unalocated slot. | ||||
return NO_PEER; | return NO_PEER; | ||||
} | } | ||||
// Enough of that nonsense, let fallback to linear search. | // Enough of that nonsense, let fallback to linear search. | ||||
for (size_t i = begin; i < end; i++) { | for (size_t i = begin; i < end; i++) { | ||||
// We have a match. | // We have a match. | ||||
if (slots[i].start <= slot && slot < slots[i].stop) { | if (slots[i].contains(slot)) { | ||||
return i; | return i; | ||||
} | } | ||||
} | } | ||||
// We failed to find a slot, retry. | // We failed to find a slot, retry. | ||||
return NO_PEER; | return NO_PEER; | ||||
} | } |