1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// <algorithm>
// template<permutable I, sentinel_for<I> S, class Proj = identity,
// indirect_equivalence_relation<projected<I, Proj>> C = ranges::equal_to>
// constexpr subrange<I> unique(I first, S last, C comp = {}, Proj proj = {}); // Since C++20
//
// template<forward_range R, class Proj = identity,
// indirect_equivalence_relation<projected<iterator_t<R>, Proj>> C = ranges::equal_to>
// requires permutable<iterator_t<R>>
// constexpr borrowed_subrange_t<R>
// unique(R&& r, C comp = {}, Proj proj = {}); // Since C++20
#include <algorithm>
#include <array>
#include <concepts>
#include <functional>
#include <ranges>
#include "almost_satisfies_types.h"
#include "counting_predicates.h"
#include "counting_projection.h"
#include "test_iterators.h"
template <class Iter = int*, class Sent = int*, class Comp = std::ranges::equal_to, class Proj = std::identity>
concept HasUniqueIter =
requires(Iter&& iter, Sent&& sent, Comp&& comp, Proj&& proj) {
std::ranges::unique(
std::forward<Iter>(iter), std::forward<Sent>(sent), std::forward<Comp>(comp), std::forward<Proj>(proj));
};
static_assert(HasUniqueIter<int*, int*>);
// !permutable<I>
static_assert(!HasUniqueIter<PermutableNotForwardIterator>);
static_assert(!HasUniqueIter<PermutableNotSwappable>);
// !sentinel_for<S, I>
static_assert(!HasUniqueIter<int*, SentinelForNotSemiregular>);
// !indirect_equivalence_relation<Comp, projected<I, Proj>>
static_assert(!HasUniqueIter<int*, int*, ComparatorNotCopyable<int>>);
template <class Range, class Comp = std::ranges::equal_to, class Proj = std::identity>
concept HasUniqueRange =
requires(Range&& range, Comp&& comp, Proj&& proj) {
std::ranges::unique(std::forward<Range>(range), std::forward<Comp>(comp), std::forward<Proj>(proj));
};
template <class T>
using R = UncheckedRange<T>;
static_assert(HasUniqueRange<R<int*>>);
// !forward_range<R>
static_assert(!HasUniqueRange<ForwardRangeNotDerivedFrom>);
static_assert(!HasUniqueRange<ForwardRangeNotIncrementable>);
// permutable<ranges::iterator_t<R>>
static_assert(!HasUniqueRange<R<PermutableNotForwardIterator>>);
static_assert(!HasUniqueRange<R<PermutableNotSwappable>>);
// !indirect_equivalence_relation<Comp, projected<ranges::iterator_t<R>, Proj>>
static_assert(!HasUniqueRange<R<int*>, ComparatorNotCopyable<int>>);
template <class Iter, template <class> class SentWrapper, std::size_t N1, std::size_t N2>
constexpr void testUniqueImpl(std::array<int, N1> input, std::array<int, N2> expected) {
using Sent = SentWrapper<Iter>;
// iterator overload
{
auto in = input;
std::same_as<std::ranges::subrange<Iter>> decltype(auto) result =
std::ranges::unique(Iter{in.data()}, Sent{Iter{in.data() + in.size()}});
assert(std::ranges::equal(std::ranges::subrange<Iter>{Iter{in.data()}, result.begin()}, expected));
assert(base(result.end()) == in.data() + in.size());
}
// range overload
{
auto in = input;
std::ranges::subrange r{Iter{in.data()}, Sent{Iter{in.data() + in.size()}}};
std::same_as<std::ranges::subrange<Iter>> decltype(auto) result = std::ranges::unique(r);
assert(std::ranges::equal(std::ranges::subrange<Iter>{Iter{in.data()}, result.begin()}, expected));
assert(base(result.end()) == in.data() + in.size());
}
}
template <class Iter, template <class> class SentWrapper>
constexpr void testImpl() {
// no consecutive elements
{
std::array in{1, 2, 3, 2, 1};
std::array expected{1, 2, 3, 2, 1};
testUniqueImpl<Iter, SentWrapper>(in, expected);
}
// one group of consecutive elements
{
std::array in{2, 3, 3, 3, 4, 3};
std::array expected{2, 3, 4, 3};
testUniqueImpl<Iter, SentWrapper>(in, expected);
}
// multiple groups of consecutive elements
{
std::array in{2, 3, 3, 3, 4, 3, 3, 5, 5, 5};
std::array expected{2, 3, 4, 3, 5};
testUniqueImpl<Iter, SentWrapper>(in, expected);
}
// all the same
{
std::array in{1, 1, 1, 1, 1, 1};
std::array expected{1};
testUniqueImpl<Iter, SentWrapper>(in, expected);
}
// empty range
{
std::array<int, 0> in{};
std::array<int, 0> expected{};
testUniqueImpl<Iter, SentWrapper>(in, expected);
}
// single element range
std::array in{1};
std::array expected{1};
testUniqueImpl<Iter, SentWrapper>(in, expected);
}
template <template <class> class SentWrapper>
constexpr void withAllPermutationsOfIter() {
testImpl<forward_iterator<int*>, SentWrapper>();
testImpl<bidirectional_iterator<int*>, SentWrapper>();
testImpl<random_access_iterator<int*>, SentWrapper>();
testImpl<contiguous_iterator<int*>, SentWrapper>();
testImpl<int*, SentWrapper>();
}
constexpr bool test() {
withAllPermutationsOfIter<std::type_identity_t>();
withAllPermutationsOfIter<sentinel_wrapper>();
struct Data {
int data;
};
// Test custom comparator
{
std::array input{Data{4}, Data{8}, Data{8}, Data{8}};
std::array expected{Data{4}, Data{8}};
const auto comp = [](const Data& x, const Data& y) { return x.data == y.data; };
// iterator overload
{
auto in = input;
auto result = std::ranges::unique(in.begin(), in.end(), comp);
assert(std::ranges::equal(in.begin(), result.begin(), expected.begin(), expected.end(), comp));
assert(base(result.end()) == in.end());
}
// range overload
{
auto in = input;
auto result = std::ranges::unique(in, comp);
assert(std::ranges::equal(in.begin(), result.begin(), expected.begin(), expected.end(), comp));
assert(base(result.end()) == in.end());
}
}
// Test custom projection
{
std::array input{Data{4}, Data{8}, Data{8}, Data{8}};
std::array expected{Data{4}, Data{8}};
const auto proj = &Data::data;
// iterator overload
{
auto in = input;
auto result = std::ranges::unique(in.begin(), in.end(), {}, proj);
assert(std::ranges::equal(in.begin(), result.begin(), expected.begin(), expected.end(), {}, proj, proj));
assert(base(result.end()) == in.end());
}
// range overload
{
auto in = input;
auto result = std::ranges::unique(in, {}, proj);
assert(std::ranges::equal(in.begin(), result.begin(), expected.begin(), expected.end(), {}, proj, proj));
assert(base(result.end()) == in.end());
}
}
// Complexity: For nonempty ranges, exactly (last - first) - 1 applications of the corresponding predicate
// and no more than twice as many applications of any projection.
{
std::array input{1, 2, 3, 3, 3, 4, 3, 3, 5, 5, 6, 6, 1};
std::array expected{1, 2, 3, 4, 3, 5, 6, 1};
// iterator overload
{
auto in = input;
int numberOfComp = 0;
int numberOfProj = 0;
auto result = std::ranges::unique(
in.begin(),
in.end(),
counting_predicate{std::ranges::equal_to{}, numberOfComp},
counting_projection{numberOfProj});
assert(std::ranges::equal(in.begin(), result.begin(), expected.begin(), expected.end()));
assert(base(result.end()) == in.end());
assert(numberOfComp == in.size() - 1);
assert(numberOfProj <= static_cast<int>(2 * (in.size() - 1)));
}
// range overload
{
auto in = input;
int numberOfComp = 0;
int numberOfProj = 0;
auto result = std::ranges::unique(
in, counting_predicate{std::ranges::equal_to{}, numberOfComp}, counting_projection{numberOfProj});
assert(std::ranges::equal(in.begin(), result.begin(), expected.begin(), expected.end()));
assert(base(result.end()) == in.end());
assert(numberOfComp == in.size() - 1);
assert(numberOfProj <= static_cast<int>(2 * (in.size() - 1)));
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|