Skip to content

Commit c5f839b

Browse files
authored
[reland][libc] Add reverse_iterator comparisons (#86188)
This is a reland of #86147 but with a proper `base()` function. https://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmp
1 parent 0289ae5 commit c5f839b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

libc/src/__support/CPP/iterator.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ namespace cpp {
2020
template <typename T> struct iterator_traits;
2121
template <typename T> struct iterator_traits<T *> {
2222
using reference = T &;
23+
using value_type = T;
2324
};
2425

2526
template <typename Iter> class reverse_iterator {
2627
Iter current;
2728

2829
public:
2930
using reference = typename iterator_traits<Iter>::reference;
31+
using value_type = typename iterator_traits<Iter>::value_type;
32+
using iterator_type = Iter;
3033

3134
LIBC_INLINE reverse_iterator() : current() {}
3235
LIBC_INLINE constexpr explicit reverse_iterator(Iter it) : current(it) {}
@@ -38,6 +41,38 @@ template <typename Iter> class reverse_iterator {
3841
LIBC_INLINE constexpr explicit reverse_iterator(const Other &it)
3942
: current(it) {}
4043

44+
LIBC_INLINE friend constexpr bool operator==(const reverse_iterator &lhs,
45+
const reverse_iterator &rhs) {
46+
return lhs.base() == rhs.base();
47+
}
48+
49+
LIBC_INLINE friend constexpr bool operator!=(const reverse_iterator &lhs,
50+
const reverse_iterator &rhs) {
51+
return lhs.base() != rhs.base();
52+
}
53+
54+
LIBC_INLINE friend constexpr bool operator<(const reverse_iterator &lhs,
55+
const reverse_iterator &rhs) {
56+
return lhs.base() > rhs.base();
57+
}
58+
59+
LIBC_INLINE friend constexpr bool operator<=(const reverse_iterator &lhs,
60+
const reverse_iterator &rhs) {
61+
return lhs.base() >= rhs.base();
62+
}
63+
64+
LIBC_INLINE friend constexpr bool operator>(const reverse_iterator &lhs,
65+
const reverse_iterator &rhs) {
66+
return lhs.base() < rhs.base();
67+
}
68+
69+
LIBC_INLINE friend constexpr bool operator>=(const reverse_iterator &lhs,
70+
const reverse_iterator &rhs) {
71+
return lhs.base() <= rhs.base();
72+
}
73+
74+
LIBC_INLINE constexpr iterator_type base() const { return current; }
75+
4176
LIBC_INLINE constexpr reference operator*() const {
4277
Iter tmp = current;
4378
return *--tmp;

0 commit comments

Comments
 (0)