Skip to content

[libc] Add reverse_iterator comparisons #86147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2024

Conversation

gchatelet
Copy link
Contributor

@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2024

@llvm/pr-subscribers-libc

Author: Guillaume Chatelet (gchatelet)

Changes

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

Copy link
Member

@nickdesaulniers nickdesaulniers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I had the equality operators locally, but then got hung up adapting BlockStore::Iterator to make use of this, then got distracted by other fires.

@gchatelet
Copy link
Contributor Author

Thanks! I had the equality operators locally, but then got hung up adapting BlockStore::Iterator to make use of this, then got distracted by other fires.

np ! Let's iterate 🙃

@nickdesaulniers
Copy link
Member

Good catch with 189c99a. That's pretty subtle. It matches libcxx/include/__iterator/reverse_iterator.h so LGTM

@gchatelet gchatelet merged commit c96b61a into llvm:main Mar 21, 2024
@gchatelet gchatelet deleted the reverse_iterator_cmp branch March 21, 2024 19:33
gchatelet added a commit that referenced this pull request Mar 21, 2024
gchatelet added a commit that referenced this pull request Mar 21, 2024
gchatelet added a commit that referenced this pull request Mar 22, 2024
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
chencha3 pushed a commit to chencha3/llvm-project that referenced this pull request Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants