1 /* Copyright 2017 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 #ifndef TENSORFLOW_COMPILER_XLA_ITERATOR_UTIL_H_
17 #define TENSORFLOW_COMPILER_XLA_ITERATOR_UTIL_H_
18 
19 #include <iterator>
20 #include <utility>
21 
22 namespace xla {
23 
24 // UnwrappingIterator is a transforming iterator that calls get() on the
25 // elements it returns.
26 //
27 // Together with tensorflow::gtl::iterator_range, this lets classes which
28 // contain a collection of smart pointers expose a view of raw pointers to
29 // consumers.  For example:
30 //
31 //  class MyContainer {
32 //   public:
33 //    tensorflow::gtl::iterator_range<
34 //        UnwrappingIterator<std::vector<std::unique_ptr<Thing>>::iterator>>
35 //    things() {
36 //      return {MakeUnwrappingIterator(things_.begin()),
37 //              MakeUnwrappingIterator(things_.end())};
38 //    }
39 //
40 //    tensorflow::gtl::iterator_range<UnwrappingIterator<
41 //        std::vector<std::unique_ptr<Thing>>::const_iterator>>
42 //    things() const {
43 //      return {MakeUnwrappingIterator(things_.begin()),
44 //              MakeUnwrappingIterator(things_.end())};
45 //    }
46 //
47 //   private:
48 //    std::vector<std::unique_ptr<Thing>> things_;
49 //  };
50 //
51 //  MyContainer container = ...;
52 //  for (Thing* t : container.things()) {
53 //    ...
54 //  }
55 //
56 // For simplicity, UnwrappingIterator is currently unconditionally an
57 // input_iterator -- it doesn't inherit any superpowers NestedIterator may have.
58 template <typename NestedIter>
59 class UnwrappingIterator
60     : public std::iterator<std::input_iterator_tag,
61                            decltype(std::declval<NestedIter>()->get())> {
62  private:
63   NestedIter iter_;
64 
65  public:
UnwrappingIterator(NestedIter iter)66   explicit UnwrappingIterator(NestedIter iter) : iter_(std::move(iter)) {}
67 
68   auto operator*() -> decltype(iter_->get()) { return iter_->get(); }
69   auto operator-> () -> decltype(iter_->get()) { return iter_->get(); }
70   UnwrappingIterator& operator++() {
71     ++iter_;
72     return *this;
73   }
74   UnwrappingIterator operator++(int) {
75     UnwrappingIterator temp(iter_);
76     operator++();
77     return temp;
78   }
79 
80   friend bool operator==(const UnwrappingIterator& a,
81                          const UnwrappingIterator& b) {
82     return a.iter_ == b.iter_;
83   }
84 
85   friend bool operator!=(const UnwrappingIterator& a,
86                          const UnwrappingIterator& b) {
87     return !(a == b);
88   }
89 };
90 
91 template <typename NestedIter>
MakeUnwrappingIterator(NestedIter iter)92 UnwrappingIterator<NestedIter> MakeUnwrappingIterator(NestedIter iter) {
93   return UnwrappingIterator<NestedIter>(std::move(iter));
94 }
95 
96 }  // namespace xla
97 
98 #endif  // TENSORFLOW_COMPILER_XLA_ITERATOR_UTIL_H_
99