iterator_range.h revision dce4a407a24b04eebc6a376f8e62b41aaa7b071f
1//===- iterator_range.h - A range adaptor for iterators ---------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This provides a very simple, boring adaptor for a begin and end iterator
11/// into a range type. This should be used to build range views that work well
12/// with range based for loops and range based constructors.
13///
14/// Note that code here follows more standards-based coding conventions as it
15/// is mirroring proposed interfaces for standardization.
16///
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_ADT_ITERATOR_RANGE_H
20#define LLVM_ADT_ITERATOR_RANGE_H
21
22#include <utility>
23
24namespace llvm {
25
26/// \brief 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>
31class iterator_range {
32  IteratorT begin_iterator, end_iterator;
33
34public:
35  iterator_range() {}
36  iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
37      : begin_iterator(std::move(begin_iterator)),
38        end_iterator(std::move(end_iterator)) {}
39
40  IteratorT begin() const { return begin_iterator; }
41  IteratorT end() const { return end_iterator; }
42};
43
44/// \brief Convenience function for iterating over sub-ranges.
45///
46/// This provides a bit of syntactic sugar to make using sub-ranges
47/// in for loops a bit easier. Analogous to std::make_pair().
48template <class T> iterator_range<T> make_range(T x, T y) {
49  return iterator_range<T>(std::move(x), std::move(y));
50}
51}
52
53#endif
54