Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | size_t selectPeerImpl(const std::vector<Slot> &slots, const uint64_t slot, | ||||
while ((end - begin) > 8) { | while ((end - begin) > 8) { | ||||
// The slot we picked in not allocated. | // The slot we picked in not allocated. | ||||
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)); | ||||
assert(begin <= i && i < end); | |||||
// We have a match. | // We have a match. | ||||
if (slots[i].contains(slot)) { | if (slots[i].contains(slot)) { | ||||
return i; | return i; | ||||
} | } | ||||
// We undershooted. | // We undershooted. | ||||
if (slots[i].precedes(slot)) { | if (slots[i].precedes(slot)) { | ||||
begin = i + 1; | begin = i + 1; | ||||
if (begin >= end) { | |||||
return NO_PEER; | |||||
} | |||||
bottom = slots[begin].getStart(); | bottom = slots[begin].getStart(); | ||||
continue; | continue; | ||||
} | } | ||||
// We overshooted. | // We overshooted. | ||||
if (slots[i].follows(slot)) { | if (slots[i].follows(slot)) { | ||||
end = i; | end = i; | ||||
top = slots[end].getStart(); | top = slots[end].getStart(); | ||||
Show All 18 Lines |