[libcxx-commits] [libcxx] 560170f - [libcxx][views] Add drop_view.

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jun 23 10:11:02 PDT 2021


Author: zoecarver
Date: 2021-06-23T10:10:50-07:00
New Revision: 560170fa2de5ed3d8720bf6a88c7652ceb47ff8c

URL: https://github.com/llvm/llvm-project/commit/560170fa2de5ed3d8720bf6a88c7652ceb47ff8c
DIFF: https://github.com/llvm/llvm-project/commit/560170fa2de5ed3d8720bf6a88c7652ceb47ff8c.diff

LOG: [libcxx][views] Add drop_view.

The first view in the libc++ ranges library 🚀

Differential Revision: https://reviews.llvm.org/D102037

Added: 
    libcxx/include/__ranges/drop_view.h
    libcxx/test/std/ranges/range.adaptors/range.drop/base.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.drop/begin.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.drop/ctad.compile.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.drop/ctor.default.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.drop/ctor.view.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.drop/dangling.cache.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.drop/end.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.drop/general.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.drop/size.pass.cpp
    libcxx/test/std/ranges/range.adaptors/range.drop/types.h

Modified: 
    libcxx/include/CMakeLists.txt
    libcxx/include/ranges

Removed: 
    


################################################################################
diff  --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 75e0efc34b2dc..6773ec38289a4 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -141,6 +141,7 @@ set(files
   __ranges/all.h
   __ranges/concepts.h
   __ranges/data.h
+  __ranges/drop_view.h
   __ranges/empty.h
   __ranges/empty_view.h
   __ranges/enable_borrowed_range.h

diff  --git a/libcxx/include/__ranges/drop_view.h b/libcxx/include/__ranges/drop_view.h
new file mode 100644
index 0000000000000..610305709a34c
--- /dev/null
+++ b/libcxx/include/__ranges/drop_view.h
@@ -0,0 +1,154 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+#ifndef _LIBCPP___RANGES_DROP_VIEW_H
+#define _LIBCPP___RANGES_DROP_VIEW_H
+
+#include <__config>
+#include <__iterator/iterator_traits.h>
+#include <__iterator/concepts.h>
+#include <__ranges/access.h>
+#include <__ranges/view_interface.h>
+#include <__ranges/all.h>
+#include <type_traits>
+#include <optional>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if !defined(_LIBCPP_HAS_NO_RANGES)
+
+namespace ranges {
+  template<view _View>
+  class drop_view
+    : public view_interface<drop_view<_View>> {
+
+    // We cache begin() whenever ranges::next is not guaranteed O(1) to provide an
+    // amortized O(1) begin() method. If this is an input_range, then we cannot cache
+    // begin because begin is not equality preserving.
+    // Note: drop_view<input-range>::begin() is still trivially amortized O(1) because
+    // one can't call begin() on it more than once.
+    static constexpr bool _UseCache = forward_range<_View> && !(random_access_range<_View> && sized_range<_View>);
+    using _Cache = optional<iterator_t<_View>>;
+    struct _Empty { };
+
+    // For forward ranges use std::optional to cache the begin iterator.
+    // No unique address + _Empty means we don't use any extra space when this
+    // is not a forward iterator.
+    [[no_unique_address]] conditional_t<_UseCache, _Cache, _Empty> __cached_begin_;
+    range_
diff erence_t<_View> __count_ = 0;
+    _View __base_ = _View();
+
+public:
+    drop_view() requires default_initializable<_View> = default;
+
+    constexpr drop_view(_View __base, range_
diff erence_t<_View> __count)
+      : __cached_begin_()
+      , __count_(__count)
+      , __base_(_VSTD::move(__base))
+    {
+      _LIBCPP_ASSERT(__count_ >= 0, "count must be greater than or equal to zero.");
+    }
+
+    constexpr drop_view(drop_view const& __other)
+      : __cached_begin_() // Intentionally not propagating the cached begin iterator.
+      , __count_(__other.__count_)
+      , __base_(__other.__base_)
+    { }
+
+    constexpr drop_view(drop_view&& __other)
+      : __cached_begin_() // Intentionally not propagating the cached begin iterator.
+      , __count_(_VSTD::move(__other.__count_))
+      , __base_(_VSTD::move(__other.__base_))
+    { }
+
+    constexpr drop_view& operator=(drop_view const& __other) {
+      if constexpr (_UseCache) {
+        __cached_begin_.reset();
+      }
+      __base_ = __other.__base_;
+      __count_ = __other.__count_;
+      return *this;
+    }
+
+    constexpr drop_view& operator=(drop_view&& __other) {
+      if constexpr (_UseCache) {
+        __cached_begin_.reset();
+        __other.__cached_begin_.reset();
+      }
+      __base_ = _VSTD::move(__other.__base_);
+      __count_ = _VSTD::move(__other.__count_);
+      return *this;
+    }
+
+    constexpr _View base() const& requires copy_constructible<_View> { return __base_; }
+    constexpr _View base() && { return _VSTD::move(__base_); }
+
+    constexpr auto begin()
+      requires (!(__simple_view<_View> &&
+                  random_access_range<const _View> && sized_range<const _View>))
+    {
+      if constexpr (_UseCache)
+        if (__cached_begin_)
+          return *__cached_begin_;
+
+      auto __tmp = ranges::next(ranges::begin(__base_), __count_, ranges::end(__base_));
+      if constexpr (_UseCache)
+        __cached_begin_ = __tmp;
+      return __tmp;
+    }
+
+    constexpr auto begin() const
+      requires random_access_range<const _View> && sized_range<const _View>
+    {
+      return ranges::next(ranges::begin(__base_), __count_, ranges::end(__base_));
+    }
+
+    constexpr auto end()
+      requires (!__simple_view<_View>)
+    { return ranges::end(__base_); }
+
+    constexpr auto end() const
+      requires range<const _View>
+    { return ranges::end(__base_); }
+
+    static constexpr auto __size(auto& __self) {
+      const auto __s = ranges::size(__self.__base_);
+      const auto __c = static_cast<decltype(__s)>(__self.__count_);
+      return __s < __c ? 0 : __s - __c;
+    }
+
+    constexpr auto size()
+      requires sized_range<_View>
+    { return __size(*this); }
+
+    constexpr auto size() const
+      requires sized_range<const _View>
+    { return __size(*this); }
+  };
+
+  template<class _Range>
+  drop_view(_Range&&, range_
diff erence_t<_Range>)
+  // TODO: this is just recreating all_t.
+    -> drop_view<decltype(views::all(std::declval<_Range>()))>;
+
+} // namespace ranges
+
+#endif // !defined(_LIBCPP_HAS_NO_RANGES)
+
+_LIBCPP_END_NAMESPACE_STD
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___RANGES_DROP_VIEW_H

diff  --git a/libcxx/include/ranges b/libcxx/include/ranges
index 70d0dd07eb2e2..52c69124a3e1f 100644
--- a/libcxx/include/ranges
+++ b/libcxx/include/ranges
@@ -97,6 +97,7 @@ namespace std::ranges {
 #include <__ranges/all.h>
 #include <__ranges/concepts.h>
 #include <__ranges/data.h>
+#include <__ranges/drop_view.h>
 #include <__ranges/empty.h>
 #include <__ranges/empty_view.h>
 #include <__ranges/enable_borrowed_range.h>

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.drop/base.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.drop/base.pass.cpp
new file mode 100644
index 0000000000000..f485d170a5660
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.drop/base.pass.cpp
@@ -0,0 +1,45 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr V base() const& requires copy_constructible<V> { return base_; }
+// constexpr V base() && { return std::move(base_); }
+
+#include <ranges>
+
+#include "test_macros.h"
+#include "types.h"
+
+constexpr bool test() {
+  std::ranges::drop_view<ContiguousView> dropView1;
+  auto base1 = std::move(dropView1).base();
+  assert(std::ranges::begin(base1) == globalBuff);
+
+  // Note: we should *not* drop two elements here.
+  std::ranges::drop_view<ContiguousView> dropView2(ContiguousView{4}, 2);
+  auto base2 = std::move(dropView2).base();
+  assert(std::ranges::begin(base2) == globalBuff + 4);
+
+  std::ranges::drop_view<CopyableView> dropView3;
+  auto base3 = dropView3.base();
+  assert(std::ranges::begin(base3) == globalBuff);
+  auto base4 = std::move(dropView3).base();
+  assert(std::ranges::begin(base4) == globalBuff);
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.drop/begin.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.drop/begin.pass.cpp
new file mode 100644
index 0000000000000..b19143e76f979
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.drop/begin.pass.cpp
@@ -0,0 +1,73 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr auto begin()
+//   requires (!(simple-view<V> &&
+//               random_access_range<const V> && sized_range<const V>));
+// constexpr auto begin() const
+//   requires random_access_range<const V> && sized_range<const V>;
+
+#include <ranges>
+
+#include "test_macros.h"
+#include "types.h"
+
+template<class T>
+concept BeginInvocable = requires(std::ranges::drop_view<T> t) { t.begin(); };
+
+constexpr bool test() {
+  // random_access_range<const V> && sized_range<const V>
+  std::ranges::drop_view dropView1(ContiguousView(), 4);
+  assert(dropView1.begin() == globalBuff + 4);
+
+  // !random_access_range<const V>
+  std::ranges::drop_view dropView2(ForwardView(), 4);
+  assert(dropView2.begin().base() == globalBuff + 4);
+
+  // !random_access_range<const V>
+  std::ranges::drop_view dropView3(InputView(), 4);
+  assert(dropView3.begin().base() == globalBuff + 4);
+
+  // random_access_range<const V> && sized_range<const V>
+  std::ranges::drop_view dropView4(ContiguousView(), 8);
+  assert(dropView4.begin() == globalBuff + 8);
+
+  // random_access_range<const V> && sized_range<const V>
+  std::ranges::drop_view dropView5(ContiguousView(), 0);
+  assert(dropView5.begin() == globalBuff);
+
+  // random_access_range<const V> && sized_range<const V>
+  const std::ranges::drop_view dropView6(ContiguousView(), 0);
+  assert(dropView6.begin() == globalBuff);
+
+  // random_access_range<const V> && sized_range<const V>
+  std::ranges::drop_view dropView7(ContiguousView(), 10);
+  assert(dropView7.begin() == globalBuff + 8);
+
+  CountedView view8;
+  std::ranges::drop_view dropView8(view8, 5);
+  assert(dropView8.begin().base().base() == globalBuff + 5);
+  assert(dropView8.begin().stride_count() == 5);
+  assert(dropView8.begin().base().base() == globalBuff + 5);
+  assert(dropView8.begin().stride_count() == 5);
+
+  static_assert(!BeginInvocable<const ForwardView>);
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.drop/ctad.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.drop/ctad.compile.pass.cpp
new file mode 100644
index 0000000000000..c22417bf877ab
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.drop/ctad.compile.pass.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// template<class R>
+// drop_view(R&&, range_
diff erence_t<R>) -> drop_view<views::all_t<R>>;
+
+#include <ranges>
+
+#include "test_macros.h"
+#include "types.h"
+
+namespace ranges = std::ranges;
+
+static_assert(std::same_as<decltype(ranges::drop_view(ContiguousView(), 0)), ranges::drop_view<ContiguousView>>);
+static_assert(std::same_as<decltype(ranges::drop_view(CopyableView(), 0)), ranges::drop_view<CopyableView>>);
+static_assert(std::same_as<decltype(ranges::drop_view(ForwardView(), 0)), ranges::drop_view<ForwardView>>);
+static_assert(std::same_as<decltype(ranges::drop_view(InputView(), 0)), ranges::drop_view<InputView>>);
+
+static_assert(std::same_as<decltype(ranges::drop_view(std::declval<ForwardRange&>(), 0)),
+                           ranges::drop_view<ranges::ref_view<ForwardRange>>>);
+
+static_assert(std::same_as<decltype(ranges::drop_view(BorrowableRange(), 0)),
+                           ranges::drop_view<ranges::subrange<int*>>>);

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.drop/ctor.default.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.drop/ctor.default.pass.cpp
new file mode 100644
index 0000000000000..1e41825a4b3ac
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.drop/ctor.default.pass.cpp
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// drop_view() requires default_initializable<V> = default;
+
+#include <ranges>
+
+#include "test_macros.h"
+#include "types.h"
+
+constexpr bool test() {
+  std::ranges::drop_view<ContiguousView> dropView1;
+  assert(std::ranges::begin(dropView1) == globalBuff);
+
+  static_assert( std::is_default_constructible_v<std::ranges::drop_view<ForwardView>>);
+  static_assert(!std::is_default_constructible_v<std::ranges::drop_view<NoDefaultCtorForwardView>>);
+
+  static_assert( std::is_nothrow_default_constructible_v<std::ranges::drop_view<ForwardView>>);
+  static_assert(!std::is_nothrow_default_constructible_v<ThrowingDefaultCtorForwardView>);
+  static_assert(!std::is_nothrow_default_constructible_v<std::ranges::drop_view<ThrowingDefaultCtorForwardView>>);
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.drop/ctor.view.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.drop/ctor.view.pass.cpp
new file mode 100644
index 0000000000000..05cd1b172e84e
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.drop/ctor.view.pass.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr drop_view(V base, range_
diff erence_t<V> count);
+
+#include <ranges>
+
+#include "test_macros.h"
+#include "types.h"
+
+constexpr bool test() {
+  std::ranges::drop_view dropView1(ContiguousView(), 4);
+  assert(dropView1.size() == 4);
+  assert(dropView1.begin() == globalBuff + 4);
+
+  std::ranges::drop_view dropView2(ForwardView(), 4);
+  assert(dropView2.begin().base() == globalBuff + 4);
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.drop/dangling.cache.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.drop/dangling.cache.pass.cpp
new file mode 100644
index 0000000000000..f5ca14d2e15b2
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.drop/dangling.cache.pass.cpp
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// If we have a copy-propagating cache, when we copy ZeroOnDestroy, we will get a
+// dangling reference to the copied-from object. This test ensures that we do not
+// propagate the cache on copy.
+
+#include <ranges>
+
+#include "test_macros.h"
+#include "types.h"
+
+struct ZeroOnDestroy : std::ranges::view_base {
+  unsigned count = 0;
+  int buff[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  constexpr ForwardIter begin() { return ForwardIter(buff); }
+  constexpr ForwardIter begin() const { return ForwardIter(); }
+  constexpr ForwardIter end() { return ForwardIter(buff + 8); }
+  constexpr ForwardIter end() const { return ForwardIter(); }
+
+  ~ZeroOnDestroy() {
+    memset(buff, 0, sizeof(buff));
+  }
+
+  static auto dropFirstFour() {
+    ZeroOnDestroy zod;
+    std::ranges::drop_view dv(zod, 4);
+    // Make sure we call begin here so the next call to begin will
+    // use the cached iterator.
+    assert(*dv.begin() == 5);
+    // Intentionally invoke the copy ctor here.
+    return std::ranges::drop_view(dv);
+  }
+};
+
+int main(int, char**) {
+  auto noDanlingCache = ZeroOnDestroy::dropFirstFour();
+  // If we use the cached version, it will reference the copied-from view.
+  // Worst case this is a segfault, best case it's an assertion fired.
+  assert(*noDanlingCache.begin() == 5);
+
+  return 0;
+}

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.drop/end.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.drop/end.pass.cpp
new file mode 100644
index 0000000000000..b0e5c543bbbf9
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.drop/end.pass.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr auto end()
+//   requires (!simple-view<V>)
+// constexpr auto end() const
+//   requires range<const V>
+
+#include <ranges>
+
+#include "test_macros.h"
+#include "types.h"
+
+constexpr bool test() {
+  // range<const V>
+  std::ranges::drop_view dropView1(ContiguousView(), 4);
+  assert(dropView1.end() == globalBuff + 8);
+
+  // !simple-view<V>
+  std::ranges::drop_view dropView2(InputView(), 4);
+  assert(dropView2.end() == globalBuff + 8);
+
+  // range<const V>
+  const std::ranges::drop_view dropView3(ContiguousView(), 0);
+  assert(dropView3.end() == globalBuff + 8);
+
+  // !simple-view<V>
+  const std::ranges::drop_view dropView4(InputView(), 2);
+  assert(dropView4.end() == globalBuff + 8);
+
+  // range<const V>
+  std::ranges::drop_view dropView5(ContiguousView(), 10);
+  assert(dropView5.end() == globalBuff + 8);
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.drop/general.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.drop/general.pass.cpp
new file mode 100644
index 0000000000000..2d1245a0fe975
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.drop/general.pass.cpp
@@ -0,0 +1,91 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// Some basic examples of how drop_view might be used in the wild. This is a general
+// collection of sample algorithms and functions that try to mock general usage of
+// this view.
+
+#include <ranges>
+
+#include <vector>
+#include <list>
+#include <string>
+
+#include <cassert>
+#include "test_macros.h"
+#include "test_iterators.h"
+#include "types.h"
+
+template<class T>
+concept ValidDropView = requires { typename std::ranges::drop_view<T>; };
+
+static_assert(ValidDropView<ContiguousView>);
+static_assert(!ValidDropView<Range>);
+
+template<std::ranges::view View>
+bool orderedFibonacci(View v, int n = 1) {
+  if (v.size() < 3)
+    return true;
+
+  if (v[2] != v[0] + v[1])
+    return false;
+
+  return orderedFibonacci(std::ranges::drop_view(v.base(), n), n + 1);
+}
+
+template<std::ranges::view View>
+std::ranges::view auto makeEven(View v) {
+  return std::ranges::drop_view(v, v.size() % 2);
+}
+
+template<std::ranges::view View, class T>
+int indexOf(View v, T element) {
+  int index = 0;
+  for (auto e : v) {
+    if (e == element)
+      return index;
+    index++;
+  }
+  return -1;
+}
+
+template<std::ranges::view View, class T>
+std::ranges::view auto removeBefore(View v, T element) {
+  std::ranges::drop_view out(v, indexOf(v, element) + 1);
+  return View(out.begin(), out.end());
+}
+
+template<>
+constexpr bool std::ranges::enable_view<std::vector<int>> = true;
+
+template<>
+constexpr bool std::ranges::enable_view<std::list<int>> = true;
+
+template<>
+constexpr bool std::ranges::enable_view<std::string> = true;
+
+int main(int, char**) {
+  const std::vector vec = {1,1,2,3,5,8,13};
+  assert(orderedFibonacci(std::ranges::drop_view(vec, 0)));
+  const std::vector vec2 = {1,1,2,3,5,8,14};
+  assert(!orderedFibonacci(std::ranges::drop_view(vec2, 0)));
+
+  const std::list l = {1, 2, 3};
+  auto el = makeEven(l);
+  assert(el.size() == 2);
+  assert(*el.begin() == 2);
+
+  const std::string s = "Hello, World";
+  assert(removeBefore(s, ' ') == "World");
+
+  return 0;
+}

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.drop/size.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.drop/size.pass.cpp
new file mode 100644
index 0000000000000..297768afe8c24
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.drop/size.pass.cpp
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+// UNSUPPORTED: libcpp-no-concepts
+// UNSUPPORTED: gcc-10
+
+// constexpr auto size()
+//   requires sized_range<V>
+// constexpr auto size() const
+//   requires sized_range<const V>
+
+#include <ranges>
+
+#include "test_macros.h"
+#include "types.h"
+
+template<class T>
+concept SizeInvocable = requires(std::ranges::drop_view<T> t) { t.size(); };
+
+constexpr bool test() {
+  // sized_range<V>
+  std::ranges::drop_view dropView1(ContiguousView(), 4);
+  assert(dropView1.size() == 4);
+
+  // sized_range<V>
+  std::ranges::drop_view dropView2(ContiguousView(), 0);
+  assert(dropView2.size() == 8);
+
+  // sized_range<const V>
+  const std::ranges::drop_view dropView3(ContiguousView(), 8);
+  assert(dropView3.size() == 0);
+
+  // sized_range<const V>
+  const std::ranges::drop_view dropView4(ContiguousView(), 10);
+  assert(dropView4.size() == 0);
+
+  // Because ForwardView is not a sized_range.
+  static_assert(!SizeInvocable<ForwardView>);
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

diff  --git a/libcxx/test/std/ranges/range.adaptors/range.drop/types.h b/libcxx/test/std/ranges/range.adaptors/range.drop/types.h
new file mode 100644
index 0000000000000..35d91ed4b97b1
--- /dev/null
+++ b/libcxx/test/std/ranges/range.adaptors/range.drop/types.h
@@ -0,0 +1,108 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_DROP_TYPES_H
+#define TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_DROP_TYPES_H
+
+#include "test_macros.h"
+#include "test_iterators.h"
+
+int globalBuff[8];
+
+struct ContiguousView : std::ranges::view_base {
+  int start_;
+  constexpr ContiguousView(int start = 0) : start_(start) {}
+  constexpr ContiguousView(ContiguousView&&) = default;
+  constexpr ContiguousView& operator=(ContiguousView&&) = default;
+  constexpr friend int* begin(ContiguousView& view) { return globalBuff + view.start_; }
+  constexpr friend int* begin(ContiguousView const& view) { return globalBuff + view.start_; }
+  constexpr friend int* end(ContiguousView&) { return globalBuff + 8; }
+  constexpr friend int* end(ContiguousView const&) { return globalBuff + 8; }
+};
+
+struct CopyableView : std::ranges::view_base {
+  int start_;
+  constexpr CopyableView(int start = 0) : start_(start) {}
+  constexpr CopyableView(CopyableView const&) = default;
+  constexpr CopyableView& operator=(CopyableView const&) = default;
+  constexpr friend int* begin(CopyableView& view) { return globalBuff + view.start_; }
+  constexpr friend int* begin(CopyableView const& view) { return globalBuff + view.start_; }
+  constexpr friend int* end(CopyableView&) { return globalBuff + 8; }
+  constexpr friend int* end(CopyableView const&) { return globalBuff + 8; }
+};
+
+using ForwardIter = forward_iterator<int*>;
+struct ForwardView : std::ranges::view_base {
+  constexpr ForwardView() = default;
+  constexpr ForwardView(ForwardView&&) = default;
+  constexpr ForwardView& operator=(ForwardView&&) = default;
+  constexpr friend ForwardIter begin(ForwardView&) { return ForwardIter(globalBuff); }
+  constexpr friend ForwardIter begin(ForwardView const&) { return ForwardIter(globalBuff); }
+  constexpr friend ForwardIter end(ForwardView&) { return ForwardIter(globalBuff + 8); }
+  constexpr friend ForwardIter end(ForwardView const&) { return ForwardIter(globalBuff + 8); }
+};
+
+struct ForwardRange {
+  ForwardIter begin() const;
+  ForwardIter end() const;
+  ForwardIter begin();
+  ForwardIter end();
+};
+
+struct ThrowingDefaultCtorForwardView : std::ranges::view_base {
+  ThrowingDefaultCtorForwardView() noexcept(false);
+  ForwardIter begin() const;
+  ForwardIter end() const;
+  ForwardIter begin();
+  ForwardIter end();
+};
+
+struct NoDefaultCtorForwardView : std::ranges::view_base {
+  NoDefaultCtorForwardView() = delete;
+  ForwardIter begin() const;
+  ForwardIter end() const;
+  ForwardIter begin();
+  ForwardIter end();
+};
+
+struct BorrowableRange {
+  friend int* begin(BorrowableRange const& range);
+  friend int* end(BorrowableRange const&);
+  friend int* begin(BorrowableRange& range);
+  friend int* end(BorrowableRange&);
+};
+
+template<>
+inline constexpr bool std::ranges::enable_borrowed_range<BorrowableRange> = true;
+
+struct InputView : std::ranges::view_base {
+  constexpr cpp20_input_iterator<int*> begin() const { return cpp20_input_iterator<int*>(globalBuff); }
+  constexpr int* end() const { return globalBuff + 8; }
+  constexpr cpp20_input_iterator<int*> begin() { return cpp20_input_iterator<int*>(globalBuff); }
+  constexpr int* end() { return globalBuff + 8; }
+};
+
+constexpr bool operator==(const cpp20_input_iterator<int*> &lhs, int* rhs) { return lhs.base() == rhs; }
+constexpr bool operator==(int* lhs, const cpp20_input_iterator<int*> &rhs) { return rhs.base() == lhs; }
+
+struct Range {
+  friend int* begin(Range const&);
+  friend int* end(Range const&);
+  friend int* begin(Range&);
+  friend int* end(Range&);
+};
+
+using CountedIter = stride_counting_iterator<forward_iterator<int*>>;
+struct CountedView : std::ranges::view_base {
+  constexpr CountedIter begin() { return CountedIter(ForwardIter(globalBuff)); }
+  constexpr CountedIter begin() const { return CountedIter(ForwardIter(globalBuff)); }
+  constexpr CountedIter end() { return CountedIter(ForwardIter(globalBuff + 8)); }
+  constexpr CountedIter end() const { return CountedIter(ForwardIter(globalBuff + 8)); }
+};
+
+#endif // TEST_STD_RANGES_RANGE_ADAPTORS_RANGE_DROP_TYPES_H


        


More information about the libcxx-commits mailing list