[libc-commits] [libc] [libc] Add reverse_iterator comparisons (#86147) (PR #86188)
Guillaume Chatelet via libc-commits
libc-commits at lists.llvm.org
Thu Mar 21 12:49:34 PDT 2024
https://github.com/gchatelet created https://github.com/llvm/llvm-project/pull/86188
https://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmp
>From 0c8f11007f571cc39846938dc65b282d083e7501 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Thu, 21 Mar 2024 20:33:05 +0100
Subject: [PATCH 1/2] [libc] Add reverse_iterator comparisons (#86147)
https://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmp
---
libc/src/__support/CPP/iterator.h | 35 +++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/libc/src/__support/CPP/iterator.h b/libc/src/__support/CPP/iterator.h
index c5bfb1912c7b74..4d06e181bcf064 100644
--- a/libc/src/__support/CPP/iterator.h
+++ b/libc/src/__support/CPP/iterator.h
@@ -20,6 +20,7 @@ namespace cpp {
template <typename T> struct iterator_traits;
template <typename T> struct iterator_traits<T *> {
using reference = T &;
+ using value_type = T;
};
template <typename Iter> class reverse_iterator {
@@ -27,6 +28,8 @@ template <typename Iter> class reverse_iterator {
public:
using reference = typename iterator_traits<Iter>::reference;
+ using value_type = typename iterator_traits<Iter>::value_type;
+ using iterator_type = Iter;
LIBC_INLINE reverse_iterator() : current() {}
LIBC_INLINE constexpr explicit reverse_iterator(Iter it) : current(it) {}
@@ -38,6 +41,38 @@ template <typename Iter> class reverse_iterator {
LIBC_INLINE constexpr explicit reverse_iterator(const Other &it)
: current(it) {}
+ LIBC_INLINE friend constexpr bool operator==(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() == rhs.base();
+ }
+
+ LIBC_INLINE friend constexpr bool operator!=(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() != rhs.base();
+ }
+
+ LIBC_INLINE friend constexpr bool operator<(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() > rhs.base();
+ }
+
+ LIBC_INLINE friend constexpr bool operator<=(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() >= rhs.base();
+ }
+
+ LIBC_INLINE friend constexpr bool operator>(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() < rhs.base();
+ }
+
+ LIBC_INLINE friend constexpr bool operator>=(const reverse_iterator &lhs,
+ const reverse_iterator &rhs) {
+ return lhs.base() <= rhs.base();
+ }
+
+ LIBC_INLINE constexpr iterator_type base() const { current; }
+
LIBC_INLINE constexpr reference operator*() const {
Iter tmp = current;
return *--tmp;
>From 8cfdd0e96a15de205bea4f3fc2a1b78bc64e2274 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Thu, 21 Mar 2024 19:48:20 +0000
Subject: [PATCH 2/2] Add missing return statement
---
libc/src/__support/CPP/iterator.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/CPP/iterator.h b/libc/src/__support/CPP/iterator.h
index 4d06e181bcf064..b0fd5c9f22ae01 100644
--- a/libc/src/__support/CPP/iterator.h
+++ b/libc/src/__support/CPP/iterator.h
@@ -71,7 +71,7 @@ template <typename Iter> class reverse_iterator {
return lhs.base() <= rhs.base();
}
- LIBC_INLINE constexpr iterator_type base() const { current; }
+ LIBC_INLINE constexpr iterator_type base() const { return current; }
LIBC_INLINE constexpr reference operator*() const {
Iter tmp = current;
More information about the libc-commits
mailing list