attributeContainer.hpp 13.5 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
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);
	}
46 47 48 49 50

	for(unsigned int i = 0; i < m_tableMarkerAttribs.size(); ++i)
	{
		m_tableMarkerAttribs[i]->setOrbit(orbit);
	}
51 52 53
}

inline void AttributeContainer::setRegistry(std::map< std::string, RegisteredBaseAttribute* >* re)
Pierre Kraemer's avatar
Pierre Kraemer committed
54
{
55 56 57 58 59 60
	m_attributes_registry_map = re;
}

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

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

77 78 79
	// create the new attribute
	std::string typeName = nameOfType(T()) ;
	AttributeMultiVector<T>* amv = new AttributeMultiVector<T>(attribName, typeName) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
80 81 82

	if(!m_freeIndices.empty())
	{
83
		index = m_freeIndices.back() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
84
		m_freeIndices.pop_back() ;
85
		m_tableAttribs[index] = amv ;
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87 88
	}
	else
	{
89 90
		index = m_tableAttribs.size() ;
		m_tableAttribs.push_back(amv) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
91
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
92

93 94 95 96 97
	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
98
	{
99 100 101
		std::stringstream ss ;
		ss << "unknown" << m_nbUnknown++ ;
		amv->setName(ss.str()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
102 103
	}

104 105
	// update the memory cost of a line
	m_lineCost += sizeof(T) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
106

107 108 109 110
	// 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
111

112
	return amv ;
Pierre Kraemer's avatar
Pierre Kraemer committed
113 114 115
}

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

126
	// create the new attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
127
	AttributeMultiVector<T>* amv = new AttributeMultiVector<T>(attribName, nametype);
128 129 130 131 132 133 134

	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
135 136 137 138 139
	{
		std::stringstream ss;
		ss << "unknown" << m_nbUnknown++;
		amv->setName(ss.str());
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
140

141 142
	// update the memory cost of a line
	m_lineCost += sizeof(T) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
143

144 145
	// 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
146 147 148 149

	m_nbAttributes++;
}

150 151 152 153 154 155 156
template <typename T>
bool AttributeContainer::removeAttribute(const std::string& attribName)
{
	unsigned int index = getAttributeIndex(attribName) ;

	if (index == UNKNOWN)
	{
157
		std::cerr << "removeAttribute by name: attribute not found (" << attribName << ")"<< std::endl ;
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 196 197 198 199 200
		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 ;
}


201 202 203 204 205
/**************************************
 *      INFO ABOUT THE CONTAINER      *
 **************************************/

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

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

215
inline unsigned int AttributeContainer::capacity() const
Pierre Kraemer's avatar
Pierre Kraemer committed
216
{
217 218
	return m_holesBlocks.size() * _BLOCKSIZE_;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
219

220 221
inline unsigned int AttributeContainer::memoryTotalSize() const
{
222 223 224 225 226 227
	return capacity() * (m_lineCost + 8);
}

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

230 231
inline bool AttributeContainer::used(unsigned int index) const
{
Sylvain Thery's avatar
Sylvain Thery committed
232
	return m_holesBlocks[index / _BLOCKSIZE_]->used(index % _BLOCKSIZE_) != 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
233 234
}

Sylvain Thery's avatar
Sylvain Thery committed
235 236 237 238 239
inline float AttributeContainer::fragmentation()
{
	return float(m_size) / float(m_maxSize);
}

240 241 242 243
/**************************************
 *         CONTAINER TRAVERSAL        *
 **************************************/

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
//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)));
//}

265
inline unsigned int AttributeContainer::begin() const
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
{
	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
288
{
289 290 291 292 293
	unsigned int it = 0;
	while ((it < m_maxSize) && (!used(it)))
		++it;
	return it;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
294

295
inline unsigned int AttributeContainer::realEnd() const
296 297 298
{
	return m_maxSize;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
299

300
inline void AttributeContainer::realNext(unsigned int &it) const
301 302 303 304 305
{
	do
	{
		++it;
	} while ((it < m_maxSize) && (!used(it)));
Pierre Kraemer's avatar
Pierre Kraemer committed
306 307
}

Sylvain Thery's avatar
Sylvain Thery committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

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)));
}




333 334 335 336 337
/**************************************
 *          LINES MANAGEMENT          *
 **************************************/

inline void AttributeContainer::initLine(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
338
{
339 340 341 342 343 344
	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
345

346 347
inline void AttributeContainer::initMarkersOfLine(unsigned int index)
{
348
	for(unsigned int i = 0; i < m_tableMarkerAttribs.size(); ++i)
349
	{
350
		m_tableMarkerAttribs[i]->initElt(index);
351 352 353 354
	}
}


355 356 357 358 359 360 361
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);
	}
362 363 364 365 366 367

	for(unsigned int i = 0; i < m_tableMarkerAttribs.size(); ++i)
	{
		m_tableMarkerAttribs[i]->copyElt(dstIndex, srcIndex);
	}

368
}
Pierre Kraemer's avatar
Pierre Kraemer committed
369

370 371 372
inline void AttributeContainer::refLine(unsigned int index)
{
	m_holesBlocks[index / _BLOCKSIZE_]->ref(index % _BLOCKSIZE_);
Pierre Kraemer's avatar
Pierre Kraemer committed
373 374
}

375 376 377 378 379 380 381 382 383
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
384

385
inline unsigned int AttributeContainer::getNbRefs(unsigned int index) const
Pierre Kraemer's avatar
Pierre Kraemer committed
386
{
387 388
	unsigned int bi = index / _BLOCKSIZE_;
	unsigned int j = index % _BLOCKSIZE_;
Pierre Kraemer's avatar
Pierre Kraemer committed
389

390
	return m_holesBlocks[bi]->nbRefs(j);
Pierre Kraemer's avatar
Pierre Kraemer committed
391 392
}

393
inline void AttributeContainer::setNbRefs(unsigned int index, unsigned int nb)
Pierre Kraemer's avatar
Pierre Kraemer committed
394
{
395 396
	m_holesBlocks[index / _BLOCKSIZE_]->setNbRefs(index % _BLOCKSIZE_, nb);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
397

398 399 400
/**************************************
 *       ATTRIBUTES MANAGEMENT        *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
401

402 403 404 405
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
406

407 408 409
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
410 411
}

412 413 414
/**************************************
 *       ATTRIBUTES DATA ACCESS       *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
415 416

template <typename T>
417
AttributeMultiVector<T>* AttributeContainer::getDataVector(unsigned int attrIndex)
Pierre Kraemer's avatar
Pierre Kraemer committed
418
{
419 420 421 422 423
	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");
424
	return atm;
Pierre Kraemer's avatar
Pierre Kraemer committed
425 426
}

427
inline AttributeMultiVectorGen* AttributeContainer::getVirtualDataVector(unsigned int attrIndex)
428
{
429
	return m_tableAttribs[attrIndex];
430
}
Pierre Kraemer's avatar
Pierre Kraemer committed
431

432
template <typename T>
433
AttributeMultiVector<T>* AttributeContainer::getDataVector(const std::string& attribName)
Pierre Kraemer's avatar
Pierre Kraemer committed
434
{
435
	unsigned int index = getAttributeIndex(attribName) ;
436 437
	if(index == UNKNOWN)
		return NULL ;
438 439 440

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

444 445 446 447 448 449 450 451 452 453 454

inline CGoGNCodeType AttributeContainer::getTypeCode(const std::string& attribName) const
{
	unsigned int index = getAttributeIndex(attribName) ;
	if(index == UNKNOWN)
		return CGoGNUNKNOWNTYPE ;
	return m_tableAttribs[index]->getTypeCode();
}



455
inline AttributeMultiVectorGen* AttributeContainer::getVirtualDataVector(const std::string& attribName)
Pierre Kraemer's avatar
Pierre Kraemer committed
456
{
457
	unsigned int index = getAttributeIndex(attribName) ;
458 459 460 461
	if(index == UNKNOWN)
		return NULL ;
	else
		return m_tableAttribs[index];
Pierre Kraemer's avatar
Pierre Kraemer committed
462 463
}

464 465
template <typename T>
inline T& AttributeContainer::getData(unsigned int attrIndex, unsigned int eltIndex)
Pierre Kraemer's avatar
Pierre Kraemer committed
466
{
467 468 469 470 471 472 473 474
	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
475 476
}

477 478
template <typename T>
inline const T& AttributeContainer::getData(unsigned int attrIndex, unsigned int eltIndex) const
Pierre Kraemer's avatar
Pierre Kraemer committed
479
{
480 481 482 483 484 485 486 487
	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
488 489
}

490 491
template <typename T>
inline void AttributeContainer::setData(unsigned int attrIndex, unsigned int eltIndex, const T& data)
Pierre Kraemer's avatar
Pierre Kraemer committed
492
{
493 494 495 496 497 498 499 500
	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
501 502
}

503 504 505 506 507 508 509 510

inline std::vector<AttributeMultiVector<MarkerBool>*>& AttributeContainer::getMarkerAttributes()
{
	return m_tableMarkerAttribs;
}



Pierre Kraemer's avatar
Pierre Kraemer committed
511
} // namespace CGoGN