blob: ac04c3b40397727c238b19231f84731a458b1d7b [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>
Mathieu Chartierd4d83b82015-06-19 20:24:45 -070027class StrideIterator : public std::iterator<std::forward_iterator_tag, T> {
Mathieu Chartiere401d142015-04-22 13:56:20 -070028 public:
29 StrideIterator(const StrideIterator&) = default;
30 StrideIterator(StrideIterator&&) = default;
31 StrideIterator& operator=(const StrideIterator&) = default;
32 StrideIterator& operator=(StrideIterator&&) = default;
33
Mathieu Chartier54d220e2015-07-30 16:20:06 -070034 StrideIterator(T* ptr, size_t stride)
35 : ptr_(reinterpret_cast<uintptr_t>(ptr)),
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070036 stride_(stride) {}
Mathieu Chartiere401d142015-04-22 13:56:20 -070037
38 bool operator==(const StrideIterator& other) const {
Mathieu Chartier54d220e2015-07-30 16:20:06 -070039 DCHECK_EQ(stride_, other.stride_);
Mathieu Chartiere401d142015-04-22 13:56:20 -070040 return ptr_ == other.ptr_;
41 }
42
43 bool operator!=(const StrideIterator& other) const {
44 return !(*this == other);
45 }
46
47 StrideIterator operator++() { // Value after modification.
48 ptr_ += stride_;
49 return *this;
50 }
51
52 StrideIterator operator++(int) {
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070053 StrideIterator<T> temp = *this;
Mathieu Chartiere401d142015-04-22 13:56:20 -070054 ptr_ += stride_;
55 return temp;
56 }
57
Mathieu Chartier54d220e2015-07-30 16:20:06 -070058 StrideIterator operator+(ssize_t delta) const {
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070059 StrideIterator<T> temp = *this;
60 temp += delta;
Mathieu Chartier54d220e2015-07-30 16:20:06 -070061 return temp;
62 }
63
Mathieu Chartierc0fe56a2015-08-11 13:01:23 -070064 StrideIterator& operator+=(ssize_t delta) {
65 ptr_ += static_cast<ssize_t>(stride_) * delta;
66 return *this;
67 }
68
Mathieu Chartiere401d142015-04-22 13:56:20 -070069 T& operator*() const {
70 return *reinterpret_cast<T*>(ptr_);
71 }
72
73 T* operator->() const {
74 return &**this;
75 }
76
77 private:
78 uintptr_t ptr_;
Mathieu Chartiercf3b1a32015-06-01 14:30:06 -070079 // Not const for operator=.
80 size_t stride_;
Mathieu Chartiere401d142015-04-22 13:56:20 -070081};
82
83} // namespace art
84
85#endif // ART_RUNTIME_STRIDE_ITERATOR_H_