optimize Subnet::poolOverlaps
Falsely reported as copy paste error by Coverity scan:
bool
Subnet::poolOverlaps(const Lease::Type& pool_type, const PoolPtr& pool) const {
const PoolCollection& pools = getPools(pool_type);
// If no pools, we don't overlap. Nothing to do.
if (pools.empty()) {
return (false);
}
// We're going to insert a new pool, likely between two existing pools.
// So we're going to end up with the following case:
// |<---- pool1 ---->| |<-------- pool2 ------>| |<-- pool3 -->|
// F1 L1 F2 L2 F3 L3
// where pool1 and pool3 are existing pools, pool2 is a pool being
// inserted and "F"/"L" mark first and last address in the pools
// respectively. So the following conditions must be fulfilled:
// F2 > L1 and L2 < F3. Obviously, for any pool: F < L.
// Search for pool3. We use F2 and upper_bound to find the F3 (upper_bound
// returns first pool in the sorted container which first address is
// greater than F2). prefixLessThanPoolAddress with the first argument
// set to "true" is the custom comparison function for upper_bound, which
// compares F2 with the first addresses of the existing pools.
PoolCollection::const_iterator pool3_it =
std::upper_bound(pools.begin(), pools.end(), pool->getFirstAddress(),
prefixLessThanFirstAddress);
// upper_bound returns a first pool which first address is greater than the
// address F2. However, it is also possible that there is a pool which first
// address is equal to F2. Such pool is also in conflict with a new pool.
// If the returned value is pools.begin() it means that all pools have greater
// first address than F2, thus none of the pools can have first address equal
// to F2. Otherwise, we'd need to check them for equality.
if (pool3_it != pools.begin()) {
// Go back one pool and check if addresses are equal.
PoolPtr pool3 = *(pool3_it - 1);
if (pool3->getFirstAddress() == pool->getFirstAddress()) {
return (true);
}
}
// If returned value is unequal pools.end() it means that there is a pool3,
// with F3 > F2.
if (pool3_it != pools.end()) {
// Let's store the pointer to this pool.
PoolPtr pool3 = *pool3_it;
// F3 must be greater than L2, otherwise pools will overlap.
if (pool3->getFirstAddress() <= pool->getLastAddress()) {
return (true);
}
}
// If L2 is ok, we now have to find the pool1. This pool should be
// right before the pool3 if there is any pool before pool3.
if (pool3_it != pools.begin()) {
PoolPtr pool1 = *(pool3_it - 1);
// F2 must be greater than L1.
if (pool->getFirstAddress() <= pool1->getLastAddress()) {
return (true);
}
}
return (false);
}
checking
if (pool3_it != pools.begin()) {
// Go back one pool and check if addresses are equal.
PoolPtr pool3 = *(pool3_it - 1);
if (pool3->getFirstAddress() == pool->getFirstAddress()) {
return (true);
}
}
is useless as we also check it on the third comparison:
if (pool3_it != pools.begin()) {
PoolPtr pool1 = *(pool3_it - 1);
// F2 must be greater than L1.
if (pool->getFirstAddress() <= pool1->getLastAddress()) {
return (true);
}
}
as pool1->getLastAddress() >= pool3->getFirstAddress() == pool1->getFirstAddress() >= pool->getFirstAddress()