blob: a9da51ba29358eab1cae0089e3b813903e63105c [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
22namespace art {
23
24template<typename T>
Mathieu Chartierd4d83b82015-06-19 20:24:45 -070025class StrideIterator : public std::iterator<std::forward_iterator_tag, T> {
Mathieu Chartiere401d142015-04-22 13:56:20 -070026 public:
27 StrideIterator(const StrideIterator&) = default;
28 StrideIterator(StrideIterator&&) = default;
29 StrideIterator& operator=(const StrideIterator&) = default;
30 StrideIterator& operator=(StrideIterator&&) = default;
31
Mathieu Chartier54d220e2015-07-30 16:20:06 -070032 StrideIterator(T* ptr, size_t stride)
33 : ptr_(reinterpret_cast<uintptr_t>(ptr)),
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070034 stride_(stride) {}
Mathieu Chartiere401d142015-04-22 13:56:20 -070035
36 bool operator==(const StrideIterator& other) const {
Mathieu Chartier54d220e2015-07-30 16:20:06 -070037 DCHECK_EQ(stride_, other.stride_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070038 return ptr_ == other.ptr_;
39 }
40
41 bool operator!=(const StrideIterator& other) const {
42 return !(*this == other);
43 }
44
45 StrideIterator operator++() { // Value after modification.
46 ptr_ += stride_;
47 return *this;
48 }
49
50 StrideIterator operator++(int) {
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070051 StrideIterator<T> temp = *this;
Mathieu Chartiere401d142015-04-22 13:56:20 -070052 ptr_ += stride_;
53 return temp;
54 }
55
Mathieu Chartier54d220e2015-07-30 16:20:06 -070056 StrideIterator operator+(ssize_t delta) const {
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070057 StrideIterator<T> temp = *this;
58 temp += delta;
Mathieu Chartier54d220e2015-07-30 16:20:06 -070059 return temp;
60 }
61
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070062 StrideIterator& operator+=(ssize_t delta) {
63 ptr_ += static_cast<ssize_t>(stride_) * delta;
64 return *this;
65 }
66
Mathieu Chartiere401d142015-04-22 13:56:20 -070067 T& operator*() const {
68 return *reinterpret_cast<T*>(ptr_);
69 }
70
71 T* operator->() const {
72 return &**this;
73 }
74
75 private:
76 uintptr_t ptr_;
Mathieu Chartiercf3b1a32015-06-01 14:30:06 -070077 // Not const for operator=.
78 size_t stride_;
Mathieu Chartiere401d142015-04-22 13:56:20 -070079};
80
81} // namespace art
82
83#endif // ART_RUNTIME_STRIDE_ITERATOR_H_