29#include <unordered_map>
42 using iterator = std::unordered_map<index_type, T>::iterator;
44 using value_type = std::unordered_map<index_type, T>::value_type;
47 m_free_indices.insert(0);
48 m_free_indices.insert(1);
57 for (
auto const &e : l) {
58 m_container[e.first] = e.second;
60 if (
auto it = m_free_indices.find(e.first); it != m_free_indices.end()) {
61 m_free_indices.erase(it);
66 for (index_type it(0); m_free_indices.size() < 2; ++it) {
67 if (m_container.find(it) == m_container.end()) {
68 m_free_indices.insert(it);
80 index_type
add(
const T &c) {
81 const index_type ind = get_index();
87 index_type
add(T &&c) {
88 const index_type ind = get_index();
89 m_container[ind] = std::move(c);
103 assert(m_container.find(i) != m_container.end());
105 m_container.erase(i);
106 m_free_indices.insert(i);
123 const T &
operator[](index_type i)
const {
return m_container.at(i); }
161 std::size_t
size()
const {
return m_container.size(); }
165 std::unordered_map<index_type, T> m_container;
167 std::set<index_type> m_free_indices;
181 index_type get_index() {
184 assert(!m_free_indices.empty());
185 const index_type index = *m_free_indices.begin();
187 m_free_indices.erase(index);
191 if (m_free_indices.size() == 1) {
192 m_free_indices.insert(*(--m_free_indices.end()) + 1);
Container for objects that are identified by a numeric id.
std::unordered_map< index_type, T >::iterator iterator
std::unordered_map< index_type, T >::const_iterator const_iterator
const_iterator find(int id) const
find object by id.
const T & operator[](index_type i) const
Get element from container.
std::unordered_map< index_type, T >::value_type value_type
iterator find(int id)
find object by id.
index_type add(const T &c)
Copy c into the container.
const_iterator begin() const
Get a const iterator to beginning of the container.
void remove(index_type i)
Remove element from container.
iterator end()
Get an iterator to end of the container.
const_iterator end() const
Get a const iterator to end of the container.
index_type add(T &&c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
iterator begin()
Get iterator to beginning of the container.
NumeratedContainer(std::initializer_list< value_type > l)
Construct from list of key-value pairs.
T & operator[](index_type i)
Get element from container.