LLVM 23.0.0git
iterator_range.h
Go to the documentation of this file.
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
24namespace llvm {
25
26/// A range adaptor for a pair of iterators.
27///
28/// This just wraps two iterators into a range-compatible interface. Nothing
29/// fancy at all.
30template <typename IteratorT>
32 IteratorT begin_iterator, end_iterator;
33
34public:
35#if defined(__GNUC__) && \
36 (__GNUC__ == 7 || (__GNUC__ == 8 && __GNUC_MINOR__ < 4))
37 // Be careful no to break gcc-7 and gcc-8 < 8.4 on the mlir target.
38 // See https://github.com/llvm/llvm-project/issues/63843
39 template <typename Container>
40#else
41 template <
42 typename Container,
43 std::enable_if_t<std::is_constructible_v<
45 int> = 0>
46#endif
47 iterator_range(Container &&c)
48 : begin_iterator(adl_begin(c)), end_iterator(adl_end(c)) {
49 }
50 iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
51 : begin_iterator(std::move(begin_iterator)),
52 end_iterator(std::move(end_iterator)) {}
53
54 IteratorT begin() const { return begin_iterator; }
55 IteratorT end() const { return end_iterator; }
56 bool empty() const { return begin_iterator == end_iterator; }
57};
58
59template <typename Container>
60iterator_range(Container &&)
62
63/// Convenience function for iterating over sub-ranges.
64///
65/// This provides a bit of syntactic sugar to make using sub-ranges
66/// in for loops a bit easier. Analogous to std::make_pair().
67template <class T> iterator_range<T> make_range(T x, T y) {
68 return iterator_range<T>(std::move(x), std::move(y));
69}
70
71template <typename T> iterator_range<T> make_range(std::pair<T, T> p) {
72 return iterator_range<T>(std::move(p.first), std::move(p.second));
73}
74
75}
76
77#endif
#define T
A range adaptor for a pair of iterators.
iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
IteratorT end() const
IteratorT begin() const
iterator_range(Container &&c)
decltype(adl_begin(std::declval< RangeT & >())) IterOfRange
Definition ADL.h:126
This is an optimization pass for GlobalISel generic memory operations.
constexpr auto adl_begin(RangeT &&range) -> decltype(adl_detail::begin_impl(std::forward< RangeT >(range)))
Returns the begin iterator to range using std::begin and function found through Argument-Dependent Lo...
Definition ADL.h:78
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
constexpr auto adl_end(RangeT &&range) -> decltype(adl_detail::end_impl(std::forward< RangeT >(range)))
Returns the end iterator to range using std::end and functions found through Argument-Dependent Looku...
Definition ADL.h:86
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860