LLVM  10.0.0svn
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 <iterator>
22 #include <utility>
23 
24 namespace 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.
30 template <typename IteratorT>
32  IteratorT begin_iterator, end_iterator;
33 
34 public:
35  //TODO: Add SFINAE to test that the Container's iterators match the range's
36  // iterators.
37  template <typename Container>
38  iterator_range(Container &&c)
39  //TODO: Consider ADL/non-member begin/end calls.
40  : begin_iterator(c.begin()), end_iterator(c.end()) {}
41  iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
42  : begin_iterator(std::move(begin_iterator)),
43  end_iterator(std::move(end_iterator)) {}
44 
45  IteratorT begin() const { return begin_iterator; }
46  IteratorT end() const { return end_iterator; }
47 };
48 
49 /// Convenience function for iterating over sub-ranges.
50 ///
51 /// This provides a bit of syntactic sugar to make using sub-ranges
52 /// in for loops a bit easier. Analogous to std::make_pair().
53 template <class T> iterator_range<T> make_range(T x, T y) {
54  return iterator_range<T>(std::move(x), std::move(y));
55 }
56 
57 template <typename T> iterator_range<T> make_range(std::pair<T, T> p) {
58  return iterator_range<T>(std::move(p.first), std::move(p.second));
59 }
60 
61 template <typename T>
63  int n) {
64  return make_range(std::next(adl_begin(t), n), adl_end(t));
65 }
66 }
67 
68 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Definition: BitVector.h:937
auto adl_begin(ContainerTy &&container) -> decltype(adl_detail::adl_begin(std::forward< ContainerTy >(container)))
Definition: STLExtras.h:178
iterator_range< decltype(adl_begin(std::declval< T >)))> drop_begin(T &&t, int n)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
iterator_range(Container &&c)
A range adaptor for a pair of iterators.
iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
auto adl_end(ContainerTy &&container) -> decltype(adl_detail::adl_end(std::forward< ContainerTy >(container)))
Definition: STLExtras.h:184
IteratorT begin() const
IteratorT end() const