-
Notifications
You must be signed in to change notification settings - Fork 0
/
LookupIterator.hpp
104 lines (94 loc) · 3.79 KB
/
LookupIterator.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
96
97
98
99
100
101
102
103
104
// MIT License
//
// Copyright(c) 2018 Thomas Monkman
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef LOOKUPITERATOR_HPP
#define LOOKUPITERATOR_HPP
#include <iterator>
#include <type_traits>
#include <vector>
template<class Src, class Lookup>
struct lookup_itr {
private:
typedef decltype(begin(std::declval<Src>())) src_itr_t;
typedef decltype(*begin(std::declval<Src>())) src_itr_value;
typedef decltype(begin(std::declval<Lookup>())) lookup_itr_t;
Src _src;
Lookup _lookup;
lookup_itr_t _lookup_itr;
public:
//static_assert(std::is_same<typename src_itr_t::iterator_category, std::random_access_iterator_tag>::value, "Src must allow random access interation");
using value_type = typename src_itr_t::value_type;
using difference_type = typename lookup_itr_t::difference_type;
using pointer = typename src_itr_t::pointer;
using reference = typename src_itr_t::reference;
using iterator_category = typename lookup_itr_t::iterator_category;
struct End {};
lookup_itr(Src&& src, Lookup&& lookup) : _src(src), _lookup(lookup) {
using std::begin;
_lookup_itr = begin(_lookup);
}
lookup_itr(Src&& src, Lookup&& lookup, End) : _src(src), _lookup(lookup) {
using std::end;
_lookup_itr = end(_lookup);
}
lookup_itr operator++(int) /* postfix */ {
auto temp(*this);
++_lookup_itr;
return temp;
}
lookup_itr& operator++() /* prefix */ {
++_lookup_itr;
return *this;
}
lookup_itr operator--(int) /* postfix */ {
//static_assert(std::is_same<typename iterator_category, std::bidirectional_iterator_tag>::value, "LookUp does not support bidirectional iteration");
auto temp(*this);
--_lookup_itr;
return temp;
}
lookup_itr& operator--() /* prefix */ {
//static_assert(std::is_same<typename iterator_category, std::bidirectional_iterator_tag>::value, "LookUp does not support bidirectional iteration");
--_lookup_itr;
return *this;
}
decltype(_src[*_lookup_itr]) operator* () const { return _src[*_lookup_itr]; }
bool operator==(const lookup_itr& rhs) const { return _lookup_itr == rhs._lookup_itr; }
bool operator!=(const lookup_itr& rhs) const { return _lookup_itr != rhs._lookup_itr; }
};
template<class Src, class Lookup>
struct lookup_t {
lookup_t(Src&& src, Lookup&& lookup) :
src(std::forward<Src>(src)),
lookup(std::forward<Lookup>(lookup)) {}
Src src;
Lookup lookup;
constexpr lookup_itr<Src, Lookup> begin() const {
return lookup_itr<Src, Lookup>(Src(src), Lookup(lookup));
}
constexpr lookup_itr<Src, Lookup> end() const {
return lookup_itr<Src, Lookup>(Src(src), Lookup(lookup), (typename lookup_itr<Src, Lookup>::End()));
}
};
template<class Src, class Lookup = std::vector<std::size_t>>
lookup_t<Src, Lookup> lookup(Src&& src, Lookup&& lookup) {
return lookup_t<Src, Lookup>(std::forward<Src>(src), std::forward<Lookup>(lookup));
}
#endif