1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 // This provides a very simple, boring adaptor for a begin and end iterator
17 // into a range type. This should be used to build range views that work well
18 // with range based for loops and range based constructors.
19 //
20 // Note that code here follows more standards-based coding conventions as it
21 // is mirroring proposed interfaces for standardization.
22 //
23 // Converted from chandlerc@'s code to Google style by joshl@.
24 
25 #ifndef TENSORFLOW_LIB_GTL_ITERATOR_RANGE_H_
26 #define TENSORFLOW_LIB_GTL_ITERATOR_RANGE_H_
27 
28 #include <utility>
29 
30 namespace tensorflow {
31 namespace gtl {
32 
33 // A range adaptor for a pair of iterators.
34 //
35 // This just wraps two iterators into a range-compatible interface. Nothing
36 // fancy at all.
37 template <typename IteratorT>
38 class iterator_range {
39  public:
40   using value_type = decltype(*std::declval<IteratorT>());
41   using iterator = IteratorT;
42   using const_iterator = IteratorT;
43 
iterator_range()44   iterator_range() : begin_iterator_(), end_iterator_() {}
iterator_range(IteratorT begin_iterator,IteratorT end_iterator)45   iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
46       : begin_iterator_(std::move(begin_iterator)),
47         end_iterator_(std::move(end_iterator)) {}
48 
begin()49   IteratorT begin() const { return begin_iterator_; }
end()50   IteratorT end() const { return end_iterator_; }
51 
52  private:
53   IteratorT begin_iterator_, end_iterator_;
54 };
55 
56 // Convenience function for iterating over sub-ranges.
57 //
58 // This provides a bit of syntactic sugar to make using sub-ranges
59 // in for loops a bit easier. Analogous to std::make_pair().
60 template <class T>
make_range(T x,T y)61 iterator_range<T> make_range(T x, T y) {
62   return iterator_range<T>(std::move(x), std::move(y));
63 }
64 
65 }  // namespace gtl
66 }  // namespace tensorflow
67 
68 #endif  // TENSORFLOW_LIB_GTL_ITERATOR_RANGE_H_
69