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