Skip to content

quickmerge.cpp simplification; function merge #7

@anadon

Description

@anadon

reported blurb as follows:

also in quickmerge.cpp, there's a lot of code repetition between sortDoubleSizeTPairHighToLow and sortDoubleSizeTPairLowToHigh, as might be expected from their function names.
this can actually be avoided with templating and some appropriately designed helper functions.
while also generalizing for more/any pair types.

here's an implementation of the inverse problem.

bool is_sorted(const std::vector<T>& v, bool (*compare)(const T, const T)) {
    size_t len = v.size();
    if(len < 2) return true;
    len--;
    for(size_t i = 0; i < len; ++i) {
        if(!compare(v[i],v[i+1])) return false;
    }
    return true;
}

template <typename T>
constexpr bool is_LE(const T x, const T y) {
    return (x<=y);
}

template <typename T>
constexpr bool is_GE(const T x, const T y) {
    return (x>=y);
}
// consider: is_sorted(vec, is_LE)

(g++ does not really like just letting you use operator<= there, sadly.)
(but constexpr and const T for small types should allow the compiler to essentially eliminate any resulting comparison overhead directly anyways.)

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions