blob: c69f30ed5fe872d5483205392035f3572ee29b6b [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)),
34 stride_(reinterpret_cast<uintptr_t>(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) {
51 auto temp = *this;
52 ptr_ += stride_;
53 return temp;
54 }
55
Mathieu Chartier54d220e2015-07-30 16:20:06 -070056 StrideIterator operator+(ssize_t delta) const {
57 auto temp = *this;
58 temp.ptr_ += static_cast<ssize_t>(stride_) * delta;
59 return temp;
60 }
61
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 T& operator*() const {
63 return *reinterpret_cast<T*>(ptr_);
64 }
65
66 T* operator->() const {
67 return &**this;
68 }
69
70 private:
71 uintptr_t ptr_;
Mathieu Chartiercf3b1a32015-06-01 14:30:06 -070072 // Not const for operator=.
73 size_t stride_;
Mathieu Chartiere401d142015-04-22 13:56:20 -070074};
75
76} // namespace art
77
78#endif // ART_RUNTIME_STRIDE_ITERATOR_H_