attributeContainer.cpp 26.1 KB
Newer Older
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           *
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/                                           *
21 22 23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
24 25 26 27 28 29 30 31

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

32
#include "Container/attributeContainer.h"
33
#include "Topology/generic/dart.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
34 35 36 37

namespace CGoGN
{

38
AttributeContainer::AttributeContainer() :
39
	m_currentBrowser(NULL),
40 41 42 43 44 45 46
	m_orbit(0),
	m_nbAttributes(0),
	m_nbUnknown(0),
	m_size(0),
	m_maxSize(0),
	m_lineCost(0),
	m_attributes_registry_map(NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
47 48 49 50
{
	m_holesBlocks.reserve(512);
}

51
AttributeContainer::~AttributeContainer()
Pierre Kraemer's avatar
Pierre Kraemer committed
52
{
53 54 55 56 57 58 59 60 61 62 63 64
	for (unsigned int index = 0; index < m_tableAttribs.size(); ++index)
	{
		if (m_tableAttribs[index] != NULL)
			delete m_tableAttribs[index];
	}

	for (unsigned int index = 0; index < m_holesBlocks.size(); ++index)
	{
		if (m_holesBlocks[index] != NULL)
			delete m_holesBlocks[index];
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
65 66
}

67 68 69 70
/**************************************
 *       INFO ABOUT ATTRIBUTES        *
 **************************************/

71
unsigned int AttributeContainer::getAttributeIndex(const std::string& attribName) const
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
{
	unsigned int index ;
	bool found = false ;
	for (index = 0; index < m_tableAttribs.size() && !found; ++index)
	{
		if(m_tableAttribs[index] != NULL && m_tableAttribs[index]->getName() == attribName)
			found = true ;
	}

	if (!found)
		return UNKNOWN;
	else
		return index - 1 ;
}

87
const std::string& AttributeContainer::getAttributeName(unsigned int index) const
88 89 90 91
{
	assert(index < m_tableAttribs.size() || !"getAttributeName: attribute index out of bounds");
	assert(m_tableAttribs[index] != NULL || !"getAttributeName: attribute does not exist");

92
	return m_tableAttribs[index]->getName() ;
93 94 95 96 97 98 99 100 101 102 103 104 105
}

template <typename T>
unsigned int AttributeContainer::getAttributeBlocksPointers(unsigned int attrIndex, std::vector<T*>& vect_ptr, unsigned int& byteBlockSize)
{
	assert(attrIndex < m_tableAttribs.size() || !"getAttributeBlocksPointers: attribute index out of bounds");
	assert(m_tableAttribs[attrIndex] != NULL || !"getAttributeBlocksPointers: attribute does not exist");

	AttributeMultiVector<T>* atm = dynamic_cast<AttributeMultiVector<T>*>(m_tableAttribs[attrIndex]);
	assert((atm != NULL) || !"getAttributeBlocksPointers: wrong type");
	return atm->getBlocksPointers(vect_ptr, byteBlockSize);
}

106
unsigned int AttributeContainer::getAttributesNames(std::vector<std::string>& names) const
107 108 109 110 111 112 113 114 115 116 117 118 119
{
	names.clear() ;
	names.reserve(m_nbAttributes) ;

	for (unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if(m_tableAttribs[i] != NULL)
			names.push_back(m_tableAttribs[i]->getName()) ;
	}

	return m_nbAttributes ;
}

Sylvain Thery's avatar
Sylvain Thery committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133
unsigned int AttributeContainer::getAttributesTypes(std::vector<std::string>& types)
{
	types.clear() ;
	types.reserve(m_nbAttributes) ;

	for (unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if(m_tableAttribs[i] != NULL)
			types.push_back(m_tableAttribs[i]->getTypeName()) ;
	}

	return m_nbAttributes ;
}

134 135 136 137
/**************************************
 *        CONTAINER MANAGEMENT        *
 **************************************/

138
void AttributeContainer::swap(AttributeContainer& cont)
Pierre Kraemer's avatar
Pierre Kraemer committed
139
{
140 141
	// swap everything but the orbit

Pierre Kraemer's avatar
Pierre Kraemer committed
142
	m_tableAttribs.swap(cont.m_tableAttribs);
Pierre Kraemer's avatar
Pierre Kraemer committed
143
	m_freeIndices.swap(cont.m_freeIndices);
144 145 146
	m_holesBlocks.swap(cont.m_holesBlocks);
	m_tableBlocksWithFree.swap(cont.m_tableBlocksWithFree);
	m_tableBlocksEmpty.swap(cont.m_tableBlocksEmpty);
Pierre Kraemer's avatar
Pierre Kraemer committed
147 148 149 150 151

	unsigned int temp = m_nbAttributes;
	m_nbAttributes = cont.m_nbAttributes;
	cont.m_nbAttributes = temp;

152 153 154 155
	temp = m_nbUnknown ;
	m_nbUnknown = cont.m_nbUnknown ;
	cont.m_nbUnknown = temp ;

Pierre Kraemer's avatar
Pierre Kraemer committed
156 157 158 159 160 161 162 163 164 165 166 167 168
	temp = m_size;
	m_size = cont.m_size;
	cont.m_size = temp;

	temp = m_maxSize;
	m_maxSize = cont.m_maxSize;
	cont.m_maxSize = temp;

	temp = m_lineCost;
	m_lineCost = cont.m_lineCost;
	cont.m_lineCost = temp;
}

169
void AttributeContainer::clear(bool removeAttrib)
Pierre Kraemer's avatar
Pierre Kraemer committed
170
{
171 172
	m_size = 0;
 	m_maxSize = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
173

174
		// raz des cases libres
175 176
	for (std::vector<HoleBlockRef*>::iterator it = m_holesBlocks.begin(); it != m_holesBlocks.end(); ++it)
		delete (*it);
Pierre Kraemer's avatar
Pierre Kraemer committed
177

Sylvain Thery's avatar
Sylvain Thery committed
178 179 180
	{ // add bracket just for scope of temporary vectors
		std::vector<HoleBlockRef*> bf;
		m_holesBlocks.swap(bf);
181

Sylvain Thery's avatar
Sylvain Thery committed
182 183 184
		std::vector<unsigned int> bwf;
		m_tableBlocksWithFree.swap(bwf);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
185

186 187 188 189 190 191
	// detruit les données
	for (std::vector<AttributeMultiVectorGen*>::iterator it = m_tableAttribs.begin(); it != m_tableAttribs.end(); ++it)
	{
		if ((*it) != NULL)
			(*it)->clear();
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
192

193 194 195 196 197 198 199 200 201 202 203 204
	// on enleve les attributs ?
	if (removeAttrib)
	{
		// nb a zero
		m_nbAttributes = 0;

		// detruit tous les attributs
		for (std::vector<AttributeMultiVectorGen*>::iterator it = m_tableAttribs.begin(); it != m_tableAttribs.end(); ++it)
		{
			if ((*it) != NULL)
				delete (*it);
		}
205 206

		std::vector<AttributeMultiVectorGen*> amg;
207
		m_tableAttribs.swap(amg);
208 209 210

		std::vector<unsigned int> fi;
		m_freeIndices.swap(fi);
211
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
212 213
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
bool AttributeContainer::hasMarkerAttribute() const
{
	for (auto it = m_tableAttribs.begin(); it != m_tableAttribs.end(); ++it)
	{
		if ((*it) != NULL)
		{
			std::string strMarker = (*it)->getName().substr(0,6);
			if (strMarker=="marker")
				return true;
		}
	}
	return false;
}


229
void AttributeContainer::compact(std::vector<unsigned int>& mapOldNew)
Pierre Kraemer's avatar
Pierre Kraemer committed
230
{
231
	mapOldNew.clear();
Sylvain Thery's avatar
Sylvain Thery committed
232
	mapOldNew.resize(realEnd(),0xffffffff);
Pierre Kraemer's avatar
Pierre Kraemer committed
233

Sylvain Thery's avatar
Sylvain Thery committed
234 235 236 237 238 239 240 241 242 243 244
//VERSION THAT PRESERVE ORDER OF ELEMENTS ?
//	unsigned int down = 0;
//	for (unsigned int occup = realBegin(); occup != realEnd(); next(occup))
//	{
//		mapOldNew[occup] = down;
//		copyLine(down,occup);
//		// copy ref counter
//		setNbRefs(down,getNbRefs(occup));
//		down++;
//	}

Sylvain Thery's avatar
Sylvain Thery committed
245 246 247
	// fill the holes with data & create the map Old->New
	unsigned int up = realRBegin();
	unsigned int down = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
248

Sylvain Thery's avatar
Sylvain Thery committed
249 250 251
	while (down < up)
	{
		if (!used(down))
252
		{
Sylvain Thery's avatar
Sylvain Thery committed
253 254 255 256 257 258 259
			mapOldNew[up] = down;
			// copy data
			copyLine(down,up);
			// copy ref counter
			setNbRefs(down,getNbRefs(up));
			// set next element to catch for hole filling
			realRNext(up);
260
		}
Sylvain Thery's avatar
Sylvain Thery committed
261
		down++;
262
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
263

Sylvain Thery's avatar
Sylvain Thery committed
264 265
	// end of table = nb elements
	m_maxSize = m_size;
Pierre Kraemer's avatar
Pierre Kraemer committed
266

Sylvain Thery's avatar
Sylvain Thery committed
267 268
	// no more blocks empty
	m_tableBlocksEmpty.clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
269

Sylvain Thery's avatar
Sylvain Thery committed
270 271 272 273 274 275 276 277 278 279 280 281
	// only the last block has free indices
	m_tableBlocksWithFree.clear();

	// compute nb block full
	unsigned int nbb = m_size / _BLOCKSIZE_;
	// update holeblock
	for (unsigned int i=0; i<nbb; ++i)
		m_holesBlocks[i]->compressFull(_BLOCKSIZE_);

	//update last holeblock
	unsigned int nbe = m_size % _BLOCKSIZE_;
	if (nbe != 0)
Pierre Kraemer's avatar
Pierre Kraemer committed
282
	{
Sylvain Thery's avatar
Sylvain Thery committed
283 284 285
		m_holesBlocks[nbb]->compressFull(nbe);
		m_tableBlocksWithFree.push_back(nbb);
		nbb++;
Pierre Kraemer's avatar
Pierre Kraemer committed
286 287
	}

Sylvain Thery's avatar
Sylvain Thery committed
288
	// free memory and resize
Sylvain Thery's avatar
Sylvain Thery committed
289
	for (int i = m_holesBlocks.size() - 1; i > int(nbb); --i)
Sylvain Thery's avatar
Sylvain Thery committed
290 291 292
		delete m_holesBlocks[i];
	m_holesBlocks.resize(nbb);

293

Sylvain Thery's avatar
Sylvain Thery committed
294
	// release unused data memory
295
	for(unsigned int j = 0; j < m_tableAttribs.size(); ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
296
	{
297 298
		if (m_tableAttribs[j] != NULL)
			m_tableAttribs[j]->setNbBlocks(m_holesBlocks.size());
Pierre Kraemer's avatar
Pierre Kraemer committed
299
	}
300 301
}

Sylvain Thery's avatar
Sylvain Thery committed
302

303 304 305
/**************************************
 *          LINES MANAGEMENT          *
 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
306

307 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 333 334 335 336 337 338 339 340 341 342 343
//unsigned int AttributeContainer::insertLine()
//{
//	// if no more rooms
//	if (m_tableBlocksWithFree.empty())
//	{
//		HoleBlockRef* ptr = new HoleBlockRef();					// new block
//		m_tableBlocksWithFree.push_back(m_holesBlocks.size());	// add its future position to block_free
//		m_holesBlocks.push_back(ptr);							// and add it to block table

//		for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
//		{
//			if (m_tableAttribs[i] != NULL)
//				m_tableAttribs[i]->addBlock();					// add a block to every attribute
//		}
//	}

//	// get the first free block index (last in vector)
//	unsigned int bf = m_tableBlocksWithFree.back();
//	// get the block
//	HoleBlockRef* block = m_holesBlocks[bf];

//	// add new element in block and compute index
//	unsigned int ne = block->newRefElt(m_maxSize);
//	unsigned int index = _BLOCKSIZE_ * bf + ne;

//	// if no more room in block remove it from free_blocks
//	if (block->full())
//		m_tableBlocksWithFree.pop_back();

//	++m_size;

////	initLine(index);

//	return index;
//}


344 345 346 347
unsigned int AttributeContainer::insertLine()
{
	// if no more rooms
	if (m_tableBlocksWithFree.empty())
Pierre Kraemer's avatar
Pierre Kraemer committed
348
	{
349
		HoleBlockRef* ptr = new HoleBlockRef();					// new block
350 351
		unsigned int numBlock = m_holesBlocks.size();
		m_tableBlocksWithFree.push_back(numBlock);	// add its future position to block_free
352 353 354 355 356 357 358
		m_holesBlocks.push_back(ptr);							// and add it to block table

		for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
		{
			if (m_tableAttribs[i] != NULL)
				m_tableAttribs[i]->addBlock();					// add a block to every attribute
		}
359 360 361 362 363 364 365 366

		// inc nb of elements
		++m_size;

		// add new element in block and compute index

		unsigned int ne = ptr->newRefElt(m_maxSize);
		return _BLOCKSIZE_ * numBlock + ne;
Pierre Kraemer's avatar
Pierre Kraemer committed
367
	}
368
	// else
Pierre Kraemer's avatar
Pierre Kraemer committed
369

370 371 372 373 374 375 376 377 378
	// get the first free block index (last in vector)
	unsigned int bf = m_tableBlocksWithFree.back();
	// get the block
	HoleBlockRef* block = m_holesBlocks[bf];

	// add new element in block and compute index
	unsigned int ne = block->newRefElt(m_maxSize);
	unsigned int index = _BLOCKSIZE_ * bf + ne;

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
	if (ne == _BLOCKSIZE_-1)
	{
		if (bf == (m_holesBlocks.size()-1))
		{
			// we are filling the last line of capacity
			HoleBlockRef* ptr = new HoleBlockRef();					// new block
			unsigned int numBlock = m_holesBlocks.size();
			m_tableBlocksWithFree.back() = numBlock;
			m_tableBlocksWithFree.push_back(bf);
			m_holesBlocks.push_back(ptr);

			for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
			{
				if (m_tableAttribs[i] != NULL)
					m_tableAttribs[i]->addBlock();					// add a block to every attribute
			}
		}
	}

398 399 400 401 402 403
	// if no more room in block remove it from free_blocks
	if (block->full())
		m_tableBlocksWithFree.pop_back();

	++m_size;
	return index;
Pierre Kraemer's avatar
Pierre Kraemer committed
404 405
}

406

407
void AttributeContainer::removeLine(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
408
{
409 410
	unsigned int bi = index / _BLOCKSIZE_;
	unsigned int j = index % _BLOCKSIZE_;
Pierre Kraemer's avatar
Pierre Kraemer committed
411

412
	HoleBlockRef* block = m_holesBlocks[bi];
Pierre Kraemer's avatar
Pierre Kraemer committed
413

414 415 416 417
	if (block->used(j))
	{
		if (block->full())		// block has no free elements before removal
			m_tableBlocksWithFree.push_back(bi);
Pierre Kraemer's avatar
Pierre Kraemer committed
418

419
		block->removeElt(j);
Pierre Kraemer's avatar
Pierre Kraemer committed
420

421
		--m_size;
Pierre Kraemer's avatar
Pierre Kraemer committed
422

423 424
		if (block->empty())		// block is empty after removal
			m_tableBlocksEmpty.push_back(bi);
Pierre Kraemer's avatar
Pierre Kraemer committed
425
	}
426
	else
427
		CGoGNerr << "Error removing non existing index " << index << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
428 429
}

430 431 432
/**************************************
 *            SAVE & LOAD             *
 **************************************/
Sylvain Thery's avatar
Sylvain Thery committed
433 434 435 436
//
//bool AttributeContainer::loadXmlBWF(xmlNodePtr node)
//{
//	xmlChar* prop = xmlGetProp(node, BAD_CAST "nb");
Pierre Kraemer's avatar
Pierre Kraemer committed
437
//	unsigned int nb = atoi((char*)prop);
Sylvain Thery's avatar
Sylvain Thery committed
438 439 440 441 442 443 444 445 446
//	m_tableBlocksWithFree.clear();
//
//	// charge et cree les  attributs
//	for (xmlNode* x_node = node->children; x_node != NULL; x_node = x_node->next)
//	{
//		unsigned int ind = atoi((char*)(xmlNodeGetContent(x_node)));
//		m_tableBlocksWithFree.push_back(ind);
//	}
//	if (m_tableBlocksWithFree.size() != nb)
Pierre Kraemer's avatar
Pierre Kraemer committed
447
//	{
Sylvain Thery's avatar
Sylvain Thery committed
448
//		CGoGNerr <<"Erreur lecture fichier XML incoherent"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
449 450
//		return false;
//	}
Sylvain Thery's avatar
Sylvain Thery committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
//	return true;
//}
//
//bool AttributeContainer::loadXmlAN(xmlNodePtr node, unsigned int nbb)
//{
//	xmlChar* prop = xmlGetProp(node, BAD_CAST "nb");
////	unsigned int nb = atoi((char*)prop);
//	prop = xmlGetProp(node, BAD_CAST "sv");
////	unsigned int sv = atoi((char*)prop);
//
//	// Noooooooo!!!!
////	m_tableAttribs.resize(sv);
////	for (unsigned int i=0; i< sv; ++i)
////			m_tableAttribs[i]=NULL;
//
//	// charge et cree les  attributs
//	for (xmlNode* x_node = node->children; x_node != NULL; x_node = x_node->next)
//	{
//		prop = xmlGetProp(x_node, BAD_CAST "id");
////		unsigned int id = unsigned int(atoi((char*)prop);
//
//		prop = xmlGetProp(x_node, BAD_CAST "type");
//		// recupere l'attribut enregistrer par son type
//		if (m_attributes_registry_map !=NULL)
//		{
//			std::map<std::string, RegisteredBaseAttribute*>::iterator itAtt = m_attributes_registry_map->find(std::string((char*)prop));
//			if (itAtt == m_attributes_registry_map->end())
478
//			{
Sylvain Thery's avatar
Sylvain Thery committed
479
//				CGoGNout << "Skipping non registred attribute "<< std::string((char*)prop)<<CGoGNendl;
480
//			}
Sylvain Thery's avatar
Sylvain Thery committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
//			else
//			{
//				RegisteredBaseAttribute* ra = itAtt->second;
//				prop = xmlGetProp(x_node, BAD_CAST "name");
////				ra->addAttribute(*this, std::string((char*)prop), id);
//				AttributeMultiVectorGen* amvg = ra->addAttribute(*this, std::string((char*)prop));
//				amvg->setNbBlocks(nbb);
//			}
//		}
//		else
//		{
//			CGoGNerr << "Attribute Registry non initialized"<< CGoGNendl;
//			return false;
//		}
//	}
////	if (m_attribNameMap.size() != nb)
////	{
////		CGoGNerr << "Pb lecture attributs"<< CGoGNendl;
////		return false;
////	}
//	return true;
//}
//
//bool AttributeContainer::loadXmlDL(xmlNodePtr node)
//{
//	// charge et cree les  attributs
//	for (xmlNode* x_node = node->children; x_node != NULL; x_node = x_node->next)
508
//	{
Sylvain Thery's avatar
Sylvain Thery committed
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
//		// get index
//		xmlChar* prop = xmlGetProp(x_node, BAD_CAST "id");
//		unsigned int id = atoi((char*)prop);
//		// get & set nbref
//		prop = xmlGetProp(x_node, BAD_CAST "refs");
//		unsigned int nbr = atoi((char*)prop);
//		setNbRefs(id, nbr);
//
//		if (nbr > 0)
//		{
////			for (MapNameId::iterator it = m_attribNameMap.begin(); it != m_attribNameMap.end(); ++it)
////			{
////				prop = xmlGetProp(x_node, BAD_CAST (it->first).c_str());
////				// if name of data unkwown then error
////				if (prop == NULL)
////				{
////					CGoGNerr << "inconsistent xml file"<<CGoGNendl;
////					return false;
////				}
////				m_tableAttribs[it->second]->input(id, std::string((char*)prop));
////			}
//		}
531
//	}
Sylvain Thery's avatar
Sylvain Thery committed
532 533 534 535 536 537 538 539 540 541 542 543
//	return true;
//}
//
//void AttributeContainer::saveXml(xmlTextWriterPtr writer, unsigned int id)
//{
//	if (m_nbAttributes == 0)
//		return;
//
//	// noeud du container
//	int rc = xmlTextWriterStartElement(writer, BAD_CAST "Attributes_Container");
//	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "id","%u",id);
//	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "BlockSize","%u",_BLOCKSIZE_);
544
//	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "size","%u",m_maxSize);
Sylvain Thery's avatar
Sylvain Thery committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
//
//	// recuperer le nombre d'attributs
//	unsigned int nbAtt = m_nbAttributes;
//	unsigned int sizeVectAtt = m_tableAttribs.size();
//
//	// noeud avec la liste de attributs
//	rc = xmlTextWriterStartElement(writer, BAD_CAST "Attributes_Names");
//	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "nb","%u",nbAtt);
//	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "sv","%u",sizeVectAtt);
//
//	// recuperer les attributs dans la map et les sauver
////	for (std::map<std::string, unsigned int>::iterator it = m_attribNameMap.begin(); it!= m_attribNameMap.end(); ++it)
////	{
////		int rc = xmlTextWriterStartElement(writer, BAD_CAST "Attribute");
////		rc = xmlTextWriterWriteAttribute(writer,  BAD_CAST "name",BAD_CAST (it->first).c_str());
////		const std::string& str_type = m_tableAttribs[it->second]->getTypeName();
////		rc = xmlTextWriterWriteAttribute(writer,  BAD_CAST "type",BAD_CAST str_type.c_str());
////		rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "id","%u",it->second);
////		rc = xmlTextWriterEndElement(writer);
////	}
//	// fin du noeud
//	rc = xmlTextWriterEndElement(writer);
//
//	// parcourir le container et sauver les lignes
//	rc = xmlTextWriterStartElement(writer, BAD_CAST "Data_Lines");
////	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "size","%u",m_maxSize);
//	for (unsigned int i = 0; i != m_maxSize; ++i)
//	{
//		unsigned int nbr = getNbRefs(i);
//		rc = xmlTextWriterStartElement(writer, BAD_CAST "Line");
//		rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "id","%u",i);
//		rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "refs","%u",nbr);
//		if (nbr > 0)
//		{
//			// tous les attributs de la ligne
////			for (MapNameId::iterator it = m_attribNameMap.begin(); it!= m_attribNameMap.end(); ++it)
////			{
////				std::string st_att = m_tableAttribs[it->second]->output(i);
////				rc = xmlTextWriterWriteAttribute(writer,(xmlChar*)( (it->first).c_str()), (xmlChar*)( st_att.c_str()));
////			}
//		}
//		// fin du noeud Line
//		rc = xmlTextWriterEndElement(writer);
//	}
//	// fin du noeud Data Lines
//	rc = xmlTextWriterEndElement(writer);
//
//	// fin du noeud Container
//	rc = xmlTextWriterEndElement(writer);
//}
//
//unsigned int AttributeContainer::getIdXmlNode(xmlNodePtr node)
//{
//	xmlChar *prop = xmlGetProp(node, BAD_CAST "id");
Pierre Kraemer's avatar
Pierre Kraemer committed
599
//	unsigned int id = atoi((char*)prop);
Sylvain Thery's avatar
Sylvain Thery committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
//	return id;
//}
//
//bool AttributeContainer::loadXml(xmlNodePtr node)
//{
//	xmlChar *prop = xmlGetProp(node, BAD_CAST "BlockSize");
//	unsigned int bs = atoi((char*)prop);
//
//	if (bs != _BLOCKSIZE_)
//	{
//		CGoGNerr << "Chargement impossible, tailles de block differentes: "<<_BLOCKSIZE_<<" / " << bs << CGoGNendl;
//		return false;
//	}
//
////	prop = xmlGetProp(node, BAD_CAST "id");
////	unsigned int id = atoi((char*)prop);
//
//	prop = xmlGetProp(node, BAD_CAST "size");
//	m_maxSize = atoi((char*)prop);
//
//	char* ANnode = (char*)"Attributes_Names";
//	char* DLnode= (char*)"Data_Lines";
//
//	// calcul le nombre de block et les alloue
//	unsigned int nbb = m_maxSize/_BLOCKSIZE_;
//	if (m_maxSize%_BLOCKSIZE_)
//			nbb++;
//
//	m_holesBlocks.resize(nbb);
//	for (unsigned int i=0; i<nbb; ++i)
//		m_holesBlocks[i] = new HoleBlockRef;
//
//	//load Attributes
//	xmlNode* cur = node->children;
//	while ( strcmp((char*)(cur->name),ANnode))
//		cur = cur->next;
//	loadXmlAN(cur,nbb);
//
//	cur = node->children;
//	while ( strcmp((char*)(cur->name),DLnode))
//		cur = cur->next;
//	loadXmlDL(cur);
//
//	// recreate free holes in blocks
//	nbb--;
//	for (unsigned int i = 0; i < nbb; ++i)
//	{
//		if (m_holesBlocks[i]->updateHoles(_BLOCKSIZE_))
//			m_tableBlocksWithFree.push_back(i);
//	}
//	m_holesBlocks[nbb]->updateHoles(m_maxSize - nbb * _BLOCKSIZE_);
//
//	return true;
//}
Pierre Kraemer's avatar
Pierre Kraemer committed
654

Sylvain Thery's avatar
Sylvain Thery committed
655
void AttributeContainer::saveBin(CGoGNostream& fs, unsigned int id) const
Pierre Kraemer's avatar
Pierre Kraemer committed
656
{
Sylvain Thery's avatar
Sylvain Thery committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
	std::vector<AttributeMultiVectorGen*> bufferamv;
	bufferamv.reserve(m_tableAttribs.size());

	for(std::vector<AttributeMultiVectorGen*>::const_iterator it = m_tableAttribs.begin(); it != m_tableAttribs.end(); ++it)
	{
		if (*it != NULL)
		{
			const std::string& attName = (*it)->getName();
			std::string markName = attName.substr(0,7);
			if (markName != "marker_")
				bufferamv.push_back(*it);
		}
	}


	// en ascii id et les tailles
Pierre Kraemer's avatar
Pierre Kraemer committed
673 674

	std::vector<unsigned int> bufferui;
Sylvain Thery's avatar
Sylvain Thery committed
675
	bufferui.reserve(10);
Pierre Kraemer's avatar
Pierre Kraemer committed
676 677 678 679 680

	bufferui.push_back(id);
	bufferui.push_back(_BLOCKSIZE_);
	bufferui.push_back(m_holesBlocks.size());
	bufferui.push_back(m_tableBlocksWithFree.size());
Sylvain Thery's avatar
Sylvain Thery committed
681 682
//	bufferui.push_back(m_nbAttributes);
	bufferui.push_back(bufferamv.size());
Pierre Kraemer's avatar
Pierre Kraemer committed
683 684
	bufferui.push_back(m_size);
	bufferui.push_back(m_maxSize);
Sylvain Thery's avatar
Sylvain Thery committed
685 686
	bufferui.push_back(m_orbit);
	bufferui.push_back(m_nbUnknown);
Pierre Kraemer's avatar
Pierre Kraemer committed
687

Sylvain Thery's avatar
Sylvain Thery committed
688

Sylvain Thery's avatar
Sylvain Thery committed
689
	fs.write(reinterpret_cast<const char*>(&bufferui[0]), bufferui.size()*sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
690

Pierre Kraemer's avatar
Pierre Kraemer committed
691
	unsigned int i = 0;
Sylvain Thery's avatar
Sylvain Thery committed
692

Sylvain Thery's avatar
Sylvain Thery committed
693
	for(std::vector<AttributeMultiVectorGen*>::const_iterator it = bufferamv.begin(); it != bufferamv.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
694
	{
695
		if (*it != NULL)
Sylvain Thery's avatar
Sylvain Thery committed
696 697 698 699 700 701
		{
			const std::string& attName = (*it)->getName();
			std::string markName = attName.substr(0,7);
			if (markName != "marker_")
				(*it)->saveBin(fs, i++);
		}
Sylvain Thery's avatar
Sylvain Thery committed
702
		else
Sylvain Thery's avatar
Sylvain Thery committed
703
		{
Sylvain Thery's avatar
Sylvain Thery committed
704
			CGoGNerr << "PB saving, NULL ptr in m_tableAttribs" <<  CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
705 706
			i++;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
707 708 709
	}

	//en binaire les blocks de ref
Sylvain Thery's avatar
Sylvain Thery committed
710
	for (std::vector<HoleBlockRef*>::const_iterator it = m_holesBlocks.begin(); it != m_holesBlocks.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
711
		(*it)->saveBin(fs);
Pierre Kraemer's avatar
Pierre Kraemer committed
712 713

	// les indices des blocks libres
714
	fs.write(reinterpret_cast<const char*>(&m_tableBlocksWithFree[0]), m_tableBlocksWithFree.size() * sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
715 716
}

717
unsigned int AttributeContainer::loadBinId(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
718 719 720 721 722 723
{
	unsigned int id;
	fs.read(reinterpret_cast<char*>(&id), sizeof(unsigned int));
	return id;
}

724
bool AttributeContainer::loadBin(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
725
{
726
	if (m_attributes_registry_map == NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
727
	{
728
		CGoGNerr << "Attribute Registry non initialized"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
729 730 731 732 733 734
		return false;
	}

	std::vector<unsigned int> bufferui;
	bufferui.resize(256);

Sylvain Thery's avatar
Sylvain Thery committed
735
	fs.read(reinterpret_cast<char*>(&(bufferui[0])), 8*sizeof(unsigned int));	//WARNING 9 hard coded
Pierre Kraemer's avatar
Pierre Kraemer committed
736

Pierre Kraemer's avatar
Pierre Kraemer committed
737
	unsigned int bs, szHB, szBWF, nbAtt;
Pierre Kraemer's avatar
Pierre Kraemer committed
738 739 740 741 742 743
	bs = bufferui[0];
	szHB = bufferui[1];
	szBWF = bufferui[2];
	nbAtt = bufferui[3];
	m_size = bufferui[4];
	m_maxSize = bufferui[5];
Sylvain Thery's avatar
Sylvain Thery committed
744 745 746
	m_orbit = bufferui[6];
	m_nbUnknown = bufferui[7];

Pierre Kraemer's avatar
Pierre Kraemer committed
747 748 749

	if (bs != _BLOCKSIZE_)
	{
750
		CGoGNerr << "Loading unavailable, different block sizes: "<<_BLOCKSIZE_<<" / " << bs << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
751 752 753
		return false;
	}

Sylvain Thery's avatar
Sylvain Thery committed
754

755
	for (unsigned int j = 0; j < nbAtt; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
756 757 758
	{
		std::string nameAtt;
		std::string typeAtt;
759
		/*unsigned int id = */AttributeMultiVectorGen::loadBinInfos(fs,nameAtt, typeAtt);
Pierre Kraemer's avatar
Pierre Kraemer committed
760 761 762 763

		std::map<std::string, RegisteredBaseAttribute*>::iterator itAtt = m_attributes_registry_map->find(typeAtt);
		if (itAtt == m_attributes_registry_map->end())
		{
764
			CGoGNout << "Skipping non registred attribute of type name"<< typeAtt <<CGoGNendl;
765
			AttributeMultiVectorGen::skipLoadBin(fs);
Pierre Kraemer's avatar
Pierre Kraemer committed
766 767 768 769
		}
		else
		{
			RegisteredBaseAttribute* ra = itAtt->second;
770 771
			AttributeMultiVectorGen* amvg = ra->addAttribute(*this, nameAtt);
			amvg->loadBin(fs);
Pierre Kraemer's avatar
Pierre Kraemer committed
772 773 774 775 776 777
		}
	}

	m_holesBlocks.resize(szHB);

	// blocks
Pierre Kraemer's avatar
Pierre Kraemer committed
778
	for (unsigned int i = 0; i < szHB; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
779 780 781 782 783 784 785
	{
		m_holesBlocks[i] = new HoleBlockRef;
		m_holesBlocks[i]->loadBin(fs);
	}

	// les indices des blocks libres
	m_tableBlocksWithFree.resize(szBWF);
786
	fs.read(reinterpret_cast<char*>(&(m_tableBlocksWithFree[0])), szBWF*sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
787 788 789 790

	return true;
}

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
void  AttributeContainer::copyFrom(const AttributeContainer& cont)
{
// 	clear is done from the map

	m_size = cont.m_size;
	m_maxSize = cont.m_maxSize;
	m_orbit = cont.m_orbit;
	m_nbUnknown = cont.m_nbUnknown;
	m_nbAttributes = cont.m_nbAttributes;
	m_lineCost = cont.m_lineCost;

	// blocks
	unsigned int sz = cont.m_holesBlocks.size();
	m_holesBlocks.resize(sz);
	for (unsigned int i = 0; i < sz; ++i)
		m_holesBlocks[i] = new HoleBlockRef(*(cont.m_holesBlocks[i]));

808 809 810 811 812 813
	//  free indices
	sz = cont.m_freeIndices.size();
	m_freeIndices.resize(sz);
	for (unsigned int i = 0; i < sz; ++i)
		m_freeIndices[i] = cont.m_freeIndices[i];

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
	// blocks with free
	sz = cont.m_tableBlocksWithFree.size();
	m_tableBlocksWithFree.resize(sz);
	for (unsigned int i = 0; i < sz; ++i)
		m_tableBlocksWithFree[i] = cont.m_tableBlocksWithFree[i];

	// empty blocks
	sz = cont.m_tableBlocksEmpty.size();
	m_tableBlocksEmpty.resize(sz);
	for (unsigned int i = 0; i < sz; ++i)
		m_tableBlocksEmpty[i] = cont.m_tableBlocksEmpty[i];

	//attributes (warning attribute can have different numbers than in original)
	m_tableAttribs.reserve(m_nbAttributes);
	sz = cont.m_tableAttribs.size();
	for (unsigned int i = 0; i < sz; ++i)
	{
		if (cont.m_tableAttribs[i] != NULL)
		{
833 834 835 836 837 838 839 840 841 842 843 844 845
			std::string sub = cont.m_tableAttribs[i]->getName().substr(0, 5);
			if (sub != "Mark_") // Mark leaved by
			{
				AttributeMultiVectorGen* ptr = cont.m_tableAttribs[i]->new_obj();
				ptr->setName(cont.m_tableAttribs[i]->getName());
				ptr->setOrbit(cont.m_tableAttribs[i]->getOrbit());
				ptr->setIndex(m_tableAttribs.size());
				ptr->setNbBlocks(cont.m_tableAttribs[i]->getNbBlocks());
				ptr->copy(cont.m_tableAttribs[i]);
				m_tableAttribs.push_back(ptr);
			}
			else
			{
846 847 848 849 850 851 852
				// get id of thread
				const std::string& str = cont.m_tableAttribs[i]->getName();
				unsigned int thId = (unsigned int)(str[5]-'0');
				if (str.size()==7)
					thId = 10*thId +  (unsigned int)(sub[6]-'0');
				// Mark always at the begin, because called after clear
				AttributeMultiVectorGen* ptr = m_tableAttribs[thId];
853 854 855
				ptr->setNbBlocks(cont.m_tableAttribs[i]->getNbBlocks());
				ptr->copy(cont.m_tableAttribs[i]);
			}
856 857 858 859
		}
	}
}

860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
void AttributeContainer::dumpCSV() const
{
	CGoGNout << "Name ; ;";
	for (unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
		{
			CGoGNout << m_tableAttribs[i]->getName() << " ; ";
		}
	}
	CGoGNout << CGoGNendl;
	CGoGNout << "Type  ; ;";
	for (unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
		{
			CGoGNout << m_tableAttribs[i]->getTypeName() << " ; ";
		}
	}
	CGoGNout << CGoGNendl;
	CGoGNout << "line ; refs ;";
	for (unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
		{
			CGoGNout << "value;";
		}
	}
	CGoGNout << CGoGNendl;

	for (unsigned int l=this->begin(); l!= this->end(); this->next(l))
	{
		CGoGNout << l << " ; "<< this->getNbRefs(l)<< " ; ";
		for (unsigned int i = 0; i < m_tableAttribs.size(); ++i)
		{
			if (m_tableAttribs[i] != NULL)
			{
				m_tableAttribs[i]->dump(l);
				CGoGNout << " ; ";
			}
		}
		CGoGNout << CGoGNendl;
	}
Sylvain Thery's avatar
Sylvain Thery committed
903
	CGoGNout << CGoGNendl;
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
}

void AttributeContainer::dumpByLines() const
{
	CGoGNout << "Container of "<<orbitName(this->getOrbit())<< CGoGNendl;
	for (unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
		{
			CGoGNout << "Name: "<< m_tableAttribs[i]->getName();
			CGoGNout << " / Type: "<< m_tableAttribs[i]->getTypeName();
			for (unsigned int l=this->begin(); l!= this->end(); this->next(l))
			{
				CGoGNout << l << " ; ";
				m_tableAttribs[i]->dump(l);
				CGoGNout << CGoGNendl;
			}

		}

	}
}



Pierre Kraemer's avatar
Pierre Kraemer committed
929
}