2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <folly/portability/SysTypes.h>
25 * Similar to Python's enumerate(), folly::enumerate() can be used to
26 * iterate a range with a for-range loop, and it also allows to
27 * retrieve the count of iterations so far.
31 * for (auto it : folly::enumerate(vec)) {
32 * // *it is a reference to the current element. Const if vec is const.
33 * // it->member can be used as well.
34 * // it.index contains the iteration count.
37 * If the iteration variable is const, the reference is too.
39 * for (const auto it : folly::enumerate(vec)) {
40 * // *it is always a const reference.
43 * @author Giuseppe Ottaviano <ott@fb.com>
55 struct MakeConst<T&> {
56 using type = const T&;
59 struct MakeConst<T*> {
60 using type = const T*;
63 // Raw pointers don't have an operator->() member function, so the
64 // second overload will be SFINAEd out in that case. Otherwise, the
65 // second is preferred in the partial order for getPointer(_, 0).
66 template <class Iterator>
67 auto getPointer(const Iterator& it, long) -> decltype(std::addressof(*it)) {
68 return std::addressof(*it);
70 template <class Iterator>
71 auto getPointer(const Iterator& it, int) -> decltype(it.operator->()) {
72 return it.operator->();
75 template <class Iterator>
78 explicit Enumerator(Iterator it) : it_(std::move(it)) {}
82 using difference_type = ssize_t;
83 using value_type = typename std::iterator_traits<Iterator>::value_type;
84 using reference = typename std::iterator_traits<Iterator>::reference;
85 using pointer = typename std::iterator_traits<Iterator>::pointer;
86 using iterator_category = std::input_iterator_tag;
88 explicit Proxy(const Enumerator* e) : it_(e->it_), index(e->idx_) {}
90 // Non-const Proxy: Forward constness from Iterator.
91 reference operator*() {
94 pointer operator->() {
95 return getPointer(it_, 0);
98 // Const Proxy: Force const references.
99 typename MakeConst<reference>::type operator*() const {
102 typename MakeConst<pointer>::type operator->() const {
103 return getPointer(it_, 0);
113 Proxy operator*() const {
117 Enumerator& operator++() {
123 template <typename OtherIterator>
124 bool operator==(const Enumerator<OtherIterator>& rhs) {
125 return it_ == rhs.it_;
128 template <typename OtherIterator>
129 bool operator!=(const Enumerator<OtherIterator>& rhs) {
130 return !(*this == rhs);
134 template <typename OtherIterator>
135 friend class Enumerator;
141 template <class Range>
142 class RangeEnumerator {
144 using BeginIteratorType = decltype(std::declval<Range>().begin());
145 using EndIteratorType = decltype(std::declval<Range>().end());
148 explicit RangeEnumerator(Range&& r) : r_(std::forward<Range>(r)) {}
150 Enumerator<BeginIteratorType> begin() {
151 return Enumerator<BeginIteratorType>(r_.begin());
153 Enumerator<EndIteratorType> end() {
154 return Enumerator<EndIteratorType>(r_.end());
158 } // namespace detail
160 template <class Range>
161 detail::RangeEnumerator<Range> enumerate(Range&& r) {
162 return detail::RangeEnumerator<Range>(std::forward<Range>(r));