| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#ifndef SUPPORT_FROM_RANGE_HELPERS_H |
| 10 |
#define SUPPORT_FROM_RANGE_HELPERS_H |
| 11 |
|
| 12 |
#include <array> |
| 13 |
#include <cstddef> |
| 14 |
#include <iterator> |
| 15 |
#include <type_traits> |
| 16 |
#include <vector> |
| 17 |
|
| 18 |
#include "min_allocator.h" |
| 19 |
#include "test_allocator.h" |
| 20 |
#include "test_iterators.h" |
| 21 |
#include "test_macros.h" |
| 22 |
#include "type_algorithms.h" |
| 23 |
|
| 24 |
struct Empty {}; |
| 25 |
|
| 26 |
template <class T> |
| 27 |
struct InputRange { |
| 28 |
cpp20_input_iterator<T*> begin(); |
| 29 |
sentinel_wrapper<cpp20_input_iterator<T*>> end(); |
| 30 |
}; |
| 31 |
|
| 32 |
template <class Iter, class Sent, std::ranges::input_range Range> |
| 33 |
constexpr auto wrap_input(Range&& input) { |
| 34 |
auto b = Iter(std::ranges::begin(input)); |
| 35 |
auto e = Sent(Iter(std::ranges::end(input))); |
| 36 |
return std::ranges::subrange(std::move(b), std::move(e)); |
| 37 |
} |
| 38 |
|
| 39 |
template <class Iter, class Sent, class T, std::size_t N> |
| 40 |
constexpr auto wrap_input(std::array<T, N>& input) { |
| 41 |
auto b = Iter(input.data()); |
| 42 |
auto e = Sent(Iter(input.data() + input.size())); |
| 43 |
return std::ranges::subrange(std::move(b), std::move(e)); |
| 44 |
} |
| 45 |
|
| 46 |
template <class Iter, class Sent, class T> |
| 47 |
constexpr auto wrap_input(std::vector<T>& input) { |
| 48 |
auto b = Iter(input.data()); |
| 49 |
auto e = Sent(Iter(input.data() + input.size())); |
| 50 |
return std::ranges::subrange(std::move(b), std::move(e)); |
| 51 |
} |
| 52 |
|
| 53 |
struct KeyValue { |
| 54 |
int key; // Only the key is considered for equality comparison. |
| 55 |
char value; // Allows distinguishing equivalent instances. |
| 56 |
|
| 57 |
bool operator<(const KeyValue& other) const { return key < other.key; } |
| 58 |
bool operator==(const KeyValue& other) const { return key == other.key; } |
| 59 |
}; |
| 60 |
|
| 61 |
template <> |
| 62 |
struct std::hash<KeyValue> { |
| 63 |
std::size_t operator()(const KeyValue& kv) const { |
| 64 |
return kv.key; |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
#if !defined(TEST_HAS_NO_EXCEPTIONS) |
| 69 |
|
| 70 |
template <class T> |
| 71 |
struct ThrowingAllocator { |
| 72 |
using value_type = T; |
| 73 |
using char_type = T; |
| 74 |
using is_always_equal = std::false_type; |
| 75 |
|
| 76 |
ThrowingAllocator() = default; |
| 77 |
|
| 78 |
template <class U> |
| 79 |
ThrowingAllocator(const ThrowingAllocator<U>&) {} |
| 80 |
|
| 81 |
T* allocate(std::size_t) { throw 1; } |
| 82 |
void deallocate(T*, std::size_t) {} |
| 83 |
|
| 84 |
template <class U> |
| 85 |
friend bool operator==(const ThrowingAllocator&, const ThrowingAllocator<U>&) { |
| 86 |
return true; |
| 87 |
} |
| 88 |
}; |
| 89 |
#endif |
| 90 |
|
| 91 |
template <class T, class Func> |
| 92 |
constexpr void for_all_iterators_and_allocators(Func f) { |
| 93 |
using Iterators = types::type_list< |
| 94 |
cpp20_input_iterator<T*>, |
| 95 |
forward_iterator<T*>, |
| 96 |
bidirectional_iterator<T*>, |
| 97 |
random_access_iterator<T*>, |
| 98 |
contiguous_iterator<T*>, |
| 99 |
T* |
| 100 |
>; |
| 101 |
|
| 102 |
types::for_each(Iterators{}, [=]<class Iter>() { |
| 103 |
f.template operator()<Iter, sentinel_wrapper<Iter>, std::allocator<T>>(); |
| 104 |
f.template operator()<Iter, sentinel_wrapper<Iter>, test_allocator<T>>(); |
| 105 |
f.template operator()<Iter, sentinel_wrapper<Iter>, min_allocator<T>>(); |
| 106 |
f.template operator()<Iter, sentinel_wrapper<Iter>, safe_allocator<T>>(); |
| 107 |
|
| 108 |
if constexpr (std::sentinel_for<Iter, Iter>) { |
| 109 |
f.template operator()<Iter, Iter, std::allocator<T>>(); |
| 110 |
f.template operator()<Iter, Iter, test_allocator<T>>(); |
| 111 |
f.template operator()<Iter, Iter, min_allocator<T>>(); |
| 112 |
f.template operator()<Iter, Iter, safe_allocator<T>>(); |
| 113 |
} |
| 114 |
}); |
| 115 |
} |
| 116 |
|
| 117 |
#endif // SUPPORT_FROM_RANGE_HELPERS_H |