[libcxx-commits] [PATCH] D116268: [libc++][ranges] Add indirectly_comparable concept

Nikolas Klauser via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Fri Dec 24 05:55:30 PST 2021


philnik created this revision.
philnik added reviewers: Quuxplusone, Mordante.
philnik requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Add `indirectly_comparable` concept


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116268

Files:
  libcxx/docs/Status/RangesPaper.csv
  libcxx/include/__iterator/iter_swap.h
  libcxx/test/std/iterators/iterator.requirements/indirectcallable/indirectinvocable/indirectly_comparable.compile.pass.cpp


Index: libcxx/test/std/iterators/iterator.requirements/indirectcallable/indirectinvocable/indirectly_comparable.compile.pass.cpp
===================================================================
--- /dev/null
+++ libcxx/test/std/iterators/iterator.requirements/indirectcallable/indirectinvocable/indirectly_comparable.compile.pass.cpp
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// template<class F, class I1, class I2>
+// concept indirect_binary_predicate;
+
+#include <iterator>
+#include <type_traits>
+
+#include "__iterator/readable_traits.h"
+#include "indirectly_readable.h"
+#include "test_iterators.h"
+
+struct Token1 {
+  int i;
+};
+struct Token2 {
+  int i;
+};
+using It1 = IndirectlyReadable<Token1>;
+using It2 = IndirectlyReadable<Token2>;
+
+struct GetI {
+  template <class IHolder>
+  int&& operator()(IHolder&& holder) const noexcept {
+    return holder.i;
+  }
+};
+
+template <class I1, class I2>
+struct GoodPredicate {
+  bool operator()(std::iter_value_t<I1>&, std::iter_value_t<I2>&) const;
+  bool operator()(std::iter_value_t<I1>&, std::iter_reference_t<I2>) const;
+  bool operator()(std::iter_reference_t<I1>, std::iter_value_t<I2>&) const;
+  bool operator()(std::iter_reference_t<I1>, std::iter_reference_t<I2>) const;
+  bool operator()(std::iter_common_reference_t<I1>, std::iter_common_reference_t<I2>) const;
+};
+
+struct IntPredicate {
+  bool operator()(int, int) const;
+};
+
+struct NotPredicate {};
+struct NotIt {};
+static_assert(std::indirectly_comparable<It1, It2, GoodPredicate<It1, It2>>);
+static_assert(!std::indirectly_comparable<NotIt, It1, GoodPredicate<NotIt, It2>>);
+static_assert(!std::indirectly_comparable<It1, It2, NotPredicate>);
+static_assert(std::indirectly_comparable<It1, It2, IntPredicate, GetI, GetI>);
Index: libcxx/include/__iterator/iter_swap.h
===================================================================
--- libcxx/include/__iterator/iter_swap.h
+++ libcxx/include/__iterator/iter_swap.h
@@ -10,9 +10,11 @@
 #define _LIBCPP___ITERATOR_ITER_SWAP_H
 
 #include <__config>
+#include <__functional/identity.h>
 #include <__iterator/concepts.h>
 #include <__iterator/iter_move.h>
 #include <__iterator/iterator_traits.h>
+#include <__iterator/projected.h>
 #include <__iterator/readable_traits.h>
 #include <__ranges/access.h>
 #include <__utility/forward.h>
@@ -97,6 +99,9 @@
     ranges::iter_swap(__i2, __i1);
   };
 
+template <class _I1, class _I2, class _Rp, class _P1 = identity, class _P2 = identity>
+concept indirectly_comparable = indirect_binary_predicate<_Rp, projected<_I1, _P1>, projected<_I2, _P2>>;
+
 #endif // !defined(_LIBCPP_HAS_NO_RANGES)
 
 _LIBCPP_END_NAMESPACE_STD
Index: libcxx/docs/Status/RangesPaper.csv
===================================================================
--- libcxx/docs/Status/RangesPaper.csv
+++ libcxx/docs/Status/RangesPaper.csv
@@ -63,7 +63,7 @@
 | indirectly_copyable_storable",[iterator.concepts],Zoe Carver,In progress
 [common.alg.req]: pt. 2,indirectly_swappable,"| [iterator.concepts]
 | [iterator.cust.swap]",Zoe Carver,✅
-[common.alg.req]: pt. 3,indirectly_comparable,[projected],Louis Dionne,Not started
+[common.alg.req]: pt. 3,indirectly_comparable,[projected],Louis Dionne,✅
 [common.alg.req]: pt. 4,"| permutable
 | mergeable
 | sortable",[iterator.concepts],Unassigned,Not started


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116268.396155.patch
Type: text/x-patch
Size: 3763 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211224/05fd0d57/attachment.bin>


More information about the libcxx-commits mailing list