algorithm icon indicating copy to clipboard operation
algorithm copied to clipboard

Any interest to `boost::find_gap` function?

Open denzor200 opened this issue 1 year ago • 3 comments

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..

denzor200 avatar Dec 07 '24 00:12 denzor200

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;
}

denzor200 avatar Dec 07 '24 00:12 denzor200

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

jgopel avatar Dec 09 '24 16:12 jgopel

I concur with @jgopel here; this is basically find_adjacent with a custom predicate. Doesn't means that it couldn't be useful, though.

mclow avatar Dec 09 '24 17:12 mclow