attributeContainer.h 11.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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25 26
#ifndef __ATTRIBUTE_CONTAINER__
#define __ATTRIBUTE_CONTAINER__
Pierre Kraemer's avatar
Pierre Kraemer committed
27 28 29

#include "Container/sizeblock.h"
#include "Container/holeblockref.h"
30
#include "Container/attributeMultiVector.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
31 32 33 34 35 36 37 38 39 40 41 42 43

#include <vector>
#include <map>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#include <libxml/parser.h>

namespace CGoGN
{

class RegisteredBaseAttribute;

/**
44 45 46 47
 * Container for AttributeMultiVectors
 * All the attributes always have the same size and
 * the management of holes is shared by all attributes
 */
48
class AttributeContainer
Pierre Kraemer's avatar
Pierre Kraemer committed
49
{
50
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
51
	/**
52
	* constante d'attribut inconnu
Pierre Kraemer's avatar
Pierre Kraemer committed
53
	*/
54
	static const unsigned int UNKNOWN = 0xffffffff;
Pierre Kraemer's avatar
Pierre Kraemer committed
55 56

	/**
57
	* Taille du bloc
Pierre Kraemer's avatar
Pierre Kraemer committed
58
	*/
Sylvain Thery's avatar
Sylvain Thery committed
59
//	static const unsigned int BlockSize = _BLOCKSIZE_;
Pierre Kraemer's avatar
Pierre Kraemer committed
60

61
protected:
Pierre Kraemer's avatar
Pierre Kraemer committed
62
	/**
63
	* vector of pointers to AttributeMultiVectors
Pierre Kraemer's avatar
Pierre Kraemer committed
64
	*/
65
	std::vector<AttributeMultiVectorGen*> m_tableAttribs;
Pierre Kraemer's avatar
Pierre Kraemer committed
66

67 68 69
	/**
	 * vector of free indices in the vector of AttributeMultiVectors
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
70 71
	std::vector<unsigned int> m_freeIndices;

Pierre Kraemer's avatar
Pierre Kraemer committed
72
	/**
73
	* vector of pointers to HoleBlockRef -> structure that manages holes and refs
Pierre Kraemer's avatar
Pierre Kraemer committed
74
	*/
75
	std::vector<HoleBlockRef*> m_holesBlocks;
Pierre Kraemer's avatar
Pierre Kraemer committed
76

77 78 79 80 81 82 83 84 85
	/**
	* vector of indices of blocks that have free space
	*/
	std::vector<unsigned int> m_tableBlocksWithFree;

	/**
	* vector of indices of blocks that are empty
	*/
	std::vector<unsigned int> m_tableBlocksEmpty;
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87

	/**
88 89 90 91 92 93
	 * orbit of the container
	 */
	unsigned int m_orbit;

	/**
	* number of attributes
Pierre Kraemer's avatar
Pierre Kraemer committed
94 95 96
	*/ 
	unsigned int m_nbAttributes;

97 98 99 100 101
	/**
	 * counter for attributes without name
	 */
	unsigned int m_nbUnknown;

Pierre Kraemer's avatar
Pierre Kraemer committed
102 103 104 105 106 107 108 109 110 111 112
	/**
	* size (number of elts) of the container
	*/
	unsigned int m_size;

	/**
	* size of the container with holes
	*/
	unsigned int m_maxSize;

	/**
113
	* memory cost of each line
Pierre Kraemer's avatar
Pierre Kraemer committed
114 115 116 117
	*/
	unsigned int m_lineCost;

	/**
118
	 * map pointer (shared for all container of the same map) for attribute registration
Pierre Kraemer's avatar
Pierre Kraemer committed
119
	 */
120
	std::map<std::string, RegisteredBaseAttribute*>* m_attributes_registry_map;
Pierre Kraemer's avatar
Pierre Kraemer committed
121

122 123
public:
	AttributeContainer();
Pierre Kraemer's avatar
Pierre Kraemer committed
124

125 126 127 128 129 130 131 132 133 134
	~AttributeContainer();

	unsigned int getOrbit();
	void setOrbit(unsigned int orbit);

	void setRegistry(std::map<std::string, RegisteredBaseAttribute*>* re);

	/**************************************
	 *          BASIC FEATURES            *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
135 136

	/**
137 138 139 140
	 * add a new attribute to the container
	 * @param T (template) type of the new attribute
	 * @param attribName name of the new attribute
	 * @return pointer to the new AttributeMultiVector
Pierre Kraemer's avatar
Pierre Kraemer committed
141
	 */
142 143
	template <typename T>
	AttributeMultiVector<T>* addAttribute(const std::string& attribName);
Pierre Kraemer's avatar
Pierre Kraemer committed
144

145
protected:
Pierre Kraemer's avatar
Pierre Kraemer committed
146
	/**
147 148 149 150 151
	 * add a new attribute with a given index (for load only)
	 * @param T (template) type of the new attribute
	 * @param attribName name of the new attribute
	 * @param typeName name of the new attribute's type
	 * @param index index of the new attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
152
	 */
153 154
	template <typename T>
	void addAttribute(const std::string& attribName, const std::string& typeName, unsigned int index);
Pierre Kraemer's avatar
Pierre Kraemer committed
155 156 157

public:
	/**
158 159 160
	* Remove an attribute (destroys data)
	* @param attribName name of the attribute to remove
	* @return removed or not
Pierre Kraemer's avatar
Pierre Kraemer committed
161
	*/
162
	template <typename T>
163
	bool removeAttribute(const std::string& attribName);
Pierre Kraemer's avatar
Pierre Kraemer committed
164 165

	/**
166 167 168
	* Remove an attribute (destroys data)
	* @param index index of the attribute to remove
	* @return removed or not
Pierre Kraemer's avatar
Pierre Kraemer committed
169
	*/
170
	template <typename T>
171 172 173 174 175
	bool removeAttribute(unsigned int index);

	/**************************************
	 *      INFO ABOUT THE CONTAINER      *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
176 177

	/**
178 179 180
	 * Number of attributes of the container
	 */
	unsigned int getNbAttributes() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182

	/**
183
	* Size of the container (number of lines)
Pierre Kraemer's avatar
Pierre Kraemer committed
184
	*/
185
	unsigned int size() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
186 187

	/**
188
	* Capacity of the container (number of lines including holes)
Pierre Kraemer's avatar
Pierre Kraemer committed
189
	*/
190
	unsigned int capacity() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
191 192

	/**
193 194 195
	* Total memory cost of container
	*/
	unsigned int memoryTotalSize() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
196

197 198 199 200 201
	/**
	* Memory cost of every used line
	*/
	unsigned int memorySize() const;

Pierre Kraemer's avatar
Pierre Kraemer committed
202
	/**
203 204 205
	* is the line used in the container
	*/
	inline bool used(unsigned int index) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
206

207 208 209
	/**************************************
	 *         CONTAINER TRAVERSAL        *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
210 211

	/**
212
	 * return the index of the first line of the container
Pierre Kraemer's avatar
Pierre Kraemer committed
213
	 */
214
	unsigned int begin() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
215 216

	/**
217
	 * return the index of the last line of the container
Pierre Kraemer's avatar
Pierre Kraemer committed
218
	 */
219
	unsigned int end() const;
Pierre Kraemer's avatar
Pierre Kraemer committed
220 221

	/**
222 223
	 * get the index of the line after it in the container
	 * MUST BE USED INSTEAD OF ++ !
Pierre Kraemer's avatar
Pierre Kraemer committed
224
	 */
225
	void next(unsigned int &it) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
226

227 228 229
	/**************************************
	 *       INFO ABOUT ATTRIBUTES        *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
230 231 232 233

	/**
	* recuperation du code d'un attribut
	* @param attribName nom de l'attribut
234
	* @return l'indice de l'attribut
Pierre Kraemer's avatar
Pierre Kraemer committed
235
	*/
236
	unsigned int getAttributeIndex(const std::string& attribName);
Pierre Kraemer's avatar
Pierre Kraemer committed
237 238 239 240 241 242

	/**
	 * get the name of an attribute, given its index in the container
	 */
	const std::string& getAttributeName(unsigned int attrIndex);

Pierre Kraemer's avatar
Pierre Kraemer committed
243
	/**
244 245 246 247 248
	 * fill a vector with pointers to the blocks of the given attribute
	 * @param attrIndex index of the attribute
	 * @param vect_addr (OUT) vector of pointers
	 * @param byteBlockSize (OUT) size in bytes of each block
	 * @return number of blocks
Pierre Kraemer's avatar
Pierre Kraemer committed
249
	 */
250 251
	template<typename T>
	unsigned int getAttributeBlocksPointers(unsigned int attrIndex, std::vector<T*>& vect_ptr, unsigned int& byteBlockSize);
Pierre Kraemer's avatar
Pierre Kraemer committed
252 253

	/**
Sylvain Thery's avatar
Sylvain Thery committed
254 255 256
	 * fill a vector with attributes names
	 * @param names vector of names
	 * @return number of attributes
Pierre Kraemer's avatar
Pierre Kraemer committed
257
	 */
258
	unsigned int getAttributesNames(std::vector<std::string>& names);
Pierre Kraemer's avatar
Pierre Kraemer committed
259

Sylvain Thery's avatar
Sylvain Thery committed
260 261 262 263 264 265 266
	/**
	 * fill a vector with attribute type names
	 * @param types vector of type names
	 * @return number of attributes
	 */
	unsigned int getAttributesTypes(std::vector<std::string>& types);

267 268 269
	/**************************************
	 *        CONTAINER MANAGEMENT        *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
270 271

	/**
272
	 * swap two containers
Pierre Kraemer's avatar
Pierre Kraemer committed
273
	 */
274
	void swap(AttributeContainer& cont);
Pierre Kraemer's avatar
Pierre Kraemer committed
275 276

	/**
277 278 279 280
	 * clear the container
	 * @param removeAttrib remove the attributes (not only their data)
	 */
	void clear(bool clearAttrib = false);
Pierre Kraemer's avatar
Pierre Kraemer committed
281 282

	/**
283 284 285 286
	 * container compacting
	 * @param mapOldNew table that contains a map from old indices to new indices (holes -> 0xffffffff)
	 */
	void compact(std::vector<unsigned int>& mapOldNew);
Pierre Kraemer's avatar
Pierre Kraemer committed
287

288 289 290
	/**************************************
	 *          LINES MANAGEMENT          *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
291 292

	/**
293 294
	* insert a line in the container
	* @return index of the line
Pierre Kraemer's avatar
Pierre Kraemer committed
295
	*/
296
	unsigned int insertLine();
Pierre Kraemer's avatar
Pierre Kraemer committed
297 298

	/**
299 300
	* remove a line in the container
	* @param index index of the line to remove
Pierre Kraemer's avatar
Pierre Kraemer committed
301
	*/
302
	void removeLine(unsigned int index);
Pierre Kraemer's avatar
Pierre Kraemer committed
303 304

	/**
305 306 307
	 * initialize a line of the container (an element of each attribute)
	 */
	void initLine(unsigned int index);
Pierre Kraemer's avatar
Pierre Kraemer committed
308 309

	/**
310 311 312
	 * copy the content of line src in line dst
	 */
	void copyLine(unsigned int dstIndex, unsigned int srcIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
313 314

	/**
315 316
	* increment the ref counter of the given line
	* @param index index of the line
Pierre Kraemer's avatar
Pierre Kraemer committed
317
	*/
318
	void refLine(unsigned int index);
Pierre Kraemer's avatar
Pierre Kraemer committed
319 320

	/**
321 322 323
	* decrement the ref counter of the given line
	* @param index index of the line
	* @return true if the line was removed
Pierre Kraemer's avatar
Pierre Kraemer committed
324
	*/
325
	bool unrefLine(unsigned int eltIdx);
Pierre Kraemer's avatar
Pierre Kraemer committed
326 327

	/**
328 329 330
	* get the number of refs of the given line
	* @param index index of the line
	* @return number of refs of the line
Pierre Kraemer's avatar
Pierre Kraemer committed
331
	*/
332
	unsigned int getNbRefs(unsigned int index);
Pierre Kraemer's avatar
Pierre Kraemer committed
333 334

	/**
335 336 337
	* set the number of refs of the given line
	* @param index index of the line
	* @param nb number of refs
Pierre Kraemer's avatar
Pierre Kraemer committed
338
	*/
339
	void setNbRefs(unsigned int eltIdx, unsigned int nb);
Pierre Kraemer's avatar
Pierre Kraemer committed
340

341 342 343
	/**************************************
	 *       ATTRIBUTES MANAGEMENT        *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
344 345

	/**
346 347 348
	 * copy the data of attribute src in attribute dst (type has to be the same)
	 */
	bool copyAttribute(unsigned int dstIndex, unsigned int srcIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
349 350

	/**
351 352 353
	 * swap the data of attribute 1 with attribute 2 (type has to be the same)
	 */
	bool swapAttributes(unsigned int index1, unsigned int index2);
Pierre Kraemer's avatar
Pierre Kraemer committed
354

355 356 357
	/**************************************
	 *       ATTRIBUTES DATA ACCESS       *
	 **************************************/
Pierre Kraemer's avatar
Pierre Kraemer committed
358 359

	/**
360 361
	* get an AttributeMultiVector
	* @param attrIndex index of the attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
362 363
	*/
	template<typename T>
364
	AttributeMultiVector<T>* getDataVector(unsigned int attrIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
365

366
	AttributeMultiVectorGen* getVirtualDataVector(unsigned int attrIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
367 368

	/**
369 370
	* get an AttributeMultiVector
	* @param attribName name of the attribute
Pierre Kraemer's avatar
Pierre Kraemer committed
371
	*/
372
	template<typename T>
373
	AttributeMultiVector<T>* getDataVector(const std::string& attribName);
Pierre Kraemer's avatar
Pierre Kraemer committed
374

375
	AttributeMultiVectorGen* getVirtualDataVector(const std::string& attribName);
Pierre Kraemer's avatar
Pierre Kraemer committed
376 377

	/**
378 379 380 381 382
	* get a given element of a given attribute
	* @param T type of the attribute
	* @param attrIndex index of the attribute
	* @param eltIndex index of the element
	* @return a reference on the element
Pierre Kraemer's avatar
Pierre Kraemer committed
383
	*/
384 385
	template <typename T>
	T& getData(unsigned int attrIndex, unsigned int eltIndex);
Pierre Kraemer's avatar
Pierre Kraemer committed
386 387

	/**
388 389 390 391 392
	* get a given const element of a given attribute
	* @param T type of the attribute
	* @param attrIndex index of the attribute
	* @param eltIndex index of the element
	* @return a const reference on the element
Pierre Kraemer's avatar
Pierre Kraemer committed
393
	*/
394 395
	template <typename T>
	const T& getData(unsigned int attrIndex, unsigned int eltIndex) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
396 397

	/**
398 399 400 401 402
	* set a given element of a given attribute
	* @param T type of the attribute
	* @param attrIndex index of the attribute
	* @param eltIndex index of the element
	* @param data data to insert
Pierre Kraemer's avatar
Pierre Kraemer committed
403
	*/
404 405
	template <typename T>
	void setData(unsigned int attrIndex, unsigned int eltIndex, const T& data);
Pierre Kraemer's avatar
Pierre Kraemer committed
406 407


408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433

	/**************************************
	 *            SAVE & LOAD             *
	 **************************************/

public:
	/**
	* save binary file
	* @param fs a file stream
	* @param id the id to save
	*/
	void saveBin(CGoGNostream& fs, unsigned int id);

	/**
	* get id from file binary stream
	* @param fs file stream
	* @return the id of attribute container
	*/
	static unsigned int loadBinId(CGoGNistream& fs);

	/**
	* load from binary file
	* @param fs a file stream
	* @param id  ??
	*/
	bool loadBin(CGoGNistream& fs);
434 435 436 437 438 439 440

	/**
	 * copy container
	 * TODO a version that compact on the fly ?
	 */
	void copyFrom(const AttributeContainer& cont);

Pierre Kraemer's avatar
Pierre Kraemer committed
441 442 443 444
};

} // namespace CGoGN

445
#include "attributeContainer.hpp"
Pierre Kraemer's avatar
Pierre Kraemer committed
446 447

#endif