1 //===- iterator_range.h - A range adaptor for iterators ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 /// Note that code here follows more standards-based coding conventions as it
15 /// is mirroring proposed interfaces for standardization.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_ADT_ITERATOR_RANGE_H
20 #define LLVM_ADT_ITERATOR_RANGE_H
28 template <typename Range>
30 typedef typename Range::difference_type difference_type;
33 /// \brief A range adaptor for a pair of iterators.
35 /// This just wraps two iterators into a range-compatible interface. Nothing
37 template <typename IteratorT>
38 class iterator_range {
39 IteratorT begin_iterator, end_iterator;
42 // FIXME: We should be using iterator_traits to determine the
43 // difference_type, but most of our iterators do not expose anything like it.
44 typedef int difference_type;
47 iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
48 : begin_iterator(std::move(begin_iterator)),
49 end_iterator(std::move(end_iterator)) {}
51 IteratorT begin() const { return begin_iterator; }
52 IteratorT end() const { return end_iterator; }
55 /// \brief Determine the distance between the end() and begin() iterators of
56 /// a range. Analogous to std::distance().
57 template <class Range>
58 typename range_traits<Range>::difference_type distance(Range R) {
59 return std::distance(R.begin(), R.end());
62 /// \brief Copies members of a range into the output iterator provided.
63 /// Analogous to std::copy.
64 template <class Range, class OutputIterator>
65 OutputIterator copy(Range In, OutputIterator Result) {
66 return std::copy(In.begin(), In.end(), Result);