Skip to main content
Commonmark migration
Source Link

Style and other issues

##Style and other issues EqualityEquality comparisons with boolean constants are usually redundant:

##Style and other issues Equality comparisons with boolean constants are usually redundant:

Style and other issues

Equality comparisons with boolean constants are usually redundant:

Bounty Ended with 50 reputation awarded by andrean
added 11 characters in body
Source Link
Toby Speight
  • 81.8k
  • 14
  • 101
  • 309

##Style and other issues Equality comparisons with boolean constants are usually redundant:

            if (sorted_distinct_numbers[i] == true)

can be simply if (sorted_distinct_numbers[i])

gen_random_range() doesn't need an lvalue RndGen, so we can simplify a lot:

template<typename T>
std::vector<T> gen_random_range(std::size_t count, T start, T end) {
    std::vector<T> vec(count);
    std::generate(vec.begin(), vec.end(), RndGen<T>{start, end});
    std::sort(vec.begin(), vec.end());

    return vec;
}

The RndGen doesn't need to store a random device:

template<typename T>
class RndGen {
public:
    RndGen(T start, T end)
        : mersenne_engine{std::random_device{}()},
          dist{start, end}
    {
    }

    T operator()()
    {
        return dist(mersenne_engine);
    }

private:
    std::mt19937 mersenne_engine;
    std::uniform_int_distribution<T> dist;
};

If we make reversed() accept argument by value, that means we don't need to explicitly copy it:

template<typename T>
std::vector<T> reversed(std::vector<T> v) {
    std::reverse(v.begin(), v.end());
    return v;
}

##Style and other issues Equality comparisons with boolean constants are usually redundant:

            if (sorted_distinct_numbers[i] == true)

can be simply if (sorted_distinct_numbers[i])

gen_random_range() doesn't need an lvalue RndGen, so we can simplify a lot:

template<typename T>
std::vector<T> gen_random_range(std::size_t count, T start, T end) {
    std::vector<T> vec(count);
    std::generate(vec.begin(), vec.end(), RndGen<T>{start, end});
    std::sort(vec.begin(), vec.end());

    return vec;
}

The RndGen doesn't need to store a random device:

template<typename T>
class RndGen {
public:
    RndGen(T start, T end)
        : mersenne_engine{std::random_device{}()},
          dist{start, end}
    {
    }

    T operator()()
    {
        return dist(mersenne_engine);
    }

private:
    std::mt19937 mersenne_engine;
    std::uniform_int_distribution<T> dist;
};

If we make reversed() accept argument by value, that means we don't need to explicitly copy it:

template<typename T>
std::vector<T> reversed(std::vector<T> v) {
    std::reverse(v.begin(), v.end());
    return v;
}
added 11 characters in body
Source Link
Toby Speight
  • 81.8k
  • 14
  • 101
  • 309

The functions cheat their benchmarks by allocating their auxiliary storage outside of the timing loop. That's not reasonable unless you can arrange for the "real life" versions to have their own (per-thread?) long-lived auxiliary storage.

The functions cheat their benchmarks by allocating their auxiliary storage outside of the timing loop. That's not reasonable unless you can arrange for the "real life" versions to have their own (per-thread?) auxiliary storage.

The functions cheat their benchmarks by allocating their auxiliary storage outside of the timing loop. That's not reasonable unless you can arrange for the "real life" versions to have their own (per-thread?) long-lived auxiliary storage.

Source Link
Toby Speight
  • 81.8k
  • 14
  • 101
  • 309
Loading