blob: 0560c33eee8f403737bf5db78b324e0cd99c632a [file] [log] [blame]
Mathieu Chartiere401d142015-04-22 13:56:20 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_STRIDE_ITERATOR_H_
18#define ART_RUNTIME_STRIDE_ITERATOR_H_
19
20#include <iterator>
21
Vladimir Marko35831e82015-09-11 11:59:18 +010022#include "base/logging.h"
23
Mathieu Chartiere401d142015-04-22 13:56:20 -070024namespace art {
25
26template<typename T>
Vladimir Marko0eb882b2017-05-15 13:39:18 +010027class StrideIterator : public std::iterator<std::random_access_iterator_tag, T> {
Mathieu Chartiere401d142015-04-22 13:56:20 -070028 public:
Vladimir Marko0eb882b2017-05-15 13:39:18 +010029 using difference_type =
30 typename std::iterator<std::random_access_iterator_tag, T>::difference_type;
31
Mathieu Chartiere401d142015-04-22 13:56:20 -070032 StrideIterator(const StrideIterator&) = default;
33 StrideIterator(StrideIterator&&) = default;
34 StrideIterator& operator=(const StrideIterator&) = default;
35 StrideIterator& operator=(StrideIterator&&) = default;
36
Mathieu Chartier54d220e2015-07-30 16:20:06 -070037 StrideIterator(T* ptr, size_t stride)
38 : ptr_(reinterpret_cast<uintptr_t>(ptr)),
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070039 stride_(stride) {}
Mathieu Chartiere401d142015-04-22 13:56:20 -070040
41 bool operator==(const StrideIterator& other) const {
Mathieu Chartier54d220e2015-07-30 16:20:06 -070042 DCHECK_EQ(stride_, other.stride_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070043 return ptr_ == other.ptr_;
44 }
45
46 bool operator!=(const StrideIterator& other) const {
47 return !(*this == other);
48 }
49
Vladimir Marko0eb882b2017-05-15 13:39:18 +010050 StrideIterator& operator++() { // Value after modification.
Mathieu Chartiere401d142015-04-22 13:56:20 -070051 ptr_ += stride_;
52 return *this;
53 }
54
55 StrideIterator operator++(int) {
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070056 StrideIterator<T> temp = *this;
Vladimir Marko0eb882b2017-05-15 13:39:18 +010057 ++*this;
Mathieu Chartiere401d142015-04-22 13:56:20 -070058 return temp;
59 }
60
Vladimir Marko0eb882b2017-05-15 13:39:18 +010061 StrideIterator& operator--() { // Value after modification.
62 ptr_ -= stride_;
63 return *this;
64 }
65
66 StrideIterator operator--(int) {
67 StrideIterator<T> temp = *this;
68 --*this;
69 return temp;
70 }
71
72 StrideIterator& operator+=(difference_type delta) {
73 ptr_ += static_cast<ssize_t>(stride_) * delta;
74 return *this;
75 }
76
77 StrideIterator operator+(difference_type delta) const {
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070078 StrideIterator<T> temp = *this;
79 temp += delta;
Mathieu Chartier54d220e2015-07-30 16:20:06 -070080 return temp;
81 }
82
Vladimir Marko0eb882b2017-05-15 13:39:18 +010083 StrideIterator& operator-=(difference_type delta) {
84 ptr_ -= static_cast<ssize_t>(stride_) * delta;
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070085 return *this;
86 }
87
Vladimir Marko0eb882b2017-05-15 13:39:18 +010088 StrideIterator operator-(difference_type delta) const {
89 StrideIterator<T> temp = *this;
90 temp -= delta;
91 return temp;
92 }
93
94 difference_type operator-(const StrideIterator& rhs) {
95 DCHECK_EQ(stride_, rhs.stride_);
96 DCHECK_EQ((ptr_ - rhs.ptr_) % stride_, 0u);
97 return (ptr_ - rhs.ptr_) / stride_;
98 }
99
Mathieu Chartiere401d142015-04-22 13:56:20 -0700100 T& operator*() const {
101 return *reinterpret_cast<T*>(ptr_);
102 }
103
104 T* operator->() const {
105 return &**this;
106 }
107
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100108 T& operator[](difference_type n) {
109 return *(*this + n);
110 }
111
Mathieu Chartiere401d142015-04-22 13:56:20 -0700112 private:
113 uintptr_t ptr_;
Mathieu Chartiercf3b1a32015-06-01 14:30:06 -0700114 // Not const for operator=.
115 size_t stride_;
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100116
117 template <typename U>
118 friend bool operator<(const StrideIterator<U>& lhs, const StrideIterator<U>& rhs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700119};
120
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100121template <typename T>
122StrideIterator<T> operator+(typename StrideIterator<T>::difference_type dist,
123 const StrideIterator<T>& it) {
124 return it + dist;
125}
126
127template <typename T>
128bool operator<(const StrideIterator<T>& lhs, const StrideIterator<T>& rhs) {
129 DCHECK_EQ(lhs.stride_, rhs.stride_);
130 return lhs.ptr_ < rhs.ptr_;
131}
132
133template <typename T>
134bool operator>(const StrideIterator<T>& lhs, const StrideIterator<T>& rhs) {
135 return rhs < lhs;
136}
137
138template <typename T>
139bool operator<=(const StrideIterator<T>& lhs, const StrideIterator<T>& rhs) {
140 return !(rhs < lhs);
141}
142
143template <typename T>
144bool operator>=(const StrideIterator<T>& lhs, const StrideIterator<T>& rhs) {
145 return !(lhs < rhs);
146}
147
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148} // namespace art
149
150#endif // ART_RUNTIME_STRIDE_ITERATOR_H_