| 1 | //===- iterator_range.h - A range adaptor for iterators ---------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// \file |
| 9 | /// This provides a very simple, boring adaptor for a begin and end iterator |
| 10 | /// into a range type. This should be used to build range views that work well |
| 11 | /// with range based for loops and range based constructors. |
| 12 | /// |
| 13 | /// Note that code here follows more standards-based coding conventions as it |
| 14 | /// is mirroring proposed interfaces for standardization. |
| 15 | /// |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #ifndef LLVM_ADT_ITERATOR_RANGE_H |
| 19 | #define LLVM_ADT_ITERATOR_RANGE_H |
| 20 | |
| 21 | #include "llvm/ADT/ADL.h" |
| 22 | #include <type_traits> |
| 23 | #include <utility> |
| 24 | |
| 25 | namespace llvm { |
| 26 | |
| 27 | template <typename From, typename To, typename = void> |
| 28 | struct explicitly_convertible : std::false_type {}; |
| 29 | |
| 30 | template <typename From, typename To> |
| 31 | struct explicitly_convertible< |
| 32 | From, To, |
| 33 | std::void_t<decltype(static_cast<To>( |
| 34 | std::declval<std::add_rvalue_reference_t<From>>()))>> : std::true_type { |
| 35 | }; |
| 36 | |
| 37 | /// A range adaptor for a pair of iterators. |
| 38 | /// |
| 39 | /// This just wraps two iterators into a range-compatible interface. Nothing |
| 40 | /// fancy at all. |
| 41 | template <typename IteratorT> |
| 42 | class iterator_range { |
| 43 | IteratorT begin_iterator, end_iterator; |
| 44 | |
| 45 | public: |
| 46 | #if __GNUC__ == 7 |
| 47 | // Be careful no to break gcc-7 on the mlir target. |
| 48 | // See https://github.com/llvm/llvm-project/issues/63843 |
| 49 | template <typename Container> |
| 50 | #else |
| 51 | template <typename Container, |
| 52 | std::enable_if_t<explicitly_convertible< |
| 53 | detail::IterOfRange<Container>, IteratorT>::value> * = nullptr> |
| 54 | #endif |
| 55 | iterator_range(Container &&c) |
| 56 | : begin_iterator(adl_begin(std::forward<Container>(c))), |
| 57 | end_iterator(adl_end(std::forward<Container>(c))) { |
| 58 | } |
| 59 | iterator_range(IteratorT begin_iterator, IteratorT end_iterator) |
| 60 | : begin_iterator(std::move(begin_iterator)), |
| 61 | end_iterator(std::move(end_iterator)) {} |
| 62 | |
| 63 | IteratorT begin() const { return begin_iterator; } |
| 64 | IteratorT end() const { return end_iterator; } |
| 65 | bool empty() const { return begin_iterator == end_iterator; } |
| 66 | }; |
| 67 | |
| 68 | template <typename Container> |
| 69 | iterator_range(Container &&) -> iterator_range<detail::IterOfRange<Container>>; |
| 70 | |
| 71 | /// Convenience function for iterating over sub-ranges. |
| 72 | /// |
| 73 | /// This provides a bit of syntactic sugar to make using sub-ranges |
| 74 | /// in for loops a bit easier. Analogous to std::make_pair(). |
| 75 | template <class T> iterator_range<T> make_range(T x, T y) { |
| 76 | return iterator_range<T>(std::move(x), std::move(y)); |
| 77 | } |
| 78 | |
| 79 | template <typename T> iterator_range<T> make_range(std::pair<T, T> p) { |
| 80 | return iterator_range<T>(std::move(p.first), std::move(p.second)); |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | #endif |
| 86 | |