omega_h
Reliable mesh adaptation
Omega_h_cast_iterator.hpp
1 #ifndef OMEGA_H_CAST_ITERATOR_HPP
2 #define OMEGA_H_CAST_ITERATOR_HPP
3 
4 #include <iterator>
5 
6 namespace Omega_h {
7 
8 template <typename To, typename From>
9 class CastIterator {
10  From const* ptr;
11 
12  public:
13  using value_type = To;
14  using difference_type = std::ptrdiff_t;
15  using reference = value_type;
16  using pointer = value_type const*;
17  using iterator_category = std::random_access_iterator_tag;
18  inline CastIterator() = default;
19  OMEGA_H_INLINE CastIterator(From const* ptr_in) : ptr(ptr_in) {}
20  OMEGA_H_INLINE bool operator==(CastIterator const& other) const {
21  return ptr == other.ptr;
22  }
23  OMEGA_H_INLINE bool operator!=(CastIterator const& other) const {
24  return ptr != other.ptr;
25  }
26  OMEGA_H_INLINE reference operator*() const { return *ptr; }
27  OMEGA_H_INLINE CastIterator& operator++() {
28  ++ptr;
29  return *this;
30  }
31  OMEGA_H_INLINE CastIterator operator++(int) {
32  auto const ret = *this;
33  ++ptr;
34  return ret;
35  }
36  OMEGA_H_INLINE CastIterator& operator--() {
37  --ptr;
38  return *this;
39  }
40  OMEGA_H_INLINE CastIterator operator--(int) {
41  auto const ret = *this;
42  --ptr;
43  return ret;
44  }
45  OMEGA_H_INLINE CastIterator& operator+=(difference_type n) {
46  ptr += n;
47  return *this;
48  }
49  OMEGA_H_INLINE CastIterator& operator-=(difference_type n) {
50  ptr -= n;
51  return *this;
52  }
53  OMEGA_H_INLINE CastIterator operator+(difference_type n) const {
54  return CastIterator(ptr + n);
55  }
56  OMEGA_H_INLINE CastIterator operator-(difference_type n) const {
57  return CastIterator(ptr - n);
58  }
59  OMEGA_H_INLINE difference_type operator-(CastIterator const& other) const {
60  return ptr - other.ptr;
61  }
62  OMEGA_H_INLINE value_type operator[](difference_type n) const {
63  return *(ptr + n);
64  }
65  OMEGA_H_INLINE bool operator<(CastIterator const& other) const {
66  return ptr < other.ptr;
67  }
68  OMEGA_H_INLINE bool operator>(CastIterator const& other) const {
69  return ptr > other.ptr;
70  }
71  OMEGA_H_INLINE bool operator<=(CastIterator const& other) const {
72  return ptr <= other.ptr;
73  }
74  OMEGA_H_INLINE bool operator>=(CastIterator const& other) const {
75  return ptr >= other.ptr;
76  }
77 };
78 
79 template <typename To, typename From>
80 OMEGA_H_INLINE CastIterator<To, From> operator+(
81  typename CastIterator<To, From>::difference_type n,
83  return it + n;
84 }
85 
86 } // namespace Omega_h
87 
88 #endif
Definition: Omega_h_cast_iterator.hpp:9
Definition: amr_mpi_test.cpp:6