| 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 |
// UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 |
|
| 11 |
// Some basic examples of how drop_view might be used in the wild. This is a general |
| 12 |
// collection of sample algorithms and functions that try to mock general usage of |
| 13 |
// this view. |
| 14 |
|
| 15 |
#include <ranges> |
| 16 |
|
| 17 |
#include <vector> |
| 18 |
#include <list> |
| 19 |
#include <string> |
| 20 |
|
| 21 |
#include <cassert> |
| 22 |
#include "test_macros.h" |
| 23 |
#include "test_iterators.h" |
| 24 |
#include "types.h" |
| 25 |
|
| 26 |
template<class T> |
| 27 |
concept ValidDropView = requires { typename std::ranges::drop_view<T>; }; |
| 28 |
|
| 29 |
static_assert( ValidDropView<MoveOnlyView>); |
| 30 |
static_assert(!ValidDropView<Range>); |
| 31 |
|
| 32 |
static_assert(!std::ranges::enable_borrowed_range<std::ranges::drop_view<MoveOnlyView>>); |
| 33 |
static_assert( std::ranges::enable_borrowed_range<std::ranges::drop_view<BorrowableView>>); |
| 34 |
|
| 35 |
template<std::ranges::view View> |
| 36 |
bool orderedFibonacci(View v, int n = 1) { |
| 37 |
if (v.size() < 3) |
| 38 |
return true; |
| 39 |
|
| 40 |
if (v[2] != v[0] + v[1]) |
| 41 |
return false; |
| 42 |
|
| 43 |
return orderedFibonacci(std::ranges::drop_view(v.base(), n), n + 1); |
| 44 |
} |
| 45 |
|
| 46 |
template<std::ranges::view View> |
| 47 |
std::ranges::view auto makeEven(View v) { |
| 48 |
return std::ranges::drop_view(v, v.size() % 2); |
| 49 |
} |
| 50 |
|
| 51 |
template<std::ranges::view View, class T> |
| 52 |
int indexOf(View v, T element) { |
| 53 |
int index = 0; |
| 54 |
for (auto e : v) { |
| 55 |
if (e == element) |
| 56 |
return index; |
| 57 |
index++; |
| 58 |
} |
| 59 |
return -1; |
| 60 |
} |
| 61 |
|
| 62 |
template<std::ranges::view View, class T> |
| 63 |
std::ranges::view auto removeBefore(View v, T element) { |
| 64 |
std::ranges::drop_view out(v, indexOf(v, element) + 1); |
| 65 |
return View(out.begin(), out.end()); |
| 66 |
} |
| 67 |
|
| 68 |
template<> |
| 69 |
constexpr bool std::ranges::enable_view<std::vector<int>> = true; |
| 70 |
|
| 71 |
template<> |
| 72 |
constexpr bool std::ranges::enable_view<std::list<int>> = true; |
| 73 |
|
| 74 |
template<> |
| 75 |
constexpr bool std::ranges::enable_view<std::string> = true; |
| 76 |
|
| 77 |
int main(int, char**) { |
| 78 |
const std::vector vec = {1,1,2,3,5,8,13}; |
| 79 |
assert(orderedFibonacci(std::ranges::drop_view(vec, 0))); |
| 80 |
const std::vector vec2 = {1,1,2,3,5,8,14}; |
| 81 |
assert(!orderedFibonacci(std::ranges::drop_view(vec2, 0))); |
| 82 |
|
| 83 |
const std::list l = {1, 2, 3}; |
| 84 |
auto el = makeEven(l); |
| 85 |
assert(el.size() == 2); |
| 86 |
assert(*el.begin() == 2); |
| 87 |
|
| 88 |
const std::string s = "Hello, World"; |
| 89 |
assert(removeBefore(s, ' ') == "World"); |
| 90 |
|
| 91 |
return 0; |
| 92 |
} |