[libc++][Android] Allow testing libc++ with clang-r536225 (#116149)
[llvm-project.git] / libc / src / __support / CPP / iterator.h
blob168a2697318220be9062a20807e026ea414b8697
1 //===-- Standalone implementation of iterator -------------------*- C++ -*-===//
2 //
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
6 //
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 {
19 namespace cpp {
21 template <typename T> struct iterator_traits;
22 template <typename T> struct iterator_traits<T *> {
23 using reference = T &;
24 using value_type = T;
27 template <typename Iter> class reverse_iterator {
28 Iter current;
30 public:
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>,
41 int> = 0>
42 LIBC_INLINE constexpr explicit reverse_iterator(const Other &it)
43 : current(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 {
78 Iter tmp = current;
79 return *--tmp;
81 LIBC_INLINE constexpr reverse_iterator operator--() {
82 ++current;
83 return *this;
85 LIBC_INLINE constexpr reverse_iterator &operator++() {
86 --current;
87 return *this;
89 LIBC_INLINE constexpr reverse_iterator operator++(int) {
90 reverse_iterator tmp(*this);
91 --current;
92 return tmp;
96 } // namespace cpp
97 } // namespace LIBC_NAMESPACE_DECL
99 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_ITERATOR_H