[libc-commits] [libc] [libc] Add reverse_iterator comparisons (PR #86147)
Guillaume Chatelet via libc-commits
libc-commits at lists.llvm.org
Thu Mar 21 11:00:01 PDT 2024
https://github.com/gchatelet updated https://github.com/llvm/llvm-project/pull/86147
>From f984e8adacfd35c5cc9d07dccc3ca1200fadf9c1 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Thu, 21 Mar 2024 16:22:08 +0000
Subject: [PATCH 1/3] [libc] Add reverse_iterator comparisons
https://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmp
---
libc/src/__support/CPP/iterator.h | 34 +++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/libc/src/__support/CPP/iterator.h b/libc/src/__support/CPP/iterator.h
index c5bfb1912c7b74..258bde080798f2 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,7 @@ template <typename Iter> class reverse_iterator {
public:
using reference = typename iterator_traits<Iter>::reference;
+ using value_type = typename iterator_traits<Iter>::value_type;
LIBC_INLINE reverse_iterator() : current() {}
LIBC_INLINE constexpr explicit reverse_iterator(Iter it) : current(it) {}
@@ -38,6 +40,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 189c99a76e97bc8e7f8b699d3fdc1ac93930fb67 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Thu, 21 Mar 2024 16:24:10 +0000
Subject: [PATCH 2/3] Fix ordering
---
libc/src/__support/CPP/iterator.h | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/libc/src/__support/CPP/iterator.h b/libc/src/__support/CPP/iterator.h
index 258bde080798f2..ee1ebb5bf71043 100644
--- a/libc/src/__support/CPP/iterator.h
+++ b/libc/src/__support/CPP/iterator.h
@@ -52,22 +52,22 @@ template <typename Iter> class reverse_iterator {
LIBC_INLINE friend constexpr bool operator<(const reverse_iterator &lhs,
const reverse_iterator &rhs) {
- return lhs.base() < rhs.base();
+ return lhs.base() > rhs.base();
}
LIBC_INLINE friend constexpr bool operator<=(const reverse_iterator &lhs,
const reverse_iterator &rhs) {
- return lhs.base() <= rhs.base();
+ return lhs.base() >= rhs.base();
}
LIBC_INLINE friend constexpr bool operator>(const reverse_iterator &lhs,
const reverse_iterator &rhs) {
- return lhs.base() > rhs.base();
+ return lhs.base() < rhs.base();
}
LIBC_INLINE friend constexpr bool operator>=(const reverse_iterator &lhs,
const reverse_iterator &rhs) {
- return lhs.base() >= rhs.base();
+ return lhs.base() <= rhs.base();
}
LIBC_INLINE constexpr iterator_type base() const { current; }
>From dddbce6dd85f560584fdc8338361682564eef44d Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Thu, 21 Mar 2024 17:59:32 +0000
Subject: [PATCH 3/3] Add missing `iterator_type`
---
libc/src/__support/CPP/iterator.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/libc/src/__support/CPP/iterator.h b/libc/src/__support/CPP/iterator.h
index ee1ebb5bf71043..4d06e181bcf064 100644
--- a/libc/src/__support/CPP/iterator.h
+++ b/libc/src/__support/CPP/iterator.h
@@ -29,6 +29,7 @@ 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) {}
More information about the libc-commits
mailing list