Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any interest to boost::find_gap function? #122

Open
denzor200 opened this issue Dec 7, 2024 · 3 comments
Open

Any interest to boost::find_gap function? #122

denzor200 opened this issue Dec 7, 2024 · 3 comments

Comments

@denzor200
Copy link
Contributor

denzor200 commented Dec 7, 2024

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
Copy link
Contributor Author

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

@jgopel
Copy link
Contributor

jgopel commented Dec 9, 2024

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

@mclow
Copy link
Collaborator

mclow commented Dec 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants