7#ifndef MAPPED_PTR_VECTOR_C
8#define MAPPED_PTR_VECTOR_C
19using std::out_of_range;
26template <
typename T,
typename StringCompareT>
34template <
typename T,
typename StringCompareT>
36 const StringCompareT& cmp) : _index(cmp)
43template <
typename T,
typename StringCompareT>
48 _vector = inMappedPtrVector._vector;
49 _index = inMappedPtrVector._index;
50 _currentName = inMappedPtrVector._currentName;
51 _currentIndices = inMappedPtrVector._currentIndices;
56template <
typename T,
typename StringCompareT>
65template <
typename T,
typename StringCompareT>
69 _vector = inMappedPtrVector._vector;
70 _index = inMappedPtrVector._index;
71 _currentName = inMappedPtrVector._currentName;
72 _currentIndices = inMappedPtrVector._currentIndices;
77template <
typename T,
typename StringCompareT>
81 return(_vector.size());
86template <
typename T,
typename StringCompareT>
90 return(_vector.empty());
95template <
typename T,
typename StringCompareT>
103 _currentName.clear();
108template <
typename T,
typename StringCompareT>
113 return(_vector == inMappedPtrVector._vector);
118template <
typename T,
typename StringCompareT>
120 const unsigned int fileIndex)
126 "mapped_ptr_vector::push_back");
129 _vector.push_back(inP);
131 typename tIndex::value_type valuePair(inP->GetName(),
132 make_pair(_vector.size() - 1, fileIndex));
134 _index.insert(valuePair);
136 _currentName = inP->GetName();
137 _currentIndices = make_pair(_vector.size() - 1, fileIndex);
142template <
typename T,
typename StringCompareT>
144 const unsigned int fileIndex)
147 _vector.push_back(NULL);
149 typename tIndex::value_type valuePair(name,
150 make_pair(_vector.size() - 1, fileIndex));
152 _index.insert(valuePair);
155 _currentIndices = make_pair(_vector.size() - 1, fileIndex);
159template <
typename T,
typename StringCompareT>
161 const vector<string>& names,
const vector<unsigned int>& fileIndices)
164 if (names.size() != fileIndices.size())
166 throw out_of_range(
"Different sizes of names and fileIndices in"\
167 " mapped_ptr_vector::push_back");
170 for (
unsigned int nameI = 0; nameI < names.size(); ++nameI)
172 push_back(names[nameI], fileIndices[nameI]);
178template <
typename T,
typename StringCompareT>
182 for (
unsigned int nameI = 0; nameI < names.size(); ++nameI)
184 push_back(names[nameI], 0);
190template <
typename T,
typename StringCompareT>
197 "mapped_ptr_vector::set");
200 pair<unsigned int, unsigned int> indices = get_indices(inP->GetName());
202 if (indices.first == _vector.size())
205 "mapped_ptr_vector::set");
208 _vector[indices.first] = inP;
213template <
typename T,
typename StringCompareT>
217 if (index >= _vector.size())
219 throw out_of_range(
"Invalid index in"\
220 " mapped_ptr_vector::operator[]");
223 return((T&)(*(_vector[index])));
228template <
typename T,
typename StringCompareT>
232 pair<unsigned int, unsigned int> indices = get_indices(name);
234 if (indices.first == _vector.size())
237 "mapped_ptr_vector::operator[]");
240 return((T&)(*(_vector[indices.first])));
245template <
typename T,
typename StringCompareT>
249 pair<unsigned int, unsigned int> indices = get_indices(name);
251 return(indices.first);
256template <
typename T,
typename StringCompareT>
258 const string& newName)
261 pair<unsigned int, unsigned int> indices = get_indices(oldName);
263 if (indices.first == _vector.size())
266 "mapped_ptr_vector::rename");
270 _index.erase(oldName);
272 typename tIndex::value_type valuePair(newName, indices);
274 _index.insert(valuePair);
276 _vector[indices.first]->SetName(newName);
278 typename tIndex::key_compare keyComp = _index.key_comp();
280 if (is_equal(_currentName, oldName, keyComp))
282 _currentName = newName;
288template <
typename T,
typename StringCompareT>
292 pair<unsigned int, unsigned int> indices = get_indices(name);
294 if (indices.first == _vector.size())
297 "mapped_ptr_vector::erase");
300 _vector.erase(_vector.begin() + indices.first);
309 for (
typename tIndex::iterator pos = _index.begin(); pos != _index.end();
312 if (pos->second.first > indices.first)
314 --(pos->second.first);
318 typename tIndex::key_compare keyComp = _index.key_comp();
320 if (is_equal(_currentName, name, keyComp))
322 _currentName.clear();
323 _currentIndices = make_pair(_vector.size(), (
unsigned int)0);
329template <
typename T,
typename StringCompareT>
333 pair<unsigned int, unsigned int> indices = get_indices(name);
335 if (indices.first == _vector.size())
338 "mapped_ptr_vector::is_read");
341 if (_vector[indices.first] != NULL)
353template <
typename T,
typename StringCompareT>
357 pair<unsigned int, unsigned int> indices = get_indices(name);
359 if (indices.first == _vector.size())
362 "mapped_ptr_vector::read");
365 _vector[indices.first]->Read(indices.second);
370template <
typename T,
typename StringCompareT>
377 pair<unsigned int, unsigned int> indices = get_indices(name);
379 if (indices.first == _vector.size())
382 "mapped_ptr_vector::write");
388 indices.second = _vector[indices.first]->Write();
390 typename tIndex::value_type valuePair(name, indices);
392 _index.insert(valuePair);
395 _currentIndices = indices;
397 return(indices.second);
401template <
typename T,
typename StringCompareT>
408 return(make_pair(_vector.size(), (
unsigned int)0));
411 typename tIndex::key_compare keyComp = _index.key_comp();
413 if (is_equal(name, _currentName, keyComp))
415 return(_currentIndices);
420 typename tIndex::iterator pos = _index.find(name);
421 if (pos != _index.end())
425 _currentIndices = pos->second;
432 return(make_pair(_vector.size(), (
unsigned int)0));
439template <
typename T,
typename StringCompareT>
443 if (index >= _vector.size())
445 throw out_of_range(
"Invalid index in"\
446 " mapped_ptr_vector::get_name");
452 for (
typename tIndex::iterator pos = _index.begin(); pos != _index.end();
454 if (pos->second.first == index)
464template <
typename T,
typename StringCompareT>
466 vector<unsigned int>& sortedIndices)
469 sortedIndices.clear();
472 for (
typename tIndex::iterator pos = _index.begin(); pos != _index.end();
475 sortedIndices.push_back(pos->second.first);
481template <
typename T,
typename StringCompareT>
483 const string& second,
const typename tIndex::key_compare& keyComp)
const
486 return(!(keyComp(first, second) || keyComp(second, first)));
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