-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColumnIterator.h
90 lines (81 loc) · 2.92 KB
/
ColumnIterator.h
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "adl.h"
#include <cstddef>
#include <type_traits>
#include <iterator>
template<typename CIt>
class ColumnIterator
{
template<typename...>
struct void_type
{
using type = void;
};
template<typename T, typename = void>
struct ValueType
{
using type = typename std::remove_reference<
decltype(std::declval<T>()[0])>::type;
};
template<typename T>
struct ValueType<T, typename void_type<typename T::value_type>::type>
{
using type = typename T::value_type;
};
public:
using difference_type =
typename std::iterator_traits<CIt>::difference_type;
using value_type = typename ValueType<
typename std::iterator_traits<CIt>::value_type>::type;
using pointer = value_type*;
using reference = decltype((*std::declval<CIt>())[0]);
using iterator_category =
typename std::iterator_traits<CIt>::iterator_category;
ColumnIterator(CIt it, std::size_t pos) : it(it), pos(pos) {}
reference operator*() const { return (*it)[pos]; }
pointer operator->() const { return &(*it)[pos]; }
reference operator[](difference_type n) const { return it[n][pos]; }
ColumnIterator& operator++() { ++it; return *this; }
ColumnIterator operator++(int) { auto ret = *this; ++*this; return ret; }
ColumnIterator& operator--() { --it; return *this; }
ColumnIterator operator--(int) { auto ret = *this; --*this; return ret; }
ColumnIterator& operator+=(difference_type n) { it += n; return *this; }
ColumnIterator operator+(difference_type n) const
{ auto ret = *this; return ret += n; }
ColumnIterator& operator-=(difference_type n) { it -= n; return *this; }
ColumnIterator operator-(difference_type n) const
{ auto ret = *this; return ret -= n; }
difference_type operator-(const ColumnIterator& other) const
{ return it - other.it; }
bool operator==(const ColumnIterator& other) const
{ return it == other.it; }
bool operator!=(const ColumnIterator& other) const
{ return it != other.it; }
bool operator<(const ColumnIterator& other) const { return it < other.it; }
bool operator>(const ColumnIterator& other) const { return it > other.it; }
bool operator>=(const ColumnIterator& other) const
{ return it >= other.it; }
bool operator<=(const ColumnIterator& other) const
{ return it <= other.it; }
private:
CIt it;
std::size_t pos;
};
template<typename CIt>
ColumnIterator<CIt> operator+(
typename ColumnIterator<CIt>::difference_type n,
const ColumnIterator<CIt>& it)
{
return it + n;
}
template<typename C>
auto begin_column(C&& c, std::size_t pos)
-> ColumnIterator<decltype(adl_begin(std::forward<C>(c)))>
{
return {adl_begin(std::forward<C>(c)), pos};
}
template<typename C>
auto end_column(C&& c, std::size_t pos)
-> ColumnIterator<decltype(adl_end(std::forward<C>(c)))>
{
return {adl_end(std::forward<C>(c)), pos};
}