1 //===-- Standalone implementation of iterator -------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_ITERATOR_H
10 #define LLVM_LIBC_SRC___SUPPORT_CPP_ITERATOR_H
12 #include "src/__support/CPP/type_traits/enable_if.h"
13 #include "src/__support/CPP/type_traits/is_convertible.h"
14 #include "src/__support/CPP/type_traits/is_same.h"
15 #include "src/__support/macros/attributes.h"
16 #include "src/__support/macros/config.h"
18 namespace LIBC_NAMESPACE_DECL
{
21 template <typename T
> struct iterator_traits
;
22 template <typename T
> struct iterator_traits
<T
*> {
23 using reference
= T
&;
27 template <typename Iter
> class reverse_iterator
{
31 using reference
= typename iterator_traits
<Iter
>::reference
;
32 using value_type
= typename iterator_traits
<Iter
>::value_type
;
33 using iterator_type
= Iter
;
35 LIBC_INLINE
reverse_iterator() : current() {}
36 LIBC_INLINE
constexpr explicit reverse_iterator(Iter it
) : current(it
) {}
38 template <typename Other
,
39 cpp::enable_if_t
<!cpp::is_same_v
<Iter
, Other
> &&
40 cpp::is_convertible_v
<const Other
&, Iter
>,
42 LIBC_INLINE
constexpr explicit reverse_iterator(const Other
&it
)
45 LIBC_INLINE
friend constexpr bool operator==(const reverse_iterator
&lhs
,
46 const reverse_iterator
&rhs
) {
47 return lhs
.base() == rhs
.base();
50 LIBC_INLINE
friend constexpr bool operator!=(const reverse_iterator
&lhs
,
51 const reverse_iterator
&rhs
) {
52 return lhs
.base() != rhs
.base();
55 LIBC_INLINE
friend constexpr bool operator<(const reverse_iterator
&lhs
,
56 const reverse_iterator
&rhs
) {
57 return lhs
.base() > rhs
.base();
60 LIBC_INLINE
friend constexpr bool operator<=(const reverse_iterator
&lhs
,
61 const reverse_iterator
&rhs
) {
62 return lhs
.base() >= rhs
.base();
65 LIBC_INLINE
friend constexpr bool operator>(const reverse_iterator
&lhs
,
66 const reverse_iterator
&rhs
) {
67 return lhs
.base() < rhs
.base();
70 LIBC_INLINE
friend constexpr bool operator>=(const reverse_iterator
&lhs
,
71 const reverse_iterator
&rhs
) {
72 return lhs
.base() <= rhs
.base();
75 LIBC_INLINE
constexpr iterator_type
base() const { return current
; }
77 LIBC_INLINE
constexpr reference
operator*() const {
81 LIBC_INLINE
constexpr reverse_iterator
operator--() {
85 LIBC_INLINE
constexpr reverse_iterator
&operator++() {
89 LIBC_INLINE
constexpr reverse_iterator
operator++(int) {
90 reverse_iterator
tmp(*this);
97 } // namespace LIBC_NAMESPACE_DECL
99 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_ITERATOR_H