-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathiterator_helpers.hpp
95 lines (77 loc) · 2.21 KB
/
iterator_helpers.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
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
91
92
93
94
95
#pragma once
#include <assert.h>
template<typename Container>
struct const_forward_iterator_wrapper
{
constexpr const_forward_iterator_wrapper() noexcept = default;
constexpr const_forward_iterator_wrapper(const Container& container, const typename Container::const_iterator& iterator): _iterator(iterator), _container(&container) {}
const_forward_iterator_wrapper& operator=(const typename Container::const_iterator it)
{
assert(valid());
_iterator = it;
return *this;
}
const_forward_iterator_wrapper& operator++()
{
assert(valid());
++_iterator;
return *this;
}
const typename Container::value_type& operator*() const
{
assert(valid());
return *_iterator;
}
typename Container::value_type const * operator->() const
{
assert(valid());
return &(*_iterator);
}
[[nodiscard]] bool endReached() const
{
assert(valid());
return _iterator >= _container->cend();
}
[[nodiscard]] bool valid() const
{
return _container != nullptr;
}
[[nodiscard]] bool operator==(const typename Container::const_iterator other) const
{
assert(valid());
return _iterator == other;
}
[[nodiscard]] bool operator==(const const_forward_iterator_wrapper other) const
{
assert(valid() && _container == other._container);
return _iterator == other._iterator;
}
[[nodiscard]] bool operator!=(const typename Container::const_iterator other) const
{
return !(*this == other);
}
[[nodiscard]] bool operator!=(const const_forward_iterator_wrapper other) const
{
return !(*this == other);
}
typename Container::const_iterator _iterator;
const Container* _container = nullptr;
};
namespace forward_iterator_wrapper {
template<typename Container>
[[nodiscard]] const_forward_iterator_wrapper<Container> cbegin(const Container& c)
{
const_forward_iterator_wrapper<Container> it;
it._container = &c;
it._iterator = c.cbegin();
return it;
}
template<typename Container>
[[nodiscard]] const_forward_iterator_wrapper<Container> cend(const Container& c)
{
const_forward_iterator_wrapper<Container> it;
it._container = &c;
it._iterator = c.cend();
return it;
}
} // namespace forward_iterator_wrapper