forked from LLRCMS/KLUBAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathordered_map.h
212 lines (164 loc) · 5.15 KB
/
ordered_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
#ifndef UNORDEREDMAP_H
#define UNORDEREDMAP_H
/*
** class: ordered_map
** author: L. Cadamuro (LLR)
** date: 25/05/2016
** description: container that behaves like std::map but maintains the order of insertion of the objects
** templates the type of key and value. NOTE: key must be printable, key and values must be copiable
*/
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <iostream>
// using namespace std;
template <class Tkey, class Tvalue> class ordered_map
{
public:
ordered_map();
ordered_map(const ordered_map & omap);
~ordered_map();
Tvalue& at(Tkey key);
Tvalue& at(int idx);
const Tvalue& at (Tkey key) const;
const Tvalue& at (int idx) const;
Tvalue& back();
const Tvalue& back() const;
void append(Tkey key, Tvalue value);
void insert(int idx, Tkey key, Tvalue value);
void remove(Tkey key);
void remove(int idx);
size_t size() const;
Tkey key(int idx) const;
bool has_key(Tkey key);
private:
std::vector<Tkey> keys_;
std::vector<Tvalue> values_;
std::unordered_map <Tkey, int> remap_;
void remap();
};
// ----------------------------------------------------------------
// ------------------ IMPLEMENTATION ------------------------------
template <class Tkey, class Tvalue>
ordered_map<Tkey, Tvalue>::ordered_map()
{}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
ordered_map<Tkey, Tvalue>::ordered_map(const ordered_map & omap)
{
keys_ = omap.keys_;
values_ = omap.values_;
remap_ = omap.remap_;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
ordered_map<Tkey, Tvalue>::~ordered_map()
{}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
void ordered_map<Tkey, Tvalue>::append(Tkey key, Tvalue value)
{
if (find(keys_.begin(), keys_.end(), key) != keys_.end())
{
std::cerr << "ordered_map::append - warning: key " << key << " already existing, exiting" << std::endl;
return;
}
keys_.push_back(key);
values_.push_back(value);
remap_[key] = keys_.size()-1;
return;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
void ordered_map<Tkey, Tvalue>::insert(int idx, Tkey key, Tvalue value)
{
auto itrKey = keys_.begin() + idx;
auto itrVal = values_.begin() + idx;
keys_.insert(itrKey, key);
values_.insert(itrVal, value);
remap();
return;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
void ordered_map<Tkey, Tvalue>::remove(Tkey key)
{
int offset = remap_[key];
keys_.erase(keys_.begin()+offset);
values_.erase(values_.begin()+offset);
remap();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
void ordered_map<Tkey, Tvalue>::remove(int idx)
{
keys_.erase(keys_.begin()+idx);
values_.erase(values_.begin()+idx);
remap();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
size_t ordered_map<Tkey, Tvalue>::size() const
{
return values_.size();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
Tkey ordered_map<Tkey, Tvalue>::key(int idx) const
{
return keys_.at(idx);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
Tvalue& ordered_map<Tkey, Tvalue>::at(Tkey key)
{
return values_.at(remap_.at(key));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
Tvalue& ordered_map<Tkey, Tvalue>::at(int idx)
{
return values_.at(idx);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
const Tvalue& ordered_map<Tkey, Tvalue>::at(Tkey key) const
{
return values_.at(remap_.at(key));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
const Tvalue& ordered_map<Tkey, Tvalue>::at(int idx) const
{
return values_.at(idx);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
Tvalue& ordered_map<Tkey, Tvalue>::back()
{
return values_.at(size() - 1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
const Tvalue& ordered_map<Tkey, Tvalue>::back() const
{
return values_.at(size() - 1);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
void ordered_map<Tkey, Tvalue>::remap()
{
remap_.clear();
for (size_t idx = 0; idx < keys_.size(); idx++)
remap_[keys_.at(idx)] = (int) idx;
return;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <class Tkey, class Tvalue>
bool ordered_map<Tkey, Tvalue>::has_key(Tkey key)
{
return find (keys_.begin(), keys_.end(), key) != keys_.end() ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#endif
// #include "ordered_map.cc"