3#include <Core/RaCore.hpp>
9#include <Core/Utils/Index.hpp>
33 typename IndexContainer::const_iterator;
36 typename Container::iterator;
59 template <
typename... Args>
60 inline Index
emplace(
const Args&&... args );
66 inline bool remove(
const Index& idx );
72 inline const T&
at(
const Index& idx )
const;
75 inline T&
access(
const Index& idx );
80 inline size_t size()
const;
90 inline Index
index(
const uint i )
98 const Index& idx )
const;
130 inline void push_free_index(
const Index& idx );
131 inline bool pop_free_index( Index& idx );
158 m_data( id_map.m_data ), m_index( id_map.m_index ), m_free( id_map.m_free ) {}
172 if ( pop_free_index( idx ) ) {
173 typename std::deque<Index>::iterator it =
175 if ( it == m_index.end() ) { m_data.insert( m_data.end(), obj ); }
176 else { m_data.insert( citfromIndex( it ), obj ); }
177 m_index.insert( it, idx );
183template <
typename... Args>
186 if ( pop_free_index( idx ) ) {
187 typename std::deque<Index>::iterator it =
189 if ( it == m_index.end() ) { m_data.emplace( m_data.end(), args... ); }
190 else { m_data.emplace( citfromIndex( it ), args... ); }
191 m_index.insert( it, idx );
201 typename std::deque<Index>::iterator it =
std::find( m_index.begin(), m_index.end(), idx );
202 if ( it == m_index.end() ) {
return false; }
203 m_data.erase( itfromIndex( it ) );
205 push_free_index( idx );
214 typename std::deque<Index>::const_iterator it =
215 std::find( m_index.begin(), m_index.end(), idx );
216 CORE_ASSERT( it != m_index.end(),
"Index not found" );
217 return m_data.at( idxfromIndex( it ) );
222 typename std::deque<Index>::iterator it =
std::find( m_index.begin(), m_index.end(), idx );
223 CORE_ASSERT( ( it != m_index.end() ),
"Index not found" );
224 return *itfromIndex( it );
232 return m_data.size();
240 m_free.push_back( Index( 0 ) );
248 return m_data.empty();
253 return m_free.empty();
258 typename std::deque<Index>::const_iterator it =
259 std::find( m_index.begin(), m_index.end(), idx );
260 return !( it == m_index.end() );
265 if ( i >= m_index.size() ) {
return Index::Invalid(); }
266 return m_index.at( i );
274 return access( idx );
287 return m_index.cbegin();
292 return m_index.cend();
300 return m_data.begin();
310 return m_data.begin();
320 return m_data.cbegin();
325 return m_data.cend();
333 std::deque<Index>::iterator free_it =
std::lower_bound( m_free.begin(), m_free.end(), idx );
334 m_free.insert( free_it, idx );
335 if ( m_data.empty() ) {
337 m_free.push_back( Index( 0 ) );
343 if ( m_free.empty() ) {
344 idx = Index::Invalid();
347 idx = m_free.front();
349 if ( m_free.empty() ) {
350 Index next = idx + 1;
351 if ( next.isValid() ) {
352 if ( uint( next.getValue() ) > m_data.size() ) { m_free.push_back( next ); }
365 auto dataIt = m_data.begin();
373 auto dataIt = m_data.begin();
typename IndexContainer::const_iterator ConstIndexIterator
Where the indices are stored.
Index index(const uint i) const
Return true if the IndexMap contains a object with the given index.
const T & at(const Index &idx) const
Return a read-only ref to object with the given index. Crashes if index does not exist.
typename std::deque< Index > IndexContainer
Where the objects are stored.
IndexMap(const IndexMap &id_map)
Default constructor.
~IndexMap()
Copy constructor.
ConstIterator end() const
Return a iterator to the first object in the IndexMap.
IndexContainer m_index
Objects in the IndexMap.
bool remove(const Index &idx)
Remove the object with the given index. Return false if the operation failed.
void clear()
Return the size of the IndexMap ( number of object contained ).
Index emplace(const Args &&... args)
T & access(const Index &idx)
Return a reference to the object with the given index. Crash if index does not exist.
Index insert(const T &obj)
Destructor.
Iterator end()
Return a iterator to the first object in the IndexMap.
bool empty() const
Clear the IndexMap.
Iterator begin()
Return a const iterator to the end of the indices of the IndexMap.
ConstIndexIterator cbegin_index() const
Return a const reference to the object with given index.
ConstIterator cbegin() const
Return a iterator to the end of the object list in the IndexMap.
ConstIterator begin() const
Return a iterator to the end of the object list in the IndexMap.
bool contains(const Index &idx) const
Return true if the IndexMap cannot contain more objects.
Container m_data
Return a const iterator to the end of the object list in the IndexMap.
const T & operator[](const Index &idx) const
Return a reference to the object with given index.
bool full() const
Return true if the IndexMap is empty.
typename Container::const_iterator ConstIterator
Iterator to the list of objects of the IndexMap.
ConstIndexIterator cend_index() const
Return a const iterator to the first index in the IndexMap.
T & operator[](const Index &idx)
Return the i-th index. Return an invalid index if i is out of bound.
ConstIterator cend() const
Return a const iterator to the first object in the IndexMap.
typename Container::iterator Iterator
hepler function to manage enum as underlying types in VariableSet