algorithm
algorithm copied to clipboard
Any interest to `boost::find_gap` function?
std::vector<int> nums = {6,7,8,10,11}; // lost 9
boost::find_gap(nums, 6); // will point to value 10 at index 3
This function is useful for validating sorted data that are expected to be consecutive integers, for example - packet ids that might be lost, unix timestamps in a video archive that might be sparsed, etc..
O(n) in time and constant space
template<typename It>
It find_gap(It it, It end, int i) {
for (; it!=end; ++it) {
if (*it != i++) {
return it;
}
}
return end;
}
A decision here would be up to @mclow I belive - I just have a few comments out of curiosity. What is the purpose of the parameter i? Just a start value? Why not infer that from the start of the range or maybe have it be optionally inferred?
Also, I think this would be good to implement in terms of other algorithms. Fundamentally this is a 2-wide sliding window problem. When structured as-such, it pretty elegantly fits itself into std::adjacent_find (or the boost equivalent). Impl here: https://godbolt.org/z/4PxMT58W1
I concur with @jgopel here; this is basically find_adjacent with a custom predicate.
Doesn't means that it couldn't be useful, though.