attributeContainer.hpp 12.7 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>
Pierre Kraemer's avatar
Pierre Kraemer committed
26 27 28 29 30 31 32
#include <cassert>
#include "Container/registered.h"
#include "Utils/nameTypes.h"

namespace CGoGN
{

33
inline unsigned int AttributeContainer::getOrbit() const
34 35 36
{
	return m_orbit ;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
37

38 39 40 41 42 43 44 45 46 47 48
inline void AttributeContainer::setOrbit(unsigned int orbit)
{
	m_orbit = orbit ;
	for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
			m_tableAttribs[i]->setOrbit(orbit);
	}
}

inline void AttributeContainer::setRegistry(std::map< std::string, RegisteredBaseAttribute* >* re)
Pierre Kraemer's avatar
Pierre Kraemer committed
49
{
50 51 52 53 54 55
	m_attributes_registry_map = re;
}

/**************************************
 *          BASIC FEATURES            *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
56

57 58 59
template <typename T>
AttributeMultiVector<T>* AttributeContainer::addAttribute(const std::string& attribName)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
60
	// first check if attribute already exist
61
	unsigned int index ;
Pierre Kraemer's avatar
Pierre Kraemer committed
62 63
	if (attribName != "")
	{
64 65
		index = getAttributeIndex(attribName) ;
		if (index != UNKNOWN)
66 67
		{
			std::cout << "attribute " << attribName << " already found.." << std::endl ;
68
			return NULL ;
69
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
70 71
	}

72 73 74
	// create the new attribute
	std::string typeName = nameOfType(T()) ;
	AttributeMultiVector<T>* amv = new AttributeMultiVector<T>(attribName, typeName) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
75 76 77

	if(!m_freeIndices.empty())
	{
78
		index = m_freeIndices.back() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
79
		m_freeIndices.pop_back() ;
80
		m_tableAttribs[index] = amv ;
Pierre Kraemer's avatar
Pierre Kraemer committed
81 82 83
	}
	else
	{
84 85
		index = m_tableAttribs.size() ;
		m_tableAttribs.push_back(amv) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
86
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
87

88 89 90 91 92
	amv->setOrbit(m_orbit) ;
	amv->setIndex(index) ;

	// generate a name for the attribute if no one was given
	if (attribName == "")
Pierre Kraemer's avatar
Pierre Kraemer committed
93
	{
94 95 96
		std::stringstream ss ;
		ss << "unknown" << m_nbUnknown++ ;
		amv->setName(ss.str()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
97 98
	}

99 100
	// update the memory cost of a line
	m_lineCost += sizeof(T) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
101

102 103 104 105
	// resize the new attribute so that it has the same size than others
	amv->setNbBlocks(m_holesBlocks.size()) ;

	m_nbAttributes++ ;
Pierre Kraemer's avatar
Pierre Kraemer committed
106

107
	return amv ;
Pierre Kraemer's avatar
Pierre Kraemer committed
108 109 110
}

template <typename T>
111
void AttributeContainer::addAttribute(const std::string& attribName, const std::string& nametype, unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
112 113 114 115
{
	// first check if attribute already exist
	if (attribName != "")
	{
116 117 118
		unsigned int i = getAttributeIndex(attribName) ;
		if (i != UNKNOWN)
			return ;
Pierre Kraemer's avatar
Pierre Kraemer committed
119 120
	}

121
	// create the new attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
122
	AttributeMultiVector<T>* amv = new AttributeMultiVector<T>(attribName, nametype);
123 124 125 126 127 128 129

	m_tableAttribs[index] = amv;
	amv->setOrbit(m_orbit) ;
	amv->setIndex(index) ;

	// generate a name for the attribute if no one was given
	if (attribName == "")
Pierre Kraemer's avatar
Pierre Kraemer committed
130 131 132 133 134
	{
		std::stringstream ss;
		ss << "unknown" << m_nbUnknown++;
		amv->setName(ss.str());
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
135

136 137
	// update the memory cost of a line
	m_lineCost += sizeof(T) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
138

139 140
	// resize the new attribute so that it has the same size than others
	amv->setNbBlocks(m_holesBlocks.size()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
141 142 143 144

	m_nbAttributes++;
}

145 146 147 148 149 150 151
template <typename T>
bool AttributeContainer::removeAttribute(const std::string& attribName)
{
	unsigned int index = getAttributeIndex(attribName) ;

	if (index == UNKNOWN)
	{
152
		std::cerr << "removeAttribute by name: attribute not found (" << attribName << ")"<< std::endl ;
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
		return false ;
	}

	// delete the attribute
	delete m_tableAttribs[index] ;
	m_tableAttribs[index] = NULL ;

	if (index == m_tableAttribs.size() - 1)
		m_tableAttribs.pop_back() ;
	else
		m_freeIndices.push_back(index) ;

	--m_nbAttributes ;
	m_lineCost -= sizeof(T);

	return true ;
}

template <typename T>
bool AttributeContainer::removeAttribute(unsigned int index)
{
	if(m_tableAttribs[index] == NULL)
	{
		std::cerr << "removeAttribute by index: attribute not found" << std::endl ;
		return false ;
	}

	// delete the attribute
	delete m_tableAttribs[index] ;
	m_tableAttribs[index] = NULL ;

	if(index == m_tableAttribs.size() - 1)
		m_tableAttribs.pop_back() ;
	else
		m_freeIndices.push_back(index) ;

	--m_nbAttributes ;
	m_lineCost -= sizeof(T);

	return true ;
}


196 197 198 199 200
/**************************************
 *      INFO ABOUT THE CONTAINER      *
 **************************************/

inline unsigned int AttributeContainer::getNbAttributes() const
Pierre Kraemer's avatar
Pierre Kraemer committed
201
{
Pierre Kraemer's avatar
Pierre Kraemer committed
202
	return m_nbAttributes;
Pierre Kraemer's avatar
Pierre Kraemer committed
203 204
}

205 206 207 208
inline unsigned int AttributeContainer::size() const
{
	return m_size;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
209

210
inline unsigned int AttributeContainer::capacity() const
Pierre Kraemer's avatar
Pierre Kraemer committed
211
{
212 213
	return m_holesBlocks.size() * _BLOCKSIZE_;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
214

215 216
inline unsigned int AttributeContainer::memoryTotalSize() const
{
217 218 219 220 221 222
	return capacity() * (m_lineCost + 8);
}

inline unsigned int AttributeContainer::memorySize() const
{
	return size() * (m_lineCost + 8);
223
}
Pierre Kraemer's avatar
Pierre Kraemer committed
224

225 226
inline bool AttributeContainer::used(unsigned int index) const
{
Sylvain Thery's avatar
Sylvain Thery committed
227
	return m_holesBlocks[index / _BLOCKSIZE_]->used(index % _BLOCKSIZE_) != 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
228 229
}

230 231 232 233 234
inline float AttributeContainer::fragmentation()
{
	return float(m_size) / float(m_maxSize);
}

235 236 237 238
/**************************************
 *         CONTAINER TRAVERSAL        *
 **************************************/

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
//inline unsigned int AttributeContainer::begin() const
//{
//	unsigned int it = 0;
//	while ((it < m_maxSize) && (!used(it)))
//		++it;
//	return it;
//}

//inline unsigned int AttributeContainer::end() const
//{
//	return m_maxSize;
//}

//inline void AttributeContainer::next(unsigned int &it) const
//{
//	do
//	{
//		++it;
//	} while ((it < m_maxSize) && (!used(it)));
//}

260
inline unsigned int AttributeContainer::begin() const
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
{
	if (m_currentBrowser != NULL)
		return m_currentBrowser->begin();
	return AttributeContainer::realBegin();
}

inline unsigned int AttributeContainer::end() const
{
	if (m_currentBrowser != NULL)
		return m_currentBrowser->end();
	return AttributeContainer::realEnd();
}

inline void AttributeContainer::next(unsigned int &it) const
{
	if (m_currentBrowser != NULL)
		m_currentBrowser->next(it);
	else
		AttributeContainer::realNext(it);
}

inline unsigned int AttributeContainer::realBegin() const
Pierre Kraemer's avatar
Pierre Kraemer committed
283
{
284 285 286 287 288
	unsigned int it = 0;
	while ((it < m_maxSize) && (!used(it)))
		++it;
	return it;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
289

290
inline unsigned int AttributeContainer::realEnd() const
291 292 293
{
	return m_maxSize;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
294

295
inline void AttributeContainer::realNext(unsigned int &it) const
296 297 298 299 300
{
	do
	{
		++it;
	} while ((it < m_maxSize) && (!used(it)));
Pierre Kraemer's avatar
Pierre Kraemer committed
301 302
}

Sylvain Thery's avatar
Sylvain Thery committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

inline unsigned int AttributeContainer::realRBegin() const
{
	unsigned int it = m_maxSize-1;
	while ((it != 0xffffffff) && (!used(it)))
		--it;
	return it;
}

inline unsigned int AttributeContainer::realREnd() const
{
	return 0xffffffff; // -1
}

inline void AttributeContainer::realRNext(unsigned int &it) const
{
	do
	{
		--it;
	} while ((it !=0xffffffff) && (!used(it)));
}




328 329 330 331 332
/**************************************
 *          LINES MANAGEMENT          *
 **************************************/

inline void AttributeContainer::initLine(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
333
{
334 335 336 337 338 339
	for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
			m_tableAttribs[i]->initElt(index);
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
340

341 342 343 344 345 346 347 348
inline void AttributeContainer::copyLine(unsigned int dstIndex, unsigned int srcIndex)
{
	for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
			m_tableAttribs[i]->copyElt(dstIndex, srcIndex);
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
349

350 351 352
inline void AttributeContainer::refLine(unsigned int index)
{
	m_holesBlocks[index / _BLOCKSIZE_]->ref(index % _BLOCKSIZE_);
Pierre Kraemer's avatar
Pierre Kraemer committed
353 354
}

355 356 357 358 359 360 361 362 363
inline bool AttributeContainer::unrefLine(unsigned int index)
{
	if (m_holesBlocks[index / _BLOCKSIZE_]->unref(index % _BLOCKSIZE_))
	{
		--m_size;
		return true;
	}
	return false;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
364

365
inline unsigned int AttributeContainer::getNbRefs(unsigned int index) const
Pierre Kraemer's avatar
Pierre Kraemer committed
366
{
367 368
	unsigned int bi = index / _BLOCKSIZE_;
	unsigned int j = index % _BLOCKSIZE_;
Pierre Kraemer's avatar
Pierre Kraemer committed
369

370
	return m_holesBlocks[bi]->nbRefs(j);
Pierre Kraemer's avatar
Pierre Kraemer committed
371 372
}

373
inline void AttributeContainer::setNbRefs(unsigned int index, unsigned int nb)
Pierre Kraemer's avatar
Pierre Kraemer committed
374
{
375 376
	m_holesBlocks[index / _BLOCKSIZE_]->setNbRefs(index % _BLOCKSIZE_, nb);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
377

378 379 380
/**************************************
 *       ATTRIBUTES MANAGEMENT        *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
381

382 383 384 385
inline bool AttributeContainer::copyAttribute(unsigned int index_dst, unsigned int index_src)
{
	return m_tableAttribs[index_dst]->copy(m_tableAttribs[index_src]);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
386

387 388 389
inline bool AttributeContainer::swapAttributes(unsigned int index1, unsigned int index2)
{
	return m_tableAttribs[index1]->swap(m_tableAttribs[index2]);
Pierre Kraemer's avatar
Pierre Kraemer committed
390 391
}

392 393 394
/**************************************
 *       ATTRIBUTES DATA ACCESS       *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
395 396

template <typename T>
397
AttributeMultiVector<T>* AttributeContainer::getDataVector(unsigned int attrIndex)
Pierre Kraemer's avatar
Pierre Kraemer committed
398
{
399 400 401 402 403
	assert(attrIndex < m_tableAttribs.size() || !"getDataVector: attribute index out of bounds");
	assert(m_tableAttribs[attrIndex] != NULL || !"getDataVector: attribute does not exist");

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[attrIndex]);
	assert((atm != NULL) || !"getDataVector: wrong type");
404
	return atm;
Pierre Kraemer's avatar
Pierre Kraemer committed
405 406
}

407
inline AttributeMultiVectorGen* AttributeContainer::getVirtualDataVector(unsigned int attrIndex)
408
{
409
	return m_tableAttribs[attrIndex];
410
}
Pierre Kraemer's avatar
Pierre Kraemer committed
411

412
template <typename T>
413
AttributeMultiVector<T>* AttributeContainer::getDataVector(const std::string& attribName)
Pierre Kraemer's avatar
Pierre Kraemer committed
414
{
415
	unsigned int index = getAttributeIndex(attribName) ;
416 417
	if(index == UNKNOWN)
		return NULL ;
418 419 420

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[index]);
	assert((atm != NULL) || !"getDataVector: wrong type");
421
	return atm;
Pierre Kraemer's avatar
Pierre Kraemer committed
422 423
}

424
inline AttributeMultiVectorGen* AttributeContainer::getVirtualDataVector(const std::string& attribName)
Pierre Kraemer's avatar
Pierre Kraemer committed
425
{
426
	unsigned int index = getAttributeIndex(attribName) ;
427 428 429 430
	if(index == UNKNOWN)
		return NULL ;
	else
		return m_tableAttribs[index];
Pierre Kraemer's avatar
Pierre Kraemer committed
431 432
}

433 434
template <typename T>
inline T& AttributeContainer::getData(unsigned int attrIndex, unsigned int eltIndex)
Pierre Kraemer's avatar
Pierre Kraemer committed
435
{
436 437 438 439 440 441 442 443
	assert(eltIndex < m_maxSize || !"getData: element index out of bounds");
	assert(m_holesBlocks[eltIndex / _BLOCKSIZE_]->used(eltIndex % _BLOCKSIZE_) || !"getData: element does not exist");
	assert((m_tableAttribs[attrIndex] != NULL) || !"getData: attribute does not exist");

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[attrIndex]);
	assert((atm != NULL) || !"getData: wrong type");

	return atm->operator[](eltIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
444 445
}

446 447
template <typename T>
inline const T& AttributeContainer::getData(unsigned int attrIndex, unsigned int eltIndex) const
Pierre Kraemer's avatar
Pierre Kraemer committed
448
{
449 450 451 452 453 454 455 456
	assert(eltIndex < m_maxSize || !"getData: element index out of bounds");
	assert(m_holesBlocks[eltIndex / _BLOCKSIZE_]->used(eltIndex % _BLOCKSIZE_) || !"getData: element does not exist");
	assert((m_tableAttribs[attrIndex] != NULL) || !"getData: attribute does not exist");

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[attrIndex]);
	assert((atm != NULL) || !"getData: wrong type");

	return atm->operator[](eltIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
457 458
}

459 460
template <typename T>
inline void AttributeContainer::setData(unsigned int attrIndex, unsigned int eltIndex, const T& data)
Pierre Kraemer's avatar
Pierre Kraemer committed
461
{
462 463 464 465 466 467 468 469
	assert(eltIndex < m_maxSize || !"getData: element index out of bounds");
	assert(m_holesBlocks[eltIndex / _BLOCKSIZE_]->used(eltIndex % _BLOCKSIZE_) || !"getData: element does not exist");
	assert((m_tableAttribs[attrIndex] != NULL) || !"getData: attribute does not exist");

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[attrIndex]);
	assert((atm != NULL) || !"getData: wrong type");

	atm->operator[](eltIndex) = data;
Pierre Kraemer's avatar
Pierre Kraemer committed
470 471 472
}

} // namespace CGoGN