commonlib
vector_with_ids.h File Reference

VectorWithIds is a vector that automatically assigns unique ids to it's members. More...

#include <vector>
#include <unordered_map>
#include <cassert>
#include "vector_with_ids_iterator.h"

Go to the source code of this file.

Classes

class  VectorWithIds< T >
 

Detailed Description

VectorWithIds is a vector that automatically assigns unique ids to it's members.

Author
Luka Jerman
Date
09/08/2019
Version
1.0

DESCRIPTION

In management of objects a lot of time there is a need to assign unique id numbers to objects. This can be done easily with dictionaries. This provides elegant search by id number but random access isn't as fast as in normal array type container like std::vector. Also the order of elements isn't preserved. Another option is to use an array type container with objects that have id numbers, management of ids is then left to the programmer. VectorWithIds stores the objects in an std::vector and provides similar access as normal std::vector. In parallel to this vector a dictionary with id numbers and references to vector elements is stored and updated on vector changes. Id numbers are automatically assigned to elements according to the initial minimum value
and step between id numbers. When assigned automatically new ids are always set on the the lowest unassigned id. There is also an option that enables element insertion on the back of the vector with a specific id. Id numbers can not be negative.