[libc-commits] [libc] [libc] Add reverse_iterator comparisons (PR #86147)

via libc-commits libc-commits at lists.llvm.org
Thu Mar 21 09:23:05 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Guillaume Chatelet (gchatelet)

<details>
<summary>Changes</summary>

https://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmp


---
Full diff: https://github.com/llvm/llvm-project/pull/86147.diff


1 Files Affected:

- (modified) libc/src/__support/CPP/iterator.h (+34) 


``````````diff
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;

``````````

</details>


https://github.com/llvm/llvm-project/pull/86147


More information about the libc-commits mailing list