-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathiterator_range.hpp
56 lines (47 loc) · 1.59 KB
/
iterator_range.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef ITERATOR_RANGE_HPP
#define ITERATOR_RANGE_HPP
#include <iterator>
#include <type_traits>
template<typename Iterator>
struct is_const_iterator
{
static const bool value = std::is_const<
typename std::remove_reference<
typename std::iterator_traits<
Iterator
>::reference
>::type
>::value;
};
template<bool const_iterator_range, typename Iterator>
class iterator_range_impl
{
public:
iterator_range_impl(Iterator begin, Iterator end) : begin_(begin), end_(end) {}
Iterator begin() const {return begin_;}
Iterator end() const {return end_;}
typename std::iterator_traits<Iterator>::difference_type const size(){return end_ - begin_;}
using const_iterator = Iterator;
private:
Iterator begin_, end_;
};
template<typename Iterator>
class iterator_range_impl<false, Iterator>
{
public:
iterator_range_impl(Iterator begin, Iterator end) : begin_(begin), end_(end) {}
Iterator begin() const {return begin_;}
Iterator end() const {return end_;}
typename std::iterator_traits<Iterator>::difference_type const size(){return end_ - begin_;}
using iterator = Iterator;
private:
Iterator begin_, end_;
};
template<typename Iterator>
class iterator_range : public iterator_range_impl<is_const_iterator<Iterator>::value, Iterator>
{
public:
typedef iterator_range_impl<is_const_iterator<Iterator>::value, Iterator> base;
iterator_range(Iterator begin, Iterator end) : base(begin, end) {}
};
#endif