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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/******************************************************************************
*
* Copyright (C) 1997-2020 by Dimitri van Heesch.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
* Documents produced by Doxygen are derivative works derived from the
* input used in their production; they are not affected by this license.
*
*/
#ifndef LINKEDMAP_H
#define LINKEDMAP_H
#include <unordered_map>
#include <vector>
#include <memory>
#include <string>
#include <algorithm>
#include <cctype>
#include "qcstring.h"
//! @brief Container class representing a vector of objects with keys.
//! @details Objects can efficiently be looked up given the key.
//! Objects are owned by the container.
//! When adding objects the order of addition is kept, and used while iterating.
template<class T, class Hash = std::hash<std::string>,
class KeyEqual = std::equal_to<std::string>,
class Map = std::unordered_map<std::string,T*,Hash,KeyEqual > >
class LinkedMap
{
public:
using Ptr = std::unique_ptr<T>;
using Vec = std::vector<Ptr>;
using iterator = typename Vec::iterator;
using const_iterator = typename Vec::const_iterator;
using reverse_iterator = typename Vec::reverse_iterator;
using const_reverse_iterator = typename Vec::const_reverse_iterator;
//! Find an object given the key.
//! Returns a pointer to the element if found or nullptr if it is not found.
const T *find(const std::string &key) const
{
auto it = m_lookup.find(key);
return it!=m_lookup.end() ? it->second : nullptr;
}
//! Find an object given the key.
//! Returns a pointer to the element if found or nullptr if it is not found.
const T *find(const QCString &key) const
{
auto it = m_lookup.find(key.str());
return it!=m_lookup.end() ? it->second : nullptr;
}
//! Find an object given the key.
//! Returns a pointer to the element if found or nullptr if it is not found.
const T *find(const char *key) const
{
return find(std::string(key ? key : ""));
}
//! A non-const wrapper for find() const
T* find(const char *key)
{
return const_cast<T*>(static_cast<const LinkedMap&>(*this).find(key));
}
//! A non-const wrapper for find() const
T* find(const QCString &key)
{
return const_cast<T*>(static_cast<const LinkedMap&>(*this).find(key));
}
//! A non-const wrapper for find() const
T* find(const std::string &key)
{
return const_cast<T*>(static_cast<const LinkedMap&>(*this).find(key));
}
//! Adds a new object to the ordered vector if it was not added already.
//! Return a non-owning pointer to the newly added object, or to the existing object if
//! it was already inserted before under the given key.
template<class...Args>
T *add(const char *k, Args&&... args)
{
T *result = find(k);
if (result==nullptr)
{
std::string key(k ? k : "");
Ptr ptr = std::make_unique<T>(QCString(k),std::forward<Args>(args)...);
result = ptr.get();
m_lookup.insert({key,result});
m_entries.push_back(std::move(ptr));
}
return result;
}
template<class...Args>
T *add(const QCString &k, Args&&... args)
{
std::string key = k.str();
T *result = find(key);
if (result==nullptr)
{
Ptr ptr = std::make_unique<T>(k,std::forward<Args>(args)...);
result = ptr.get();
m_lookup.insert({key,result});
m_entries.push_back(std::move(ptr));
}
return result;
}
//! Adds an existing object to the ordered vector (unless another object was already
//! added under the same key). Ownership is transferred.
//! Return a non-owning pointer to the newly added object, or to the existing object if
//! it was already inserted before under the given key.
T *add(const char *k, Ptr &&ptr)
{
T *result = find(k);
if (result==nullptr)
{
std::string key(k ? k : "");
result = ptr.get();
m_lookup.insert({key,result});
m_entries.push_back(std::move(ptr));
}
return result;
}
T *add(const QCString &k, Ptr &&ptr)
{
std::string key = k.str();
T *result = find(key);
if (result==nullptr)
{
result = ptr.get();
m_lookup.insert({key,result});
m_entries.push_back(std::move(ptr));
}
return result;
}
//! Prepends a new object to the ordered vector if it was not added already.
//! Return a non-owning pointer to the newly added object, or to the existing object if
//! it was already inserted before under the given key.
template<class...Args>
T *prepend(const char *k, Args&&... args)
{
T *result = find(k);
if (result==nullptr)
{
std::string key(k ? k : "");
Ptr ptr = std::make_unique<T>(key.c_str(),std::forward<Args>(args)...);
result = ptr.get();
m_lookup.insert({key,result});
m_entries.push_front(std::move(ptr));
}
return result;
}
template<class...Args>
T *prepend(const QCString &key, Args&&... args)
{
T *result = find(key);
if (result==nullptr)
{
Ptr ptr = std::make_unique<T>(key,std::forward<Args>(args)...);
result = ptr.get();
m_lookup.insert({key.str(),result});
m_entries.push_front(std::move(ptr));
}
return result;
}
//! Removes an object from the container and deletes it.
//! Returns true if the object was deleted or false it is was not found.
bool del(const QCString &key)
{
auto it = m_lookup.find(key.str());
if (it!=m_lookup.end())
{
auto vecit = std::find_if(m_entries.begin(),m_entries.end(),[obj=it->second](auto &el) { return el.get()==obj; });
if (vecit!=m_entries.end()) // should always be true
{
m_entries.erase(vecit);
m_lookup.erase(it);
return true;
}
}
return false;
}
Ptr &operator[](size_t pos) { return m_entries[pos]; }
const Ptr &operator[](size_t pos) const { return m_entries[pos]; }
iterator begin() { return m_entries.begin(); }
iterator end() { return m_entries.end(); }
const_iterator begin() const { return m_entries.cbegin(); }
const_iterator end() const { return m_entries.cend(); }
reverse_iterator rbegin() { return m_entries.rbegin(); }
reverse_iterator rend() { return m_entries.rend(); }
const_reverse_iterator rbegin() const { return m_entries.crbegin(); }
const_reverse_iterator rend() const { return m_entries.crend(); }
bool empty() const { return m_entries.empty(); }
size_t size() const { return m_entries.size(); }
void clear()
{
m_entries.clear();
m_lookup.clear();
}
private:
Map m_lookup;
Vec m_entries;
};
//! @brief Container class representing a vector of objects with keys.
//! @details Objects can be efficiently be looked up given the key.
//! Objects are \e not owned by the container, the container will only hold references.
//! When adding objects the order of addition is kept, and used while iterating.
template<class T, class Hash = std::hash<std::string>,
class KeyEqual = std::equal_to<std::string>,
class Map = std::unordered_map<std::string,T*,Hash,KeyEqual > >
class LinkedRefMap
{
public:
using Ptr = T*;
using Vec = std::vector<Ptr>;
using iterator = typename Vec::iterator;
using const_iterator = typename Vec::const_iterator;
using reverse_iterator = typename Vec::reverse_iterator;
using const_reverse_iterator = typename Vec::const_reverse_iterator;
//! find an object given the key.
//! Returns a pointer to the object if found or nullptr if it is not found.
const T *find(const std::string &key) const
{
auto it = m_lookup.find(key);
return it!=m_lookup.end() ? it->second : nullptr;
}
//! find an object given the key.
//! Returns a pointer to the object if found or nullptr if it is not found.
const T *find(const QCString &key) const
{
auto it = m_lookup.find(key.str());
return it!=m_lookup.end() ? it->second : nullptr;
}
//! find an object given the key.
//! Returns a pointer to the object if found or nullptr if it is not found.
const T *find(const char *key) const
{
return find(std::string(key ? key : ""));
}
//! non-const wrapper for find() const
T* find(const char *key)
{
return const_cast<T*>(static_cast<const LinkedRefMap&>(*this).find(key));
}
T* find(const QCString &key)
{
return const_cast<T*>(static_cast<const LinkedRefMap&>(*this).find(key));
}
//! non-const wrapper for find() const
T* find(const std::string &key)
{
return const_cast<T*>(static_cast<const LinkedRefMap&>(*this).find(key));
}
//! Adds an object reference to the ordered vector if it was not added already.
//! Return true if the reference was added, and false if an object with the same key
//! was already added before
bool add(const char *k, T* obj)
{
if (find(k)==nullptr) // new element
{
std::string key(k ? k : "");
m_lookup.insert({key,obj});
m_entries.push_back(obj);
return true;
}
else // already existing, don't add
{
return false;
}
}
bool add(const QCString &k, T* obj)
{
std::string key = k.str();
if (find(key)==nullptr) // new element
{
m_lookup.insert({key,obj});
m_entries.push_back(obj);
return true;
}
else // already existing, don't add
{
return false;
}
}
//! Prepends an object reference to the ordered vector if it was not added already.
//! Return true if the reference was added, and false if an object with the same key
//! was already added before
bool prepend(const char *k, T* obj)
{
if (find(k)==nullptr) // new element
{
std::string key(k ? k : "");
m_lookup.insert({key,obj});
m_entries.insert(m_entries.begin(),obj);
return true;
}
else // already existing, don't add
{
return false;
}
}
bool prepend(const QCString &key, T* obj)
{
if (find(key)==nullptr) // new element
{
m_lookup.insert({key.str(),obj});
m_entries.insert(m_entries.begin(),obj);
return true;
}
else // already existing, don't add
{
return false;
}
}
//! Removes an object from the container and deletes it.
//! Returns true if the object was deleted or false it is was not found.
bool del(const QCString &key)
{
auto it = m_lookup.find(key.str());
if (it!=m_lookup.end())
{
auto vecit = std::find_if(m_entries.begin(),m_entries.end(),[obj=it->second](auto &el) { return el.get()==obj; });
if (vecit!=m_entries.end()) // should always be true
{
m_entries.erase(vecit);
m_lookup.erase(it);
return true;
}
}
return false;
}
Ptr &operator[](size_t pos) { return m_entries[pos]; }
const Ptr &operator[](size_t pos) const { return m_entries[pos]; }
iterator begin() { return m_entries.begin(); }
iterator end() { return m_entries.end(); }
const_iterator begin() const { return m_entries.cbegin(); }
const_iterator end() const { return m_entries.cend(); }
reverse_iterator rbegin() { return m_entries.rbegin(); }
reverse_iterator rend() { return m_entries.rend(); }
const_reverse_iterator rbegin() const { return m_entries.crbegin(); }
const_reverse_iterator rend() const { return m_entries.crend(); }
bool empty() const { return m_entries.empty(); }
size_t size() const { return m_entries.size(); }
void clear()
{
m_entries.clear();
m_lookup.clear();
}
private:
Map m_lookup;
Vec m_entries;
};
#endif
|