Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [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_BASE_ITERATION_RANGE_H_ |
| 18 | #define ART_RUNTIME_BASE_ITERATION_RANGE_H_ |
| 19 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 20 | #include <iterator> |
| 21 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 22 | namespace art { |
| 23 | |
| 24 | // Helper class that acts as a container for range-based loops, given an iteration |
| 25 | // range [first, last) defined by two iterators. |
| 26 | template <typename Iter> |
| 27 | class IterationRange { |
| 28 | public: |
| 29 | typedef Iter iterator; |
| 30 | typedef typename std::iterator_traits<Iter>::difference_type difference_type; |
| 31 | typedef typename std::iterator_traits<Iter>::value_type value_type; |
| 32 | typedef typename std::iterator_traits<Iter>::pointer pointer; |
| 33 | typedef typename std::iterator_traits<Iter>::reference reference; |
| 34 | |
| 35 | IterationRange(iterator first, iterator last) : first_(first), last_(last) { } |
| 36 | |
| 37 | iterator begin() const { return first_; } |
| 38 | iterator end() const { return last_; } |
| 39 | iterator cbegin() const { return first_; } |
| 40 | iterator cend() const { return last_; } |
| 41 | |
| 42 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 43 | const iterator first_; |
| 44 | const iterator last_; |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 47 | template <typename Iter> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 48 | inline IterationRange<Iter> MakeIterationRange(const Iter& begin_it, const Iter& end_it) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 49 | return IterationRange<Iter>(begin_it, end_it); |
| 50 | } |
| 51 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 52 | template <typename Iter> |
Vladimir Marko | f04cf54 | 2016-08-31 15:25:25 +0100 | [diff] [blame] | 53 | inline IterationRange<Iter> MakeEmptyIterationRange(const Iter& it) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 54 | return IterationRange<Iter>(it, it); |
| 55 | } |
| 56 | |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame^] | 57 | template <typename Container> |
| 58 | inline auto ReverseRange(Container& c) { |
| 59 | typedef typename std::reverse_iterator<decltype(c.begin())> riter; |
| 60 | return MakeIterationRange(riter(c.end()), riter(c.begin())); |
| 61 | } |
| 62 | |
| 63 | template <typename T, size_t size> |
| 64 | inline auto ReverseRange(T (&array)[size]) { |
| 65 | return ReverseRange(MakeIterationRange<T*>(array, array+size)); |
| 66 | } |
| 67 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 68 | } // namespace art |
| 69 | |
| 70 | #endif // ART_RUNTIME_BASE_ITERATION_RANGE_H_ |