30 #include <unordered_map> 35 BEGIN_NAMESPACE_CONTAINERS
56 VectorWithIds(std::size_t idMinGenerated = 0, std::size_t idStepSizeGenerated = 1);
102 const T&
back()
const;
112 std::size_t
size()
const;
158 void swap(int32_t posA, int32_t posB);
165 int32_t
getId(int32_t pos)
const;
250 int32_t idMin, idStep;
252 std::vector<std::pair<int32_t, T>> pairs;
253 std::unordered_map<int32_t, typename std::vector<std::pair<int32_t, T>>::iterator> pairsMap;
255 void rebuildPairMap();
261 idMin =
static_cast<int32_t
>(idMinGenerated);
263 assert(idStepSizeGenerated > 0);
264 idStep =
static_cast<int32_t
>(idStepSizeGenerated);
267 template <
typename T>
271 idStep = other.idStep;
276 template <
typename T>
282 idStep = other.idStep;
289 template <
typename T>
293 idStep = other.idStep;
295 pairs = std::move(other.pairs);
298 other.pairsMap.clear();
301 template <
typename T>
307 idStep = other.idStep;
309 pairs = std::move(other.pairs);
312 other.pairsMap.clear();
317 template <
typename T>
320 return pairs[pos].second;
323 template <
typename T>
326 return pairs[pos].second;
329 template <
typename T>
332 return pairs.back().second;
335 template <
typename T>
338 return pairs.back().second;
341 template <
typename T>
348 template <
typename T>
354 template <
typename T>
357 return pairs.empty();
360 template <
typename T>
363 auto it = pairs.erase(position.pointee);
368 template <
typename T>
371 auto it = pairs.erase(pairs.begin() + pos);
376 template <
typename T>
381 std::swap(pairs[posA], pairs[posB]);
386 template <
typename T>
392 pairs.emplace_back();
393 pairs[0].first = idMin;
398 bool slotFound =
false;
399 int32_t slotId = idMin;
403 if (pairsMap.find(slotId) == pairsMap.end())
414 pairs.emplace_back();
415 (pairs.back()).first = slotId;
421 template <
typename T>
424 if (pairsMap.find(
id) == pairsMap.end())
426 pairs.emplace_back();
427 (pairs.back()).first =
id;
434 template <
typename T>
437 return pairs[pos].first;
440 template <
typename T>
443 auto pairsMapIter = pairsMap.find(
id);
445 if (pairsMapIter != pairsMap.end())
447 return iterator(pairsMapIter->second);
452 template <
typename T>
455 auto pairsMapIter = pairsMap.find(
id);
457 if (pairsMapIter != pairsMap.end())
467 template <
typename T>
471 const int32_t otherSize =
static_cast<int32_t
>(other.
size());
472 for (int32_t otherPos = 0; otherPos < otherSize; ++otherPos)
475 int32_t otherId = other.
getId(otherPos);
477 auto pairsMapIter = pairsMap.find(otherId);
479 if (pairsMapIter != pairsMap.end())
482 const auto thisPos =
static_cast<int32_t
>(std::distance(pairs.begin(), pairsMapIter->second));
483 swap(thisPos, otherPos);
488 emplaceBackWithId(otherId);
489 swap(otherPos, int32_t(pairs.size()) - 1);
494 if (pairs.size() != other.
size())
496 pairs.resize(other.
size());
501 template <
typename T>
505 for (
auto pairIter = pairs.begin(); pairIter != pairs.end(); ++pairIter)
507 pairsMap.insert(std::make_pair(pairIter->first, pairIter));
511 END_NAMESPACE_CONTAINERS
iterator begin()
Definition: vector_with_ids.h:204
void clear()
Deletes all objects in the container and sets the size to zero.
Definition: vector_with_ids.h:342
iterator erase(iterator position)
Erases an element at an iterator position.
Definition: vector_with_ids.h:361
Definition: vector_with_ids.h:38
VectorWithIds< T > & operator=(const VectorWithIds &other)
Copy assignment.
Definition: vector_with_ids.h:277
T & back()
Mutable access to the last element in the container.
Definition: vector_with_ids.h:330
Definition: vector_with_ids_iterator.h:60
const_iterator cbegin() const
Definition: vector_with_ids.h:236
iterator findById(int32_t id)
Searches for an element with an id passed.
Definition: vector_with_ids.h:441
void swap(int32_t posA, int32_t posB)
Swaps elements in container at indexed positions passed.
Definition: vector_with_ids.h:377
iterator end()
Definition: vector_with_ids.h:212
const_iterator cend() const
Definition: vector_with_ids.h:244
const_iterator begin() const
Definition: vector_with_ids.h:220
const_iterator end() const
Definition: vector_with_ids.h:228
T & operator[](std::size_t pos)
Mutable access element at position.
Definition: vector_with_ids.h:318
std::size_t size() const
Definition: vector_with_ids.h:349
VectorWithIdsIterator provides access to VectorWithIds elements and it's id numbers.
void emplaceBack()
Constructs an additional element at the at the back of the container by calling a default constructor...
Definition: vector_with_ids.h:387
VectorWithIds(std::size_t idMinGenerated=0, std::size_t idStepSizeGenerated=1)
Constructor with optional minimum automatic id and step between generated ids.
Definition: vector_with_ids.h:259
int32_t getId(int32_t pos) const
Swaps elements in container at indexed positions passed.
Definition: vector_with_ids.h:435
bool emplaceBackWithId(int32_t id)
Constructs an additional element at the at the back of the container by calling a default constructor...
Definition: vector_with_ids.h:422
void cloneStructure(const VectorWithIds< T > &other)
Clones the structure of the container to the one of the passed object.
Definition: vector_with_ids.h:468
bool empty() const
Definition: vector_with_ids.h:355