attributeContainer.cpp 22.8 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * attrib_container.cpp
 *
 *  Created on: Jun 10, 2010
 *      Author: thery
 */

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

15
#include "Container/attributeContainer.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
16
17
18
19

namespace CGoGN
{

20
AttributeContainer::AttributeContainer():
Pierre Kraemer's avatar
Pierre Kraemer committed
21
m_nbAttributes(0),
22
m_nbUnknown(0),
Pierre Kraemer's avatar
Pierre Kraemer committed
23
24
25
26
27
28
29
30
m_size(0),
m_maxSize(0),
m_lineCost(0),
m_attributes_registry_map(NULL)
{
	m_holesBlocks.reserve(512);
}

31
AttributeContainer::~AttributeContainer()
Pierre Kraemer's avatar
Pierre Kraemer committed
32
33
34
{
}

35
void AttributeContainer::swap(AttributeContainer& cont)
Pierre Kraemer's avatar
Pierre Kraemer committed
36
37
38
39
40
41
42
43
44
45
{
	m_holesBlocks.swap(cont.m_holesBlocks);
	m_tableBlocksWithFree.swap(cont.m_tableBlocksWithFree);
	m_tableAttribs.swap(cont.m_tableAttribs);
	m_attribNameMap.swap(cont.m_attribNameMap);

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

46
47
48
49
	temp = m_nbUnknown ;
	m_nbUnknown = cont.m_nbUnknown ;
	cont.m_nbUnknown = temp ;

Pierre Kraemer's avatar
Pierre Kraemer committed
50
51
52
53
54
55
56
57
58
59
60
61
62
	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;
}

63
bool AttributeContainer::removeAttribute(const std::string& attribName)
Pierre Kraemer's avatar
Pierre Kraemer committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{
	MapNameId::iterator it = m_attribNameMap.find(attribName);
	if (it == m_attribNameMap.end())
	{
		std::cerr << "removeAttribute by name: attribut inconnu" << std::endl;
		return false;
	}

	// delete the attribute
	delete (m_tableAttribs[it->second]);
	m_tableAttribs[it->second] = NULL;

	// remove the attribute from the name map
	m_attribNameMap.erase(it);

	m_nbAttributes--;

	return true;
}

84
bool AttributeContainer::removeAttribute(unsigned int index)
Pierre Kraemer's avatar
Pierre Kraemer committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{
	MapNameId::iterator it = m_attribNameMap.begin();
	while ((it != m_attribNameMap.end()) && (it->second != index))
		++it;
	if (it != m_attribNameMap.end())
	{
		// remove the attribute from the name map
		m_attribNameMap.erase(it);
	}
	// else it was an attribute without name

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

	m_nbAttributes--;

	return true;
}

105
void AttributeContainer::merge(AttributeContainer& cont)
Pierre Kraemer's avatar
Pierre Kraemer committed
106
107
108
109
110
111
112
{
	/*
	// tableau de correspondance pour reperer les tables de cont et les mettre ou il faut dans this
	std::vector<unsigned int> newId;
	newId.resize(m_tableAttribs.size() + cont.m_tableAttribs.size());


113
	unsigned int nbb = m_holesBlocks.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
114
115
116
117
118
119
120
121
122
123
124
125

	// parcours des attributs de cont pour les ajouter dans this et stocker la correspondance
	for (MapNameId::iterator it = cont.m_attribNameMap.begin(); it != cont.m_attribNameMap.end(); ++it)
	{
		MapNameId::iterator jt = m_attribNameMap.find(it->first);
		if ( jt == m_attribNameMap.end() )
		{
			unsigned int idxAttrib = m_tableAttribs.size();
			m_attribNameMap.insert(std::pair<std::string, unsigned int>(it->first,idxAttrib));
			m_nbAttributes++;

			newId[idxAttrib] = it->second;
126
			AttributeMultiVectorGen* ptr = cont.m_tableAttribs[it->second];
Pierre Kraemer's avatar
Pierre Kraemer committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
			ptr->addBlocksBefore(nbb);
			m_tableAttribs.push_back( ptr );
		}
		else
		{
			newId[jt->second] = it->second;
		}
	}

	// ajoute les donnees de cont à la fin des tables de this si necessaire
	for (MapNameId::iterator it = m_attribNameMap.begin(); it != m_attribNameMap.end(); ++it)
	{
		if (m_tableAttribs.size() == nbb)
		{
			unsigned int id = it->second;
			m_tableAttribs[id]->merge(cont.m_tableAttribs[ newId[id] ]);
		}
	}

	// merge des trous !!
	for(std::vector<  HoleBlockRef* >::iterator it = cont.m_holesBlocks.begin(); it != cont.m_holesBlocks.end(); ++it)
	{
		m_holesBlocks.push_back(*it);
	}

	// decalage entre le
	unsigned int shift_index = nbb*_BLOCKSIZE_;

	//et des block avec des trous !
	for (std::vector<unsigned int>::iterator it = cont.m_tableBlocksWithFree.begin(); it != cont.m_tableBlocksWithFree.end(); ++it)
	{
		m_tableBlocksWithFree.push_back(shift_index + (*it));
	}

	// MAJ du nbre d'elts
	m_size += cont.m_size;

	// MAJ de la taille max (id dernier elt +1)
	m_maxSize = m_size + cont.m_maxSize;

	return shift_index;
	*/
}

171
void AttributeContainer::saveXml(xmlTextWriterPtr writer, unsigned int id)
Pierre Kraemer's avatar
Pierre Kraemer committed
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
{
	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_);
	rc = xmlTextWriterWriteFormatAttribute(writer,  BAD_CAST "size","%u",m_maxSize);


	// recuperer le nombre d'attributs
	unsigned int nbAtt = m_attribNameMap.size();
	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 = nbRefs(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);

}

235
bool AttributeContainer::loadXmlBWF(xmlNodePtr node)
Pierre Kraemer's avatar
Pierre Kraemer committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
{
	xmlChar* prop = xmlGetProp(node, BAD_CAST "nb");
	unsigned int nb = atoi((char*)prop);
	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)
	{
		std::cerr <<"Erreur lecture fichier XML incoherent"<< std::endl;
		return false;
	}
	return true;
}

255
bool AttributeContainer::loadXmlAN(xmlNodePtr node, unsigned int nbb)
Pierre Kraemer's avatar
Pierre Kraemer committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
{
	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())
			{
				std::cout << "Skipping non registred attribute "<< std::string((char*)prop)<<std::endl;
			}
			else
			{
				RegisteredBaseAttribute* ra = itAtt->second;
				prop = xmlGetProp(x_node, BAD_CAST "name");
//				ra->addAttribute(*this, std::string((char*)prop), id);
				unsigned int idAtt = ra->addAttribute(*this, std::string((char*)prop));
				m_tableAttribs[idAtt]->setNbBlocks(nbb);
			}
		}
		else
		{
			std::cerr << "Attribute Registry non initialized"<< std::endl;
			return false;
		}
	}
//	if (m_attribNameMap.size() != nb)
//	{
//		std::cerr << "Pb lecture attributs"<< std::endl;
//		return false;
//	}
	return true;
}

306
bool AttributeContainer::loadXmlDL(xmlNodePtr node)
Pierre Kraemer's avatar
Pierre Kraemer committed
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
{
	// charge et cree les  attributs
	for (xmlNode* x_node = node->children; x_node!=NULL; x_node = x_node->next)
	{
		// 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);
		setRefLine(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)
				{
					std::cerr << "inconsistent xml file"<<std::endl;
					return false;
				}
				m_tableAttribs[it->second]->input(id,std::string((char*)prop));
			}
		}

	}

	return true;
}


340
unsigned int AttributeContainer::getIdXmlNode(xmlNodePtr node)
Pierre Kraemer's avatar
Pierre Kraemer committed
341
342
343
344
345
346
{
	xmlChar *prop = xmlGetProp(node, BAD_CAST "id");
	unsigned int id = atoi((char*)prop);
	return id;
}

347
bool AttributeContainer::loadXml(xmlNodePtr node)
Pierre Kraemer's avatar
Pierre Kraemer committed
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
{
	xmlChar *prop = xmlGetProp(node, BAD_CAST "BlockSize");
	unsigned int bs = atoi((char*)prop);

	if (bs != _BLOCKSIZE_)
	{
		std::cerr << "Chargement impossible, tailles de block differentes: "<<_BLOCKSIZE_<<" / " << bs << std::endl;
		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;
}

402
void AttributeContainer::saveBin(CGoGNostream& fs, unsigned int id)
Pierre Kraemer's avatar
Pierre Kraemer committed
403
{
Pierre Kraemer's avatar
Pierre Kraemer committed
404
	// en ascii id et taille les tailles
Pierre Kraemer's avatar
Pierre Kraemer committed
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420

	std::vector<unsigned int> bufferui;
	bufferui.reserve(6);

	bufferui.push_back(id);
	bufferui.push_back(_BLOCKSIZE_);
	bufferui.push_back(m_holesBlocks.size());
	bufferui.push_back(m_tableBlocksWithFree.size());
	bufferui.push_back(m_nbAttributes);
	bufferui.push_back(m_size);
	bufferui.push_back(m_maxSize);

	std::cout << "Save Container: id:" <<id <<"  nbAtt:"<<m_nbAttributes << "  size:"<<m_size<<std::endl;

	fs.write(reinterpret_cast<const char*>(&bufferui[0]) ,bufferui.size()*sizeof(unsigned int));

Pierre Kraemer's avatar
Pierre Kraemer committed
421
	unsigned int i = 0;
422
	for(std::vector<AttributeMultiVectorGen*>::iterator it=m_tableAttribs.begin(); it!=m_tableAttribs.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
423
424
	{
		if (*it !=NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
425
			(*it)->saveBin(fs, i++);
Pierre Kraemer's avatar
Pierre Kraemer committed
426
427
428
	}

	//en binaire les blocks de ref
Pierre Kraemer's avatar
Pierre Kraemer committed
429
430
	for (std::vector<HoleBlockRef*>::iterator it = m_holesBlocks.begin(); it != m_holesBlocks.end(); ++it)
		(*it)->saveBin(fs);
Pierre Kraemer's avatar
Pierre Kraemer committed
431
432

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

436
unsigned int AttributeContainer::loadBinId(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
437
438
439
440
441
442
{
	unsigned int id;
	fs.read(reinterpret_cast<char*>(&id), sizeof(unsigned int));
	return id;
}

443
bool AttributeContainer::loadBin(CGoGNistream& fs)
Pierre Kraemer's avatar
Pierre Kraemer committed
444
445
446
447
448
449
450
451
452
453
454
455
{
	if (m_attributes_registry_map ==NULL)
	{
		std::cerr << "Attribute Registry non initialized"<< std::endl;
		return false;
	}

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

	fs.read(reinterpret_cast<char*>(&(bufferui[0])), 6*sizeof(unsigned int));

Pierre Kraemer's avatar
Pierre Kraemer committed
456
	unsigned int bs, szHB, szBWF, nbAtt;
Pierre Kraemer's avatar
Pierre Kraemer committed
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
	bs = bufferui[0];
	szHB = bufferui[1];
	szBWF = bufferui[2];
	nbAtt = bufferui[3];
	m_size = bufferui[4];
	m_maxSize = bufferui[5];

	if (bs != _BLOCKSIZE_)
	{
		std::cerr << "Chargement impossible, tailles de block differentes: "<<_BLOCKSIZE_<<" / " << bs << std::endl;
		return false;
	}

	for (unsigned int j=0; j < nbAtt; ++j)
	{
		std::string nameAtt;
		std::string typeAtt;
474
		/*unsigned int id = */AttributeMultiVectorGen::loadBinInfos(fs,nameAtt, typeAtt);
Pierre Kraemer's avatar
Pierre Kraemer committed
475
476
477
478
479

		std::map<std::string, RegisteredBaseAttribute*>::iterator itAtt = m_attributes_registry_map->find(typeAtt);
		if (itAtt == m_attributes_registry_map->end())
		{
			std::cout << "Skipping non registred attribute of type name"<< typeAtt <<std::endl;
480
			AttributeMultiVectorGen::skipLoadBin(fs);
Pierre Kraemer's avatar
Pierre Kraemer committed
481
482
483
484
485
		}
		else
		{
			RegisteredBaseAttribute* ra = itAtt->second;
			unsigned int idAtt = ra->addAttribute(*this, nameAtt);
Pierre Kraemer's avatar
Pierre Kraemer committed
486
			std::cout << "loading attribute " << nameAtt << " : " << typeAtt << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
487
488
489
490
491
492
493
494
495
			m_tableAttribs[idAtt]->loadBin(fs);
			// no need the set the nb of block (done by binary read of attribmv)
			m_nbAttributes++;
		}
	}

	m_holesBlocks.resize(szHB);

	// blocks
Pierre Kraemer's avatar
Pierre Kraemer committed
496
	for (unsigned int i = 0; i < szHB; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
497
498
499
500
501
502
503
	{
		m_holesBlocks[i] = new HoleBlockRef;
		m_holesBlocks[i]->loadBin(fs);
	}

	// les indices des blocks libres
	m_tableBlocksWithFree.resize(szBWF);
504
	fs.read(reinterpret_cast<char*>(&(m_tableBlocksWithFree[0])), szBWF*sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
505
506
507
508
509
510
511
512

	return true;
}

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

513
unsigned int AttributeContainer::getAttribute(const std::string& attribName)
Pierre Kraemer's avatar
Pierre Kraemer committed
514
515
516
517
518
519
520
521
522
{
	MapNameId::iterator it = m_attribNameMap.find(attribName);
	if (it == m_attribNameMap.end())
	{
		return UNKNOWN;
	}
	return it->second;
}

523
bool AttributeContainer::isValidAttribute(unsigned int attr)
Pierre Kraemer's avatar
Pierre Kraemer committed
524
525
526
527
528
529
530
531
532
533
534
535
536
{
	if (attr == UNKNOWN)
		return false;

	for (MapNameId::iterator it = m_attribNameMap.begin(); it!= m_attribNameMap.end(); ++it)
	{
		if (it->second == attr)
			return true;
	}

	return false;
}

537
unsigned int AttributeContainer::getAttributesStrings(std::vector<std::string>& strings)
Pierre Kraemer's avatar
Pierre Kraemer committed
538
539
540
541
542
543
544
545
546
547
548
549
550
{
	strings.clear();

	strings.resize(m_tableAttribs.size());

	for (MapNameId::iterator it = m_attribNameMap.begin(); it!= m_attribNameMap.end(); ++it)
	{
		strings[it->second] = it->first;
	}

	return m_attribNameMap.size();
}

551
const std::string& AttributeContainer::getAttributeName(unsigned int attrIndex)
Pierre Kraemer's avatar
Pierre Kraemer committed
552
553
554
555
556
557
558
559
560
561
562
{
	for (MapNameId::iterator it = m_attribNameMap.begin(); it!= m_attribNameMap.end(); ++it)
	{
		if(it->second == attrIndex)
			return it->first;
	}

	std::cerr << "Fail finding attribute name" << std::endl;
	return  m_attribNameMap.begin()->first;		// just for compiling
}

563
unsigned int AttributeContainer::nbAttributes()
Pierre Kraemer's avatar
Pierre Kraemer committed
564
565
566
567
568
569
570
571
{
	return m_attribNameMap.size();
}

///////////////////////////
//   GESTION DES LIGNES
///////////////////////////

572
unsigned int AttributeContainer::insertLine()
Pierre Kraemer's avatar
Pierre Kraemer committed
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
{
	// 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();						//ajoute un bloc a chaque attribut
		}
	}

	// 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 elt in block and compute index
	unsigned int ne = block->newRefElt(m_maxSize);
	unsigned int idx = _BLOCKSIZE_*bf + ne;

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

	m_size++;

//	// init the line (all attributes)
//	for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
//	{
//		if (m_tableAttribs[i] != NULL)
//			m_tableAttribs[i]->initElt(idx);
//	}

	return idx;
}

613
void AttributeContainer::initLine(unsigned int idx)
Pierre Kraemer's avatar
Pierre Kraemer committed
614
615
616
617
618
619
620
621
{
	for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
			m_tableAttribs[i]->initElt(idx);
	}
}

622
void AttributeContainer::removeLine(unsigned int eltIdx)
Pierre Kraemer's avatar
Pierre Kraemer committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
{
	unsigned int bi = eltIdx / _BLOCKSIZE_;
	unsigned int j = eltIdx % _BLOCKSIZE_;

	HoleBlockRef* block = m_holesBlocks[bi];

	if (block->used(j))
	{
		if (block->full())		// block has no free elts before removing
			m_tableBlocksWithFree.push_back(bi);

		block->removeElt(j);

		m_size--;

		if (block->empty())		// block is empty after removing
			m_tableBlocksEmpty.push_back(bi);
	}
	else
	{
		std::cout << "Error removing non existing index " << eltIdx << std::endl;
	}
}

647
void AttributeContainer::copyLine(unsigned int dstIdx, unsigned int srcIdx)
Pierre Kraemer's avatar
Pierre Kraemer committed
648
649
650
651
652
653
654
655
656
657
658
659
{
	for(unsigned int i = 0; i < m_tableAttribs.size(); ++i)
	{
		if (m_tableAttribs[i] != NULL)
			m_tableAttribs[i]->copyElt(dstIdx, srcIdx);
	}
}

///////////////////////////
// GESTION DES ITERATEURS
///////////////////////////

660
//void AttributeContainer::next(iterator& it)
Pierre Kraemer's avatar
Pierre Kraemer committed
661
662
663
664
665
666
667
//{
//	do
//	{
//		++it;
//	} while ((it<m_maxSize) && (!used(it)));
//}

668
//AttributeContainer::iterator AttributeContainer::AttributeContainer::end()
Pierre Kraemer's avatar
Pierre Kraemer committed
669
670
671
672
673
674
675
676
677
678
679
680
681
//{
//	return m_maxSize;
//
////	if (m_holesBlocks.empty())
////		return 0;
////	return (m_holesBlocks.back())->sizeTable() + (m_holesBlocks.size()-1)*_BLOCKSIZE_;
//}


///////////////////////////
// GESTION DES TAILLES
///////////////////////////

682
unsigned int AttributeContainer::size()
Pierre Kraemer's avatar
Pierre Kraemer committed
683
684
685
686
{
	return m_size;
}

687
unsigned int AttributeContainer::capacity()
Pierre Kraemer's avatar
Pierre Kraemer committed
688
689
690
691
{
	return m_holesBlocks.size()*_BLOCKSIZE_;
}

692
unsigned int AttributeContainer::memoryTotalSize()
Pierre Kraemer's avatar
Pierre Kraemer committed
693
694
695
696
{
	return _BLOCKSIZE_*(m_holesBlocks.size()*m_lineCost + 8);
}

697
unsigned int AttributeContainer::nbRefs(unsigned int it)
Pierre Kraemer's avatar
Pierre Kraemer committed
698
699
700
701
702
703
704
705
706
707
708
709
{
	unsigned int bi = it / _BLOCKSIZE_;
	unsigned int j = it % _BLOCKSIZE_;

	return m_holesBlocks[bi]->nbRefs(j);
}


///////////////////////////
//   COMPACTAGE
///////////////////////////

710
void AttributeContainer::compact(std::vector<unsigned int>& mapOldNew)
Pierre Kraemer's avatar
Pierre Kraemer committed
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
{
	unsigned int nbe = _BLOCKSIZE_*m_holesBlocks.size();

	unsigned int nbb = m_holesBlocks.size()-1;
	while ((m_holesBlocks[nbb])->empty())
	{
		nbb--;
		nbe -= _BLOCKSIZE_;
	}
	nbb++;

	mapOldNew.reserve(nbe);

	// now get the holes
	unsigned int baseAdr=0;
	for (unsigned int i=0; i< nbb; ++i)
	{
		HoleBlockRef* block = m_holesBlocks[i];

		for (unsigned int j=0; j<_BLOCKSIZE_; ++j)
		{
			if ( j < block->sizeTable())
			{
				if (block->used(j))
				{
					mapOldNew.push_back(baseAdr);
				}
				else
				{
					mapOldNew.push_back(0xffffffff);
				}
			}
			else
			{
				mapOldNew.push_back(0xffffffff);
			}
			baseAdr++;
		}
	}

	unsigned int last = mapOldNew.size() - 1;

 	for (unsigned int i=0 ; i <last; ++i)
	{
		unsigned int val = mapOldNew[i];
		if (val == 0xffffffff)
		{
			// first find last element
			while (mapOldNew[last] ==  0xffffffff)
				--last;

			// store it in the hole
			// find the blocks and indices
			unsigned int bi = i / _BLOCKSIZE_;
			unsigned int ib = i % _BLOCKSIZE_;
			unsigned int bj = last / _BLOCKSIZE_;
			unsigned int jb = last % _BLOCKSIZE_;

			//overwrite attributes
			for(unsigned int j=0; j< m_tableAttribs.size(); ++j)
			{
				if (m_tableAttribs[j]!=NULL)
					m_tableAttribs[j]->overwrite(bj,jb,bi,ib);
			}

			// overwrite emptyLine with last line in free blocks
			m_holesBlocks[bi]->overwrite(ib, m_holesBlocks[bj], jb);

			// set the map value
			mapOldNew[last] = i;
			last--;
		}
	}

	for (int i=m_holesBlocks.size()-1; i>=0; --i)
	{
		HoleBlockRef* ptr = m_holesBlocks[i];
		if ( ptr->compressFree())
		{
			delete ptr;
			m_holesBlocks.pop_back();
		}
	}

	// maj de la table de block libre
	m_tableBlocksWithFree.clear();
	HoleBlockRef* block = m_holesBlocks.back();
	if (! block->full())
	{
		m_tableBlocksWithFree.push_back(m_holesBlocks.size()-1);
	}

	// detruit les blocks de donnees inutiles
	for(unsigned int j=0; j< m_tableAttribs.size(); ++j)
	{
		if (m_tableAttribs[j]!=NULL)
			m_tableAttribs[j]->setNbBlocks(m_holesBlocks.size());
	}

	m_maxSize = (m_holesBlocks.back())->sizeTable() + (m_holesBlocks.size()-1)*_BLOCKSIZE_;
}


///////////////////////////
//   NETTOYAGE
///////////////////////////

818
void AttributeContainer::clear(bool clearAttrib)
Pierre Kraemer's avatar
Pierre Kraemer committed
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
{
	m_size = 0;
 	m_maxSize = 0;

	std::vector< HoleBlockRef* > bf;
	std::vector< unsigned int > bwf;

	// raz des cases libres
	for (std::vector< HoleBlockRef* >::iterator it = m_holesBlocks.begin(); it!= m_holesBlocks.end(); ++it)
	{
		delete (*it);
	}
	m_holesBlocks.swap(bf);
	m_tableBlocksWithFree.swap(bwf);

	// detruit les données
835
	for (std::vector< AttributeMultiVectorGen* >::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
	{
		if ((*it) != NULL)
			(*it)->free();
	}

	// on enleve les attributs ?
	if (clearAttrib)
	{
		// nb a zero
		m_nbAttributes = 0;
		// detruit la map nom-attribut
		m_attribNameMap.clear();

		// detruit tous les attributs
850
851
		std::vector< AttributeMultiVectorGen* > amg;
		for (std::vector< AttributeMultiVectorGen* >::iterator it = m_tableAttribs.begin(); it != m_tableAttribs.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
852
853
854
855
856
857
858
859
860
861
862
863
		{
			if ((*it) != NULL)
				delete (*it);
		}
		m_tableAttribs.swap(amg);
	}
}

///////////////////////////
// OLD INLINE FUNCTIONS
///////////////////////////

864
void AttributeContainer::affect(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
865
{
866
	for (std::vector< AttributeMultiVectorGen* >::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
867
868
869
870
871
872
	{
		if (*it != NULL)
			(*it)->affect(i, j);
	}
}

873
void AttributeContainer::add(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
874
{
875
	for (std::vector< AttributeMultiVectorGen* >::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
876
877
878
879
880
881
	{
		if ((*it) != NULL)
			(*it)->add(i, j);
	}
}

882
void AttributeContainer::sub(unsigned int i, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
883
{
884
	for (std::vector< AttributeMultiVectorGen* >::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
885
886
887
888
889
890
	{
		if ((*it) != NULL)
			(*it)->sub(i, j);
	}
}

891
void AttributeContainer::mult(unsigned int i, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
892
{
893
	for (std::vector< AttributeMultiVectorGen* >::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
894
895
896
897
898
899
	{
		if ((*it) != NULL)
			(*it)->mult(i, alpha);
	}
}

900
void AttributeContainer::div(unsigned int i, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
901
{
902
	for (std::vector< AttributeMultiVectorGen* >::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
903
904
905
906
907
908
	{
		if ((*it) != NULL)
			(*it)->div(i, alpha);
	}
}

909
void AttributeContainer::lerp(unsigned res, unsigned int i, unsigned int j, double alpha)
Pierre Kraemer's avatar
Pierre Kraemer committed
910
{
911
	for (std::vector< AttributeMultiVectorGen* >::iterator it = m_tableAttribs.begin(); it!=  m_tableAttribs.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
912
913
914
915
916
917
918
919
	{
		if ((*it) != NULL)
			(*it)->lerp(res, i, j, alpha);
	}
}

}