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_LIBARTBASE_BASE_ITERATION_RANGE_H_
18 #define ART_LIBARTBASE_BASE_ITERATION_RANGE_H_
19
20 #include <iterator>
21
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
IterationRange(iterator first,iterator last)35 IterationRange(iterator first, iterator last) : first_(first), last_(last) { }
36
begin()37 iterator begin() const { return first_; }
end()38 iterator end() const { return last_; }
cbegin()39 iterator cbegin() const { return first_; }
cend()40 iterator cend() const { return last_; }
41
42 protected:
43 iterator first_;
44 iterator last_;
45 };
46
47 template <typename Iter>
MakeIterationRange(const Iter & begin_it,const Iter & end_it)48 inline IterationRange<Iter> MakeIterationRange(const Iter& begin_it, const Iter& end_it) {
49 return IterationRange<Iter>(begin_it, end_it);
50 }
51
52 template<typename List>
MakeIterationRange(List & list)53 inline IterationRange<typename List::iterator> MakeIterationRange(List& list) {
54 return IterationRange<typename List::iterator>(list.begin(), list.end());
55 }
56
57 template <typename Iter>
MakeEmptyIterationRange(const Iter & it)58 inline IterationRange<Iter> MakeEmptyIterationRange(const Iter& it) {
59 return IterationRange<Iter>(it, it);
60 }
61
62 template <typename Container>
ReverseRange(Container && c)63 inline auto ReverseRange(Container&& c) {
64 typedef typename std::reverse_iterator<decltype(c.begin())> riter;
65 return MakeIterationRange(riter(c.end()), riter(c.begin()));
66 }
67
68 template <typename T, size_t size>
ReverseRange(T (& array)[size])69 inline auto ReverseRange(T (&array)[size]) {
70 return ReverseRange(MakeIterationRange<T*>(array, array+size));
71 }
72
73 } // namespace art
74
75 #endif // ART_LIBARTBASE_BASE_ITERATION_RANGE_H_
76