Skip to content
Merged
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
31 changes: 19 additions & 12 deletions stl/inc/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -606,26 +606,33 @@ namespace ranges {
_NODISCARD constexpr iter_difference_t<_It> operator()(
_It _First, _Se _Last, const _Ty& _Val, _Pj _Proj = {}) const {
_Adl_verify_range(_First, _Last);
return _Count_unchecked(
_Get_unwrapped(_STD move(_First)), _Get_unwrapped(_STD move(_Last)), _Val, _Pass_fn(_Proj));
}

auto _UFirst = _Get_unwrapped(_STD move(_First));
const auto _ULast = _Get_unwrapped(_STD move(_Last));
iter_difference_t<_It> _Count = 0;
template <input_range _Rng, class _Ty, class _Pj = identity>
requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Rng>, _Pj>, const _Ty*>
_NODISCARD constexpr range_difference_t<_Rng> operator()(_Rng&& _Range, const _Ty& _Val, _Pj _Proj = {}) const {
return _Count_unchecked(_Ubegin(_Range), _Uend(_Range), _Val, _Pass_fn(_Proj));
}
// clang-format on
private:
template <class _It, class _Se, class _Ty, class _Pj>
_NODISCARD static constexpr iter_difference_t<_It> _Count_unchecked(
_It _First, const _Se _Last, const _Ty& _Val, _Pj _Proj) {
_STL_INTERNAL_STATIC_ASSERT(input_iterator<_It>);
_STL_INTERNAL_STATIC_ASSERT(sentinel_for<_Se, _It>);
_STL_INTERNAL_STATIC_ASSERT(indirect_binary_predicate<ranges::equal_to, projected<_It, _Pj>, const _Ty*>);

for (; _UFirst != _ULast; ++_UFirst) {
if (_STD invoke(_Proj, *_UFirst) == _Val) {
iter_difference_t<_It> _Count = 0;
for (; _First != _Last; ++_First) {
if (_STD invoke(_Proj, *_First) == _Val) {
++_Count;
}
}

return _Count;
}

template <input_range _Rng, class _Ty, class _Pj = identity>
requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Rng>, _Pj>, const _Ty*>
_NODISCARD constexpr range_difference_t<_Rng> operator()(_Rng&& _Range, const _Ty& _Val, _Pj _Proj = {}) const {
return (*this)(_RANGES begin(_Range), _RANGES end(_Range), _Val, _Pass_fn(_Proj));
}
// clang-format on
};

inline constexpr _Count_fn count{_Not_quite_object::_Construct_tag{}};
Expand Down
62 changes: 25 additions & 37 deletions tests/std/tests/P0896R4_ranges_alg_count/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,40 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <algorithm>
#include <array>
#include <cassert>
#include <concepts>
#include <ranges>
#include <utility>

#include <range_algorithm_support.hpp>

constexpr void smoke_test() {
using ranges::count;
using P = std::pair<int, int>;
std::array<P, 5> const x = {{{0, 99}, {1, 47}, {2, 99}, {3, 47}, {4, 99}}};
using D = ranges::range_difference_t<basic_borrowed_range<P const>>;

{
// Validate range overload
auto result = count(basic_borrowed_range{x}, 99, get_second);
STATIC_ASSERT(std::same_as<decltype(result), D>);
assert(result == 3);
}
{
// Validate iterator + sentinel overload
basic_borrowed_range wrapped_x{x};
auto result = count(wrapped_x.begin(), wrapped_x.end(), 47, get_second);
STATIC_ASSERT(std::same_as<decltype(result), D>);
assert(result == 2);
}
}

int main() {
STATIC_ASSERT((smoke_test(), true));
smoke_test();
}
using namespace std;
using P = pair<int, int>;

struct instantiator {
template <class In>
static void call(In&& in = {}) {
ranges::range_value_t<In> const value{};
(void) ranges::count(in, value);

struct type {
bool operator==(type const&) const = default;
};
using Projection = type (*)(std::iter_common_reference_t<ranges::iterator_t<In>>);
(void) ranges::count(in, type{}, Projection{});
static constexpr P input[5] = {{0, 99}, {1, 47}, {2, 99}, {3, 47}, {4, 99}};

template <ranges::input_range Read>
static constexpr void call() {
using ranges::count;
{ // Validate iterator + sentinel overload
Read wrapped_input{input};

auto result = count(wrapped_input.begin(), wrapped_input.end(), 47, get_second);
STATIC_ASSERT(same_as<decltype(result), ranges::range_difference_t<Read>>);
assert(result == 2);
}
{ // Validate range overload
Read wrapped_input{input};

auto result = count(wrapped_input, 99, get_second);
STATIC_ASSERT(same_as<decltype(result), ranges::range_difference_t<Read>>);
assert(result == 3);
}
}
};

template void test_in<instantiator, const int>();
int main() {
STATIC_ASSERT((test_in<instantiator, P const>(), true));
test_in<instantiator, P const>();
}