-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimple_map.h
233 lines (195 loc) · 5.99 KB
/
simple_map.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef SIMPLE_MAP_H
#define SIMPLE_MAP_H
#include <vector>
#include <algorithm>
#include <cassert>
#include <iostream>
template <class key_t, class val_t>
class SimpleMapPair: public std::pair<key_t, val_t> {
public:
/// TODO should only be used during simplemap reallocation
/// how do I enforce this?
SimpleMapPair() {}
SimpleMapPair(key_t key, val_t val) : std::pair<key_t, val_t>(key, val) {}
};
template <class key_t, class val_t>
std::ostream &
operator << (std::ostream &out, const SimpleMapPair<key_t,val_t> &p) {
return
out << "("<<p.first<<","<<p.second<<")";
}
/**
* @brief Space saving replacement for map of trace arrows in rows
*
* Maintains lists of trace arrows in col-idx sorted lists, allowing
* log-time access and efficient traversal. Erasing elements is
* supported, but takes linear time. Still, this data structure seems
* to be a good compromise, since e.g. balanced trees or hashs require
* a lot of space.
*
* KeyCompare compares the element keys; e.g. use less for ascending lists and greater
* for descending lists over comparable keys
*/
template< class key_t, class val_t, class KeyCompare=std::less<key_t> >
class SimpleMap: std::vector<SimpleMapPair<key_t, val_t> > {
public:
typedef SimpleMapPair<key_t, val_t> key_val_t;
typedef std::vector<key_val_t> key_val_vec_t;
typedef typename key_val_vec_t::iterator iterator;
typedef typename key_val_vec_t::const_iterator const_iterator;
private:
class Compare {
public:
KeyCompare elem_comp_;
Compare(KeyCompare elem_comp) : elem_comp_(elem_comp)
{}
typedef SimpleMapPair<key_t, val_t> key_val_t;
bool
operator () (const key_val_t &x,
const key_t &y) const {
return elem_comp_(x.first, y);
}
};
Compare comp_;
iterator
binsearch (iterator first, iterator last, const key_t& key)
{
first = std::lower_bound(first, last, key, comp_);
if (first == last ||
comp_(key_val_t(key, first->second), first->first)) {
return last;
}
return first;
}
const_iterator
binsearch (const_iterator first, const_iterator last, const key_t& key) const
{
first = std::lower_bound(first, last, key, comp_);
if (first == last ||
comp_(key_val_t(key, first->second), first->first)) {
return last;
}
return first;
}
public:
SimpleMap(const KeyCompare &comp=KeyCompare()): comp_(comp) {}
iterator
begin() {
return key_val_vec_t::begin();
}
const_iterator
begin() const {
return key_val_vec_t::begin();
}
iterator
end() {
return key_val_vec_t::end();
}
const_iterator
end() const {
return key_val_vec_t::end();
}
const_iterator
find(const key_t &key) const {
auto it= binsearch(key_val_vec_t::begin(), key_val_vec_t::end(), key);
assert(it == key_val_vec_t::end() || it->first == key);
return it;
}
iterator
find(const key_t &key) {
auto it= binsearch(key_val_vec_t::begin(), key_val_vec_t::end(), key);
assert(it == key_val_vec_t::end() || it->first == key);
return it;
}
bool
exists(const key_t &key) const {
return find(key) != key_val_vec_t::end();
}
// /**
// * @brief insert maintaining order of keys
// * @param key
// * @param val
// *
// * inserts into ensure vector remains sorted;
// * inserting already contained keys is illegal
// * @note this is an expensive operation (linear time in size of map)
// *
// */
// void
// insert( const key_t key, const val_t &val) {
// //std::cerr << "SimpleMap::add (" << key <<" "<< val <<")"<< std::endl;
// auto it = std::lower_bound(key_val_vec_t::begin(), key_val_vec_t::end(),
// key, comp_);
// assert(it==key_val_vec_t::end() || (! (it->first == key)));
// key_val_vec_t::insert(it, key_val_t(key,val));
// }
val_t &
operator [] (const key_t key) {
auto it = std::lower_bound(key_val_vec_t::begin(), key_val_vec_t::end(),
key, comp_);
if (it == key_val_vec_t::end() || (! (it->first == key)) ) {
it = key_val_vec_t::insert(it, key_val_t(key,val_t()));
}
return it->second;
}
const key_t last_key() {
return key_val_vec_t::operator[](size()-1).first;
}
/**
* @brief push in ascending order of keys
* @param key
* @param val
*
* successive push_sorted must be in ascending order of the key type
*/
void
push_sorted( const key_t &key, const val_t &val ) {
//printf("size:%ld key:%d last:%d\n",size(),key, size()>0?last_key():-1);
if(!( size()==0 || comp_.elem_comp_( last_key(), key ) ) ) {
std::cout << last_key() << " " << key << std::endl;
}
assert( size()==0 || comp_.elem_comp_( last_key(), key ) );
key_val_vec_t::push_back(key_val_t(key, val));
}
/*
void
assert_ascending() {
iterator it = key_val_vec_t::begin();
key_t prev_key = it->first;
++it;
while (it != key_val_vec_t::end()) {
if (!(it->first > prev_key)) {
printf("%d not > %d\n",it->first, prev_key);
}
assert(it->first > prev_key);
prev_key = it->first;
++it;
}
}
*/
void
erase(key_t key) {
assert(exists(key ));
erase(find(key));
}
void
erase(iterator it) {
assert (it != key_val_vec_t::end());
key_val_vec_t::erase(it);
}
size_t
size() const {
return key_val_vec_t::size();
}
size_t
capacity() const {
return key_val_vec_t::capacity();
}
void
reallocate() {
key_val_vec_t vec(size());
copy(key_val_vec_t::begin(),key_val_vec_t::end(),vec.begin());
vec.swap(*this);
}
};
#endif //SIMPLE_MAP_HH