Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1 | /* |
| 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 Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 22 | #include "base/logging.h" |
| 23 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 24 | namespace art { |
| 25 | |
| 26 | template<typename T> |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 27 | class StrideIterator : public std::iterator<std::forward_iterator_tag, T> { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 28 | public: |
| 29 | StrideIterator(const StrideIterator&) = default; |
| 30 | StrideIterator(StrideIterator&&) = default; |
| 31 | StrideIterator& operator=(const StrideIterator&) = default; |
| 32 | StrideIterator& operator=(StrideIterator&&) = default; |
| 33 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 34 | StrideIterator(T* ptr, size_t stride) |
| 35 | : ptr_(reinterpret_cast<uintptr_t>(ptr)), |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 36 | stride_(stride) {} |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | |
| 38 | bool operator==(const StrideIterator& other) const { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 39 | DCHECK_EQ(stride_, other.stride_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 40 | 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 Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 53 | StrideIterator<T> temp = *this; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 54 | ptr_ += stride_; |
| 55 | return temp; |
| 56 | } |
| 57 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 58 | StrideIterator operator+(ssize_t delta) const { |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 59 | StrideIterator<T> temp = *this; |
| 60 | temp += delta; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 61 | return temp; |
| 62 | } |
| 63 | |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 64 | StrideIterator& operator+=(ssize_t delta) { |
| 65 | ptr_ += static_cast<ssize_t>(stride_) * delta; |
| 66 | return *this; |
| 67 | } |
| 68 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 69 | 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 Chartier | cf3b1a3 | 2015-06-01 14:30:06 -0700 | [diff] [blame] | 79 | // Not const for operator=. |
| 80 | size_t stride_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } // namespace art |
| 84 | |
| 85 | #endif // ART_RUNTIME_STRIDE_ITERATOR_H_ |