attributeContainer.hpp 7.4 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

//#include <typeinfo>
//#include <stdio.h>
//#include <string.h>
//#include <libxml/encoding.h>
//#include <libxml/xmlwriter.h>

#include<iostream>
#include <cassert>
#include "Container/registered.h"
#include "Utils/nameTypes.h"

namespace CGoGN
{

///////////////////////////
// GESTION DES ATTRIBUTS
///////////////////////////

template <typename T>
21
unsigned int AttributeContainer::addAttribute(const std::string& attribName)
Pierre Kraemer's avatar
Pierre Kraemer committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
{
	std::string nametype = nameOfType(T());

	// first check if attribute already exist
	if (attribName != "")
	{
		MapNameId::iterator it = m_attribNameMap.find(attribName);
		if (it != m_attribNameMap.end())
		{
			return UNKNOWN;
		}
	}

	// new attribut
36
	AttributeMultiVector<T>* ptr = new AttributeMultiVector<T>(attribName, nametype);
Pierre Kraemer's avatar
Pierre Kraemer committed
37 38 39 40 41 42 43 44
	unsigned int idxAttrib = m_tableAttribs.size();
	// add it to table of attribut_manager
	m_tableAttribs.push_back(ptr);

	// add it in the map 
	if (attribName == "") // if no name, generate a fake name
	{
		std::stringstream ss;
45
		ss << "unknown" << m_nbUnknown++;
Pierre Kraemer's avatar
Pierre Kraemer committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
		m_attribNameMap.insert(std::pair<std::string, unsigned int>(ss.str(), idxAttrib));
	}
	else
		m_attribNameMap.insert(std::pair<std::string, unsigned int>(attribName, idxAttrib));

	// maj taille d'une ligne
	m_lineCost += sizeof(T);
	
	// resize the new attribute container to have same size than others
	int nbb = m_holesBlocks.size();
	ptr->setNbBlocks(nbb);

	m_nbAttributes++;

	return idxAttrib;
}

template <typename T>
64
unsigned int AttributeContainer::addAttribute(const std::string& attribName, const std::string& nametype, unsigned int idxAttrib)
Pierre Kraemer's avatar
Pierre Kraemer committed
65 66 67 68 69 70 71 72 73 74 75 76
{
	// first check if attribute already exist
	if (attribName != "")
	{
		MapNameId::iterator it = m_attribNameMap.find(attribName);
		if (it != m_attribNameMap.end())
		{
			return UNKNOWN;
		}
	}

	// new attribut
77
	AttributeMultiVector<T>* ptr = new AttributeMultiVector<T>(attribName, nametype);
Pierre Kraemer's avatar
Pierre Kraemer committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	// add it to table of attribut_manager
	m_tableAttribs[idxAttrib] = ptr;
	// add it in the map
	m_attribNameMap.insert(std::pair<std::string, unsigned int>(attribName, idxAttrib));

	// maj taille d'une ligne
	m_lineCost += sizeof(T);

	// resize the new attribute container to have same size than others
	int nbb = m_holesBlocks.size();
	ptr->setNbBlocks(nbb);

	m_nbAttributes++;

	return idxAttrib;
}

95
inline unsigned int AttributeContainer::getNbAttributes()
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97 98 99
{
	return m_tableAttribs.size();
}

Pierre Kraemer's avatar
Pierre Kraemer committed
100 101 102 103 104
///////////////////////////
// ACCES AUX DONNEES
///////////////////////////

template <typename T>
105
T& AttributeContainer::getData(unsigned int codeAttrib, unsigned int eltIdx)
Pierre Kraemer's avatar
Pierre Kraemer committed
106 107 108 109 110
{
	assert(eltIdx < m_maxSize && "Attribut non existant (indice trop grand)");
	assert(m_holesBlocks[eltIdx/_BLOCKSIZE_]->used(eltIdx%_BLOCKSIZE_)&&"Attribut non existant");
	assert((m_tableAttribs[codeAttrib]!=NULL)&&"Attribut detruit");

111
	AttributeMultiVector<T>* atm = dynamic_cast< AttributeMultiVector<T>* >(m_tableAttribs[codeAttrib]);
Pierre Kraemer's avatar
Pierre Kraemer committed
112 113 114 115 116 117
	assert((atm!=NULL)&& "type attribut non concordant");

	return atm->operator[](eltIdx);
}

template <typename T>
118
const T& AttributeContainer::getData(unsigned int codeAttrib, unsigned int eltIdx) const
Pierre Kraemer's avatar
Pierre Kraemer committed
119 120 121 122 123
{
 	assert(eltIdx < m_maxSize && "Attribut non existant (indice trop grand)");
	assert(m_holesBlocks[eltIdx/_BLOCKSIZE_]->used(eltIdx%_BLOCKSIZE_)&&"Attribut non existant");
	assert((m_tableAttribs[codeAttrib]!=NULL)&&"Attribut detruit");

124
	AttributeMultiVector<T>* atm = dynamic_cast< AttributeMultiVector<T>* >(m_tableAttribs[codeAttrib]);
Pierre Kraemer's avatar
Pierre Kraemer committed
125 126 127 128 129 130
	assert((atm!=NULL)&& "type attribut non concordant");

	return atm->operator[](eltIdx);
}

template <typename T>
131
void AttributeContainer::setData(unsigned int codeAttrib, unsigned int eltIdx,  const T& data)
Pierre Kraemer's avatar
Pierre Kraemer committed
132 133 134 135 136 137
{
	
 	assert(eltIdx < m_maxSize && "Attribut non existant (indice trop grand)");
	assert(m_holesBlocks[eltIdx/_BLOCKSIZE_]->used(eltIdx%_BLOCKSIZE_)&&"Attribut non existant");
	assert((m_tableAttribs[codeAttrib]!=NULL)&&"Attribut detruit");

138
	AttributeMultiVector<T>* atm = dynamic_cast< AttributeMultiVector<T>* >(m_tableAttribs[codeAttrib]);
Pierre Kraemer's avatar
Pierre Kraemer committed
139 140 141 142 143 144
	assert((atm!=NULL)&& "type attribut non concordant");

	atm->operator[](eltIdx) = data;
}

//template <typename T>
145
//unsigned int AttributeContainer::insertLineWidthData(unsigned int codeAttrib,const T& data)
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147 148 149 150 151 152
//{
//	unsigned int it =  insertLine();
//	setData<T>(codeAttrib, it, data);
//	return it;
//}

template<typename T>
153
AttributeMultiVector<T>& AttributeContainer::getDataVector(unsigned int codeAttrib)
Pierre Kraemer's avatar
Pierre Kraemer committed
154 155 156 157
{
	assert((codeAttrib < m_tableAttribs.size()) && "Attribut inexistant");
	assert((m_tableAttribs[codeAttrib] != NULL) && "Attribut detruit");

158
	AttributeMultiVector<T>* atm = dynamic_cast< AttributeMultiVector<T>* >(m_tableAttribs[codeAttrib]);
Pierre Kraemer's avatar
Pierre Kraemer committed
159 160 161 162
	assert((atm!=NULL)&& "type attribut non concordant");
	return *atm;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
163
template<typename T>
164
bool AttributeContainer::getAttributesVector(const std::string& attribName, AttributeMultiVector<T>** ptr)
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166 167 168 169 170 171 172 173 174
{
	MapNameId::iterator it = m_attribNameMap.find(attribName);
	if (it == m_attribNameMap.end())
		return false;
	
	int codeAttrib = it->second;

	if (m_tableAttribs[codeAttrib]==NULL) 
		return false;

175
	AttributeMultiVector<T>* atm = dynamic_cast< AttributeMultiVector<T>* >(m_tableAttribs[codeAttrib]);
Pierre Kraemer's avatar
Pierre Kraemer committed
176 177 178 179 180 181 182 183 184 185 186 187
	if (atm==NULL) 
		return false;

	*ptr = atm;
	return true;
}

///////////////////////////
//  ADRESSES MEMOIRES
///////////////////////////

template <typename T>
188
unsigned int AttributeContainer::getAddresses(unsigned int attr, std::vector<T*>& vect_adr)
Pierre Kraemer's avatar
Pierre Kraemer committed
189
{
190
	AttributeMultiVector<T>* atm = dynamic_cast< AttributeMultiVector<T>* >(m_tableAttribs[attr]);
Pierre Kraemer's avatar
Pierre Kraemer committed
191 192 193 194 195 196 197 198 199
	assert((atm!=NULL)&& "type attribut non concordant");
	return atm->getStartAddresses(vect_adr);
}

//////////////////////////////
//  Enregistrement attributs
//////////////////////////////

//// INLINED FUNCTIONS
200
inline bool AttributeContainer::used(unsigned int eltIdx) const
Pierre Kraemer's avatar
Pierre Kraemer committed
201 202 203 204
{
	return m_holesBlocks[ eltIdx / _BLOCKSIZE_ ]->used( eltIdx % _BLOCKSIZE_ );
}

205
inline void AttributeContainer::setRegistry(std::map< std::string, RegisteredBaseAttribute* >* re)
Pierre Kraemer's avatar
Pierre Kraemer committed
206 207 208 209
{
	m_attributes_registry_map = re;
}

210
inline bool AttributeContainer::copyAttribute(unsigned int index_dst, unsigned int index_src)
Pierre Kraemer's avatar
Pierre Kraemer committed
211 212 213 214
{
	return m_tableAttribs[index_dst]->copy(m_tableAttribs[index_src]);
}

215
inline bool AttributeContainer::swapAttributes(unsigned int index1, unsigned int index2)
Pierre Kraemer's avatar
Pierre Kraemer committed
216 217 218 219
{
	return m_tableAttribs[index1]->swap(m_tableAttribs[index2]);
}

220
inline void AttributeContainer::refLine(unsigned int eltIdx)
Pierre Kraemer's avatar
Pierre Kraemer committed
221 222 223 224
{
	m_holesBlocks[eltIdx / _BLOCKSIZE_]->ref(eltIdx % _BLOCKSIZE_);
}

225
inline bool AttributeContainer::unrefLine(unsigned int eltIdx)
Pierre Kraemer's avatar
Pierre Kraemer committed
226
{
227 228 229 230 231 232
	if (m_holesBlocks[eltIdx / _BLOCKSIZE_]->unref(eltIdx % _BLOCKSIZE_))
	{
		m_size--;
		return true;
	}
	return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
233 234
}

235
inline void AttributeContainer::setRefLine(unsigned int eltIdx, unsigned int nb)
Pierre Kraemer's avatar
Pierre Kraemer committed
236 237 238 239
{
	m_holesBlocks[ eltIdx / _BLOCKSIZE_]->setNbRefs(eltIdx % _BLOCKSIZE_,nb);
}

240
inline AttributeMultiVectorGen& AttributeContainer::getVirtualDataVector(unsigned int codeAttrib)
Pierre Kraemer's avatar
Pierre Kraemer committed
241 242 243 244
{
	return *(m_tableAttribs[indexAttr(codeAttrib)]);
}

245
inline unsigned int AttributeContainer::end() const
Pierre Kraemer's avatar
Pierre Kraemer committed
246 247 248 249
{
	return m_maxSize;
}

250
inline unsigned int AttributeContainer::begin() const
Pierre Kraemer's avatar
Pierre Kraemer committed
251 252 253 254 255 256 257
{
	unsigned int it = 0;
	while ((it < m_maxSize) && (!used(it)))
		++it;
	return it;
}

258
inline void AttributeContainer::next(unsigned int &it) const
Pierre Kraemer's avatar
Pierre Kraemer committed
259 260 261 262 263 264 265
{
	do
	{
		++it;
	} while ((it < m_maxSize) && (!used(it)));
}

266
inline void AttributeContainer::toggleProcess(unsigned int id)
Pierre Kraemer's avatar
Pierre Kraemer committed
267 268 269 270
{
	m_tableAttribs[indexAttr(id)]->toggleProcess();
}

271
inline void AttributeContainer::toggleNoProcess(unsigned int id)
Pierre Kraemer's avatar
Pierre Kraemer committed
272 273 274 275 276
{
	m_tableAttribs[indexAttr(id)]->toggleNoProcess();
}

} // namespace CGoGN