Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 5c47b58

Browse files
committed
Adding range-based STL-like helper APIs. llvm::distance() is the range version of std::distance. llvm::copy is the range version of std::copy.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203354 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent f089b84 commit 5c47b58

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

include/llvm/ADT/iterator_range.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323

2424
namespace llvm {
2525

26+
template <typename Range>
27+
struct range_traits {
28+
typedef typename Range::difference_type difference_type;
29+
};
30+
2631
/// \brief A range adaptor for a pair of iterators.
2732
///
2833
/// This just wraps two iterators into a range-compatible interface. Nothing
@@ -32,6 +37,10 @@ class iterator_range {
3237
IteratorT begin_iterator, end_iterator;
3338

3439
public:
40+
// FIXME: We should be using iterator_traits to determine the
41+
// difference_type, but most of our iterators do not expose anything like it.
42+
typedef int difference_type;
43+
3544
iterator_range() {}
3645
iterator_range(IteratorT begin_iterator, IteratorT end_iterator)
3746
: begin_iterator(std::move(begin_iterator)),
@@ -41,6 +50,19 @@ class iterator_range {
4150
IteratorT end() const { return end_iterator; }
4251
};
4352

53+
/// \brief Determine the distance between the end() and begin() iterators of
54+
/// a range. Analogous to std::distance().
55+
template <class Range>
56+
typename range_traits<Range>::difference_type distance(Range R) {
57+
return std::distance(R.begin(), R.end());
58+
}
59+
60+
/// \brief Copies members of a range into the output iterator provided.
61+
/// Analogous to std::copy.
62+
template <class Range, class OutputIterator>
63+
OutputIterator copy(Range In, OutputIterator Result) {
64+
return std::copy(In.begin(), In.end(), Result);
65+
}
4466
}
4567

4668
#endif

0 commit comments

Comments
 (0)