IP-Query: Use a vector instead a set and random pick an IP

This commit is contained in:
Jonas Schnelli 2016-05-31 10:40:44 +02:00
parent 839db157f9
commit 5c8b9e392b
No known key found for this signature in database
GPG key ID: 29D4BCB6416F53EC

12
db.cpp
View file

@ -180,10 +180,10 @@ void CAddrDb::GetIPs_(set<CNetAddr>& ips, uint64_t requestedFlags, int max, cons
} }
return; return;
} }
std::set<int> goodIdFiltered; std::vector<int> goodIdFiltered;
for (std::set<int>::const_iterator it = goodId.begin(); it != goodId.end(); it++) { for (std::set<int>::const_iterator it = goodId.begin(); it != goodId.end(); it++) {
if ((idToInfo[*it].services & requestedFlags) == requestedFlags) if ((idToInfo[*it].services & requestedFlags) == requestedFlags)
goodIdFiltered.insert(*it); goodIdFiltered.push_back(*it);
} }
if (!goodIdFiltered.size()) if (!goodIdFiltered.size())
@ -193,14 +193,10 @@ void CAddrDb::GetIPs_(set<CNetAddr>& ips, uint64_t requestedFlags, int max, cons
max = goodIdFiltered.size() / 2; max = goodIdFiltered.size() / 2;
if (max < 1) if (max < 1)
max = 1; max = 1;
int low = *goodIdFiltered.begin();
int high = *goodIdFiltered.rbegin();
set<int> ids; set<int> ids;
while (ids.size() < max) { while (ids.size() < max) {
int range = high-low+1; ids.insert(goodIdFiltered[rand() % goodIdFiltered.size()]);
int pos = low + (rand() % range);
int id = *(goodIdFiltered.lower_bound(pos));
ids.insert(id);
} }
for (set<int>::const_iterator it = ids.begin(); it != ids.end(); it++) { for (set<int>::const_iterator it = ids.begin(); it != ids.end(); it++) {
CService &ip = idToInfo[*it].ip; CService &ip = idToInfo[*it].ip;