Blame | Last modification | View Log | RSS feed
#ifndef __HMESH_ENTITYVECTOR_H__
#define __HMESH_ENTITYVECTOR_H__
#include <vector>
#include <cassert>
namespace HMesh
{
template<class ENTITY>
class EntityVector
{
public:
EntityVector() : size_active(0){}
EntityVector(size_t _size, ENTITY e = ENTITY()) : size_active(_size)
{
resize(_size, e);
}
/// Get a reference to entityi from kernel
ENTITY& get(size_t i)
{
assert(i < entities.size());
return entities[i];
}
/// Get a const reference to entity i from kernel
const ENTITY& get(size_t i) const
{
assert(i < entities.size());
return entities[i];
}
ENTITY& operator[](size_t i)
{
assert(i < entities.size());
return entities[i];
}
const ENTITY& operator[](size_t i) const
{
assert(i < entities.size());
return entities[i];
}
/// Add an entity to the kernel
void add(const ENTITY& e)
{
entities.push_back(e);
entities_active.push_back(true);
++size_active;
}
/// remove an entity from kernel - entity is NOT erased!
void remove(size_t i)
{
if(entities_active[i]) --size_active;
entities_active[i] = false;
}
/// erase unused entities from the kernel
void cleanup()
{
std::vector<ENTITY> entities_new;
for(size_t i = 0; i < entities.size(); ++i){
if(entities_active[i]) entities_new.push_back(entities[i]);
}
entities = entities_new;
entities_active = std::vector<bool>(entities.size(), true);
size_active = entities.size();
}
///
size_t full_size() const
{
return entities.size();
}
/// Get the size used in the kernel
size_t size() const
{
return size_active;
}
/// Resize the kernel
void resize(size_t i, ENTITY e = ENTITY())
{
entities.resize(i, e);
entities_active.resize(i, true);
size_active = i;
}
/// Request size change in kernel
void reserve(size_t i)
{
entities.reserve(i);
entities_active.reserve(i);
}
/// Clear the kernel
void clear()
{
entities.clear();
entities_active.clear();
size_active = 0;
}
/// Check if entity i is used
bool in_use(size_t i) const
{
assert(i < entities_active.size());
return entities_active[i];
}
private:
size_t size_active;
/// Kernel entities
std::vector<ENTITY> entities;
/// Memory consideration - objects flagged as unused should be remembered for future use (unless purged)
std::vector<bool> entities_active;
};
}
#endif __HMESH_ENTITYVECTOR_H__