librcsb-core-wrapper 1.005
mapped_ptr_vector.C
Go to the documentation of this file.
1//$$FILE$$
2//$$VERSION$$
3//$$DATE$$
4//$$LICENSE$$
5
6
7#ifndef MAPPED_PTR_VECTOR_C
8#define MAPPED_PTR_VECTOR_C
9
10
11#include <stdexcept>
12#include <string>
13#include <vector>
14
15#include <rcsb/Exceptions.h>
17
18
19using std::out_of_range;
20using std::string;
21using std::vector;
22using std::pair;
23using std::make_pair;
24
25
26template <typename T, typename StringCompareT>
32
33
34template <typename T, typename StringCompareT>
36 const StringCompareT& cmp) : _index(cmp)
37{
38
39
40}
41
42
43template <typename T, typename StringCompareT>
45 const mapped_ptr_vector& inMappedPtrVector)
46{
47
48 _vector = inMappedPtrVector._vector;
49 _index = inMappedPtrVector._index;
50 _currentName = inMappedPtrVector._currentName;
51 _currentIndices = inMappedPtrVector._currentIndices;
52
54
56template <typename T, typename StringCompareT>
58{
60 clear();
62}
63
64
65template <typename T, typename StringCompareT>
67{
69 _vector = inMappedPtrVector._vector;
70 _index = inMappedPtrVector._index;
71 _currentName = inMappedPtrVector._currentName;
72 _currentIndices = inMappedPtrVector._currentIndices;
73
75
77template <typename T, typename StringCompareT>
80
81 return(_vector.size());
83}
84
86template <typename T, typename StringCompareT>
89
90 return(_vector.empty());
93
94
95template <typename T, typename StringCompareT>
97{
98
99 _vector.clear();
100
101 _index.clear();
102
103 _currentName.clear();
104
105}
106
107
108template <typename T, typename StringCompareT>
110 const mapped_ptr_vector& inMappedPtrVector)
111{
112
113 return(_vector == inMappedPtrVector._vector);
114
115}
116
117
118template <typename T, typename StringCompareT>
120 const unsigned int fileIndex)
121{
122
123 if (inP == NULL)
124 {
125 throw EmptyValueException("NULL vector",
126 "mapped_ptr_vector::push_back");
127 }
128
129 _vector.push_back(inP);
130
131 typename tIndex::value_type valuePair(inP->GetName(),
132 make_pair(_vector.size() - 1, fileIndex));
133
134 _index.insert(valuePair);
135
136 _currentName = inP->GetName();
137 _currentIndices = make_pair(_vector.size() - 1, fileIndex);
138
139}
140
141
142template <typename T, typename StringCompareT>
144 const unsigned int fileIndex)
145{
146
147 _vector.push_back(NULL);
148
149 typename tIndex::value_type valuePair(name,
150 make_pair(_vector.size() - 1, fileIndex));
151
152 _index.insert(valuePair);
153
154 _currentName = name;
155 _currentIndices = make_pair(_vector.size() - 1, fileIndex);
156}
157
158
159template <typename T, typename StringCompareT>
161 const vector<string>& names, const vector<unsigned int>& fileIndices)
162{
163
164 if (names.size() != fileIndices.size())
165 {
166 throw out_of_range("Different sizes of names and fileIndices in"\
167 " mapped_ptr_vector::push_back");
168 }
169
170 for (unsigned int nameI = 0; nameI < names.size(); ++nameI)
171 {
172 push_back(names[nameI], fileIndices[nameI]);
173 }
174
175}
176
177
178template <typename T, typename StringCompareT>
179void mapped_ptr_vector<T, StringCompareT>::push_back(const vector<string>& names)
180{
181
182 for (unsigned int nameI = 0; nameI < names.size(); ++nameI)
183 {
184 push_back(names[nameI], 0);
185 }
186
187}
188
189
190template <typename T, typename StringCompareT>
192{
193
194 if (inP == NULL)
195 {
196 throw EmptyValueException("NULL vector",
197 "mapped_ptr_vector::set");
198 }
199
200 pair<unsigned int, unsigned int> indices = get_indices(inP->GetName());
201
202 if (indices.first == _vector.size())
203 {
204 throw NotFoundException("Object not found",
205 "mapped_ptr_vector::set");
206 }
207
208 _vector[indices.first] = inP;
209
210}
211
212
213template <typename T, typename StringCompareT>
215{
216
217 if (index >= _vector.size())
218 {
219 throw out_of_range("Invalid index in"\
220 " mapped_ptr_vector::operator[]");
221 }
222
223 return((T&)(*(_vector[index])));
224
225}
226
227
228template <typename T, typename StringCompareT>
230{
231
232 pair<unsigned int, unsigned int> indices = get_indices(name);
233
234 if (indices.first == _vector.size())
235 {
236 throw NotFoundException("Object not found",
237 "mapped_ptr_vector::operator[]");
238 }
239
240 return((T&)(*(_vector[indices.first])));
241
242}
243
244
245template <typename T, typename StringCompareT>
246unsigned int mapped_ptr_vector<T, StringCompareT>::find(const string& name)
247{
248
249 pair<unsigned int, unsigned int> indices = get_indices(name);
250
251 return(indices.first);
252
253}
254
255
256template <typename T, typename StringCompareT>
258 const string& newName)
259{
260
261 pair<unsigned int, unsigned int> indices = get_indices(oldName);
262
263 if (indices.first == _vector.size())
264 {
265 throw NotFoundException("Object not found",
266 "mapped_ptr_vector::rename");
267 }
268
269 // Erase it from the map as it is about to change
270 _index.erase(oldName);
271
272 typename tIndex::value_type valuePair(newName, indices);
273
274 _index.insert(valuePair);
275
276 _vector[indices.first]->SetName(newName);
277
278 typename tIndex::key_compare keyComp = _index.key_comp();
279
280 if (is_equal(_currentName, oldName, keyComp))
281 {
282 _currentName = newName;
283 }
284
285}
286
287
288template <typename T, typename StringCompareT>
290{
291
292 pair<unsigned int, unsigned int> indices = get_indices(name);
293
294 if (indices.first == _vector.size())
295 {
296 throw NotFoundException("Object not found",
297 "mapped_ptr_vector::erase");
298 }
299
300 _vector.erase(_vector.begin() + indices.first);
301
302 _index.erase(name);
303
304 // VLAD - PERFORMANCE - CAN THIS BE DONE MORE EFFICIENTLY USING FIND
305 // AND NOT GOING THROUGH THE COMPLETE CONTAINER
306
307 // Reduce by one all table indices greater than the table index of the
308 // deleted table.
309 for (typename tIndex::iterator pos = _index.begin(); pos != _index.end();
310 ++pos)
311 {
312 if (pos->second.first > indices.first)
313 {
314 --(pos->second.first);
315 }
316 }
317
318 typename tIndex::key_compare keyComp = _index.key_comp();
319
320 if (is_equal(_currentName, name, keyComp))
321 {
322 _currentName.clear();
323 _currentIndices = make_pair(_vector.size(), (unsigned int)0);
324 }
325
326}
327
328
329template <typename T, typename StringCompareT>
331{
332
333 pair<unsigned int, unsigned int> indices = get_indices(name);
334
335 if (indices.first == _vector.size())
336 {
337 throw NotFoundException("Object not found",
338 "mapped_ptr_vector::is_read");
339 }
340
341 if (_vector[indices.first] != NULL)
342 {
343 return(true);
344 }
345 else
346 {
347 return(false);
348 }
349
350}
351
352
353template <typename T, typename StringCompareT>
355{
356
357 pair<unsigned int, unsigned int> indices = get_indices(name);
358
359 if (indices.first == _vector.size())
360 {
361 throw NotFoundException("Object not found",
362 "mapped_ptr_vector::read");
363 }
364
365 _vector[indices.first]->Read(indices.second);
366
367}
368
369
370template <typename T, typename StringCompareT>
371unsigned int mapped_ptr_vector<T, StringCompareT>::write(const string& name)
372{
373
374 // VLAD TROUBLESHOOT POINT
375 // VLAD PERFORMANCE
376
377 pair<unsigned int, unsigned int> indices = get_indices(name);
378
379 if (indices.first == _vector.size())
380 {
381 throw NotFoundException("Object not found",
382 "mapped_ptr_vector::write");
383 }
384
385 // Erase it from the map as it is about to change
386 _index.erase(name);
387
388 indices.second = _vector[indices.first]->Write();
389
390 typename tIndex::value_type valuePair(name, indices);
391
392 _index.insert(valuePair);
393
394 _currentName = name;
395 _currentIndices = indices;
396
397 return(indices.second);
398}
399
400
401template <typename T, typename StringCompareT>
402pair<unsigned int, unsigned int> mapped_ptr_vector<T, StringCompareT>::get_indices(const string& name)
403{
404
405 if (_vector.empty())
406 {
407 // Empty container. Return invalid index.
408 return(make_pair(_vector.size(), (unsigned int)0));
409 }
410
411 typename tIndex::key_compare keyComp = _index.key_comp();
412
413 if (is_equal(name, _currentName, keyComp))
414 {
415 return(_currentIndices);
416 }
417 else
418 {
419 // Return index of found value or invalid index
420 typename tIndex::iterator pos = _index.find(name);
421 if (pos != _index.end())
422 {
423 // Update cache
424 _currentName = name;
425 _currentIndices = pos->second;
426 // Found
427 return(pos->second);
428 }
429 else
430 {
431 // Not found. Return invalid index.
432 return(make_pair(_vector.size(), (unsigned int)0));
433 }
434 }
435
436}
437
438
439template <typename T, typename StringCompareT>
441{
442
443 if (index >= _vector.size())
444 {
445 throw out_of_range("Invalid index in"\
446 " mapped_ptr_vector::get_name");
447 }
448
449 string ret;
450
451 // Return index of found value or invalid index
452 for (typename tIndex::iterator pos = _index.begin(); pos != _index.end();
453 ++pos)
454 if (pos->second.first == index)
455 {
456 ret = pos->first;
457 }
458
459 return(ret);
460
461}
462
463
464template <typename T, typename StringCompareT>
466 vector<unsigned int>& sortedIndices)
467{
468
469 sortedIndices.clear();
470
471 // Return index of found value or invalid index
472 for (typename tIndex::iterator pos = _index.begin(); pos != _index.end();
473 ++pos)
474 {
475 sortedIndices.push_back(pos->second.first);
476 }
477
478}
479
480
481template <typename T, typename StringCompareT>
483 const string& second, const typename tIndex::key_compare& keyComp) const
484{
485
486 return(!(keyComp(first, second) || keyComp(second, first)));
487
488}
489
490
491#endif
492
Empty value exception (e.g. NULL pointer, empty string)
Definition Exceptions.h:42
Object not found (thrown everywhere except from .find() methods)
Definition Exceptions.h:54
Definition mapped_ptr_vector.h:30
void get_sorted_indices(std::vector< unsigned int > &sortedIndices)
Definition mapped_ptr_vector.C:465
bool empty() const
Definition mapped_ptr_vector.C:87
void rename(const std::string &oldName, const std::string &newName)
Definition mapped_ptr_vector.C:257
~mapped_ptr_vector()
Definition mapped_ptr_vector.C:57
void set(T *inP)
Associate the object pointer to already entered object name.
Definition mapped_ptr_vector.C:191
unsigned int size() const
Definition mapped_ptr_vector.C:78
bool operator==(const mapped_ptr_vector &inMappedVector)
Definition mapped_ptr_vector.C:109
T & operator[](unsigned int index)
Definition mapped_ptr_vector.C:214
void erase(const std::string &name)
Removes object pointer.
Definition mapped_ptr_vector.C:289
bool is_read(const std::string &name)
Is object de-serialized.
Definition mapped_ptr_vector.C:330
unsigned int write(const std::string &name)
Serialize the object.
Definition mapped_ptr_vector.C:371
void clear()
Definition mapped_ptr_vector.C:96
mapped_ptr_vector()
Definition mapped_ptr_vector.C:27
void operator=(const mapped_ptr_vector &inMappedVector)
Definition mapped_ptr_vector.C:66
unsigned int find(const std::string &name)
Definition mapped_ptr_vector.C:246
std::string get_name(const unsigned int index)
Definition mapped_ptr_vector.C:440
std::pair< unsigned int, unsigned int > get_indices(const std::string &name)
Definition mapped_ptr_vector.C:402
void push_back(T *inP, const unsigned int fileIndex=0)
Definition mapped_ptr_vector.C:119
void read(const std::string &name)
De-serialize the object.
Definition mapped_ptr_vector.C:354