-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathhash_list.h
172 lines (164 loc) · 5.17 KB
/
hash_list.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* This file is a part of KNOSSOS.
*
* (C) Copyright 2007-2018
* Max-Planck-Gesellschaft zur Foerderung der Wissenschaften e.V.
*
* KNOSSOS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 of
* the License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* For further information, visit https://knossos.app
* or contact [email protected]
*/
#pragma once
#include <functional>
#include <iterator>
#include <list>
#include <unordered_map>
#include <utility>
template<typename T>
class hash_list {
friend class iterator;
friend class reference;
class reference;
class iterator;
std::list<T> data;
std::unordered_map<T, typename decltype(data)::iterator> positions;
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
// using reference = reference;
using const_reference = const T &;
// using iterator = iterator;
using const_iterator = typename decltype(data)::const_iterator;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
public:
const T & back() const {
return data.back();
}
reference back() {
return reference(*this, data.back());
}
const_iterator cbegin() const {
return data.cbegin();
}
const_iterator begin() const {
return data.cbegin();
}
iterator begin() {
return iterator(*this, std::begin(data));
}
void clear() {
data.clear();
positions.clear();
}
template<typename... Args>
void emplace_back(Args &&... args) {
data.emplace_back(std::forward<Args...>(args...));
auto it = std::prev(std::end(data));
positions.emplace(std::piecewise_construct, std::forward_as_tuple(*it), std::forward_as_tuple(it));
}
template<typename... Args>
void emplace_front(Args &&... args) {
data.emplace_front(std::forward<Args...>(args...));
auto it = std::begin(data);
positions.emplace(std::piecewise_construct, std::forward_as_tuple(*it), std::forward_as_tuple(it));
}
bool empty() const noexcept {
return data.empty();
}
const_iterator cend() const {
return data.cend();
}
const_iterator end() const {
return data.cend();
}
iterator end() {
return iterator(*this, std::end(data));
}
void erase(const T & expired) {
auto it = positions.find(expired);
if (it != std::end(positions)) {
data.erase(it->second);
positions.erase(it);
}
}
const T & front() const {
return data.front();
}
reference front() {
return reference(*this, data.front());
}
void replace(const T & oldValue, const T & newValue) {
auto it = positions.find(oldValue);
if (it != std::end(positions)) {
*it->second = newValue;//replace value
//new mapping to unchanged position
positions.emplace(std::piecewise_construct, std::forward_as_tuple(newValue), std::forward_as_tuple(it->second));
positions.erase(oldValue);//remove old mapping
}
}
std::size_t size() const {
return data.size();
}
};
template<typename T>
class hash_list<T>::reference {
std::reference_wrapper<hash_list> owner;
std::reference_wrapper<T> value;
friend class hash_list;
reference(hash_list & owner, T & value) : owner{owner}, value{value} {}
public:
reference(const reference &) = delete;
reference & operator=(const reference &) = delete;
reference(reference &&) = default;
reference & operator=(reference &&) = default;
reference & operator=(const T & newValue) {
owner.replace(value, newValue);
assert(newValue == value && *owner.positions[value]->second == value);
return *this;
}
operator T() const {
return value;
}
};
template<typename T>
class hash_list<T>::iterator {
hash_list & owner;
typename decltype(hash_list<T>::data)::iterator it;
friend class hash_list;
iterator(decltype(owner) & owner, const decltype(it) & it) : owner{owner}, it{it} {}
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using reference = hash_list<T>::reference;
using pointer = reference*;
bool operator!=(const iterator & other) {
return it != other.it;
}
reference operator*() {
return reference(owner, *it);
}
T operator*() const {
return *it;
}
iterator & operator++() {
it = std::next(it);
return *this;
}
iterator & operator--() {
it = std::prev(it);
return *this;
}
};