Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ add_benchmark(nth_element src/nth_element.cpp)
add_benchmark(path_lexically_normal src/path_lexically_normal.cpp)
add_benchmark(priority_queue_push_range src/priority_queue_push_range.cpp)
add_benchmark(random_integer_generation src/random_integer_generation.cpp)
add_benchmark(ranges_count_sized_range_unsized_sentinel src/ranges_count_sized_range_unsized_sentinel.cpp)
add_benchmark(ranges_div_ceil src/ranges_div_ceil.cpp)
add_benchmark(regex_match src/regex_match.cpp)
add_benchmark(regex_search src/regex_search.cpp)
Expand Down
97 changes: 97 additions & 0 deletions benchmarks/src/ranges_count_sized_range_unsized_sentinel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <benchmark/benchmark.h>

#include <algorithm>
#include <cstddef>
#include <iterator>
#include <random>
#include <ranges>
#include <vector>

#include "skewed_allocator.hpp"

using namespace std;

using input_vector = vector<int, not_highly_aligned_allocator<int>>;

// no operator-; so this unsized sentinel can not be subtracted
struct unsized_sentinel {
const int* last;

friend bool operator==(const int* current, const unsized_sentinel sentinel) noexcept {
return current == sentinel.last;
}
};

// this range knows its length and can jump to any elements, but ranges::count can not get the distance to the end
// since the sentinel has no operator-
class sized_range_unsized_sentinel {
public:
sized_range_unsized_sentinel(const int* const first, const size_t count) noexcept : first_{first}, count_{count} {}

const int* begin() const noexcept {
return first_;
}

unsized_sentinel end() const noexcept {
return {first_ + count_};
}

size_t size() const noexcept {
return count_;
}

private:
const int* first_;
size_t count_;
};

static_assert(ranges::random_access_range<sized_range_unsized_sentinel>);
static_assert(ranges::sized_range<sized_range_unsized_sentinel>);
static_assert(!ranges::common_range<sized_range_unsized_sentinel>);
static_assert(!sized_sentinel_for<unsized_sentinel, const int*>);

input_vector make_input(const size_t size) {
input_vector input(size);
mt19937 gen(115'116'108);
uniform_int_distribution<int> dist(0, 7);

for (auto& value : input) {
value = dist(gen);
}

return input;
}

void count_vector(benchmark::State& state) {
const auto input = make_input(static_cast<size_t>(state.range(0)));

for (auto _ : state) {
benchmark::DoNotOptimize(input.data());
benchmark::DoNotOptimize(ranges::count(input, 1));
}
}

void count_sized_range_unsized_sentinel(benchmark::State& state) {
const auto size = static_cast<size_t>(state.range(0));
const auto input = make_input(size);

// make it so only the range type differs from count_vector
const sized_range_unsized_sentinel source{input.data(), size};

for (auto _ : state) {
benchmark::DoNotOptimize(input.data());
benchmark::DoNotOptimize(ranges::count(source, 1));
}
}

void common_args(benchmark::Benchmark* const bm) {
bm->Arg(7)->Arg(63)->Arg(512)->Arg(8021)->Arg(1 << 16)->Arg(1 << 20);
}

BENCHMARK(count_vector)->Apply(common_args);
BENCHMARK(count_sized_range_unsized_sentinel)->Apply(common_args);

BENCHMARK_MAIN();
9 changes: 8 additions & 1 deletion stl/inc/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,14 @@ namespace ranges {
requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Rng>, _Pj>, const _Ty*>
_NODISCARD static constexpr range_difference_t<_Rng> operator()(
_Rng&& _Range, const _Ty& _Val, _Pj _Proj = {}) {
return _Count_unchecked(_Ubegin(_Range), _Uend(_Range), _Val, _STD _Pass_fn(_Proj));
auto _Ufirst = _Ubegin(_Range);
if constexpr (sized_range<_Rng> && random_access_iterator<decltype(_Ufirst)>
&& !sized_sentinel_for<decltype(_Uend(_Range)), decltype(_Ufirst)>) {
const auto _Ulast = _Ufirst + _RANGES distance(_Range);
return _Count_unchecked(_Ufirst, _Ulast, _Val, _STD _Pass_fn(_Proj));
} else {
return _Count_unchecked(_STD move(_Ufirst), _Uend(_Range), _Val, _STD _Pass_fn(_Proj));
}
}

private:
Expand Down
74 changes: 74 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_count/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
#include <algorithm>
#include <cassert>
#include <concepts>
#include <cstddef>
#include <iterator>
#include <ranges>
#include <utility>
#include <vector>

#include <range_algorithm_support.hpp>

Expand Down Expand Up @@ -35,7 +38,78 @@ struct instantiator {
}
};

// LWG-4490: count computes the end from the size instead of walking to the sentinel for sized random access ranges
struct counting_sentinel {
const int* last;
int* comparisons;

friend constexpr bool operator==(const int* const current, const counting_sentinel sentinel) {
++*sentinel.comparisons;
return current == sentinel.last;
}
};

class sized_range_unsized_sentinel {
public:
constexpr sized_range_unsized_sentinel(const int* const first, const size_t count, int* const comparisons)
: first_{first}, count_{count}, comparisons_{comparisons} {}

constexpr const int* begin() const {
return first_;
}

constexpr counting_sentinel end() const {
return {first_ + count_, comparisons_};
}

constexpr size_t size() const {
return count_;
}

private:
const int* first_;
size_t count_;
int* comparisons_;
};

static_assert(ranges::random_access_range<sized_range_unsized_sentinel>);
static_assert(ranges::sized_range<sized_range_unsized_sentinel>);
static_assert(!ranges::common_range<sized_range_unsized_sentinel>);
static_assert(!sized_sentinel_for<counting_sentinel, const int*>);

constexpr bool test_sized_range_unsized_sentinel() {
const int input[] = {1, 0, 1, 2, 1, 0, 3};
int comparisons = 0;

assert(ranges::count(sized_range_unsized_sentinel{input, 7, &comparisons}, 1) == 3);
assert(ranges::count(sized_range_unsized_sentinel{input, 7, &comparisons}, 9) == 0);
assert(ranges::count(sized_range_unsized_sentinel{input, 0, &comparisons}, 1) == 0);
assert(
ranges::count(sized_range_unsized_sentinel{input, 7, &comparisons}, 1, [](const int x) { return x + 1; }) == 2);
assert(comparisons == 0);

return true;
}

void test_sized_range_unsized_sentinel_vectorized() {
vector<int> input(200);
for (size_t i = 0; i != input.size(); ++i) {
input[i] = static_cast<int>(i % 3 == 0);
}

int comparisons = 0;
for (size_t length = 0; length != input.size(); ++length) {
const auto expected = static_cast<ptrdiff_t>((length + 2) / 3);
assert(ranges::count(sized_range_unsized_sentinel{input.data(), length, &comparisons}, 1) == expected);
}
assert(comparisons == 0);
}

int main() {
static_assert((test_in<instantiator, P const>(), true));
test_in<instantiator, P const>();

static_assert(test_sized_range_unsized_sentinel());
assert(test_sized_range_unsized_sentinel());
test_sized_range_unsized_sentinel_vectorized();
}