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