attributeMultiVectorBool.hpp 9.45 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
#include <cstring>

namespace CGoGN
{

class MarkerBool
{
public:
	std::string CGoGNnameOfType() const
	{
		return "MarkerBool";
	}
};



Sylvain Thery's avatar
Sylvain Thery committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 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 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 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
template <>
class AttributeMultiVector<MarkerBool> : public AttributeMultiVectorGen
{
	/**
	* table of blocks of data pointers: vectors!
	*/
	std::vector< unsigned int* > m_tableData;

public:
	AttributeMultiVector(const std::string& strName, const std::string& strType):
		AttributeMultiVectorGen(strName, strType)
	{
		m_tableData.reserve(1024);
	}

	AttributeMultiVector()
	{
		m_tableData.reserve(1024);
	}

	~AttributeMultiVector() {}

	inline AttributeMultiVectorGen* new_obj()
	{
		AttributeMultiVectorGen* ptr = new AttributeMultiVector<MarkerBool>;
		ptr->setTypeName(m_typeName);
		return ptr;
	}

	/**************************************
	 *       MULTI VECTOR MANAGEMENT      *
	 **************************************/

	void addBlock()
	{
		unsigned int* ptr = new unsigned int[_BLOCKSIZE_/32];
		memset(ptr,0,_BLOCKSIZE_/8);
		m_tableData.push_back(ptr);
//		std::cout << "Marker "<<this->getName()<<" - addBlock"<< std::endl;
	}

	void setNbBlocks(unsigned int nbb)
	{
		if (nbb >= m_tableData.size())
		{
			for (unsigned int i= m_tableData.size(); i <nbb; ++i)
				addBlock();
		}
		else
		{
			for (unsigned int i = m_tableData.size()-1; i>=nbb; --i)
				delete[] m_tableData[i];

			m_tableData.resize(nbb);
		}
	}


	unsigned int getNbBlocks() const
	{
		return m_tableData.size();
	}

//	void addBlocksBefore(unsigned int nbb);

	bool copy(const AttributeMultiVectorGen* atmvg)
	{
		const AttributeMultiVector<MarkerBool>* atmv = dynamic_cast<const AttributeMultiVector<MarkerBool>*>(atmvg);

		if (atmv == NULL)
		{
			CGoGNerr << "trying to copy attributes of different type" << CGoGNendl;
			return false;
		}
//		if (atmv->m_typeName != m_typeName)
//		{
//			CGoGNerr << "trying to copy attributes with different type names" << CGoGNendl;
//			return false;
//		}

		for (unsigned int i = 0; i < atmv->m_tableData.size(); ++i)
			memcpy(m_tableData[i],atmv->m_tableData[i],_BLOCKSIZE_/32);

		return true;
	}

	bool swap(AttributeMultiVectorGen* atmvg)
	{
		AttributeMultiVector<MarkerBool>* atmv = dynamic_cast<AttributeMultiVector<MarkerBool>*>(atmvg);

		if (atmv == NULL)
		{
			CGoGNerr << "trying to swap attributes of different type" << CGoGNendl;
			return false;
		}
		if (atmv->m_typeName != m_typeName)
		{
			CGoGNerr << "trying to swap attributes with different type names" << CGoGNendl;
			return false;
		}

		m_tableData.swap(atmv->m_tableData) ;
		return true;
	}

	bool merge(const AttributeMultiVectorGen& att)
	{
		const AttributeMultiVector<MarkerBool>* attrib = dynamic_cast< const AttributeMultiVector<MarkerBool>* >(&att);
		if (attrib==NULL)
		{
			CGoGNerr << "trying to merge attributes of different type" << CGoGNendl;
			return false;
		}

		if (attrib->m_typeName != m_typeName)
		{
			CGoGNerr << "trying to merge attributes with different type names" << CGoGNendl;
			return false;
		}

		for (auto it = attrib->m_tableData.begin(); it != attrib->m_tableData.end(); ++it)
			m_tableData.push_back(*it);

		return true;
	}

	void clear()
	{
		for (auto it=m_tableData.begin(); it !=m_tableData.end(); ++it)
			delete[] *it;
		m_tableData.clear();
	}

	int getSizeOfType() const
	{
		return sizeof(bool); // ?
	}

	inline void allFalse()
	{
		for (unsigned int i = 0; i < m_tableData.size(); ++i)
		{
			unsigned int *ptr =m_tableData[i];
			for (unsigned int j=0; j<_BLOCKSIZE_/32;++j)
				*ptr++ = 0;
		}
		//memset(m_tableData[i],0,_BLOCKSIZE_/8);
	}

	/**************************************
	 *             DATA ACCESS            *
	 **************************************/


	inline void setFalse(unsigned int i)
	{
		unsigned int jj = i / _BLOCKSIZE_;
		unsigned int j = i % _BLOCKSIZE_;
		unsigned int x = j/32;
		unsigned int y = j%32;
		unsigned int mask = 1 << y;
		m_tableData[jj][x] &= ~mask;
	}

	inline void setTrue(unsigned int i)
	{
		unsigned int jj = i / _BLOCKSIZE_;
		unsigned int j = i % _BLOCKSIZE_;
		unsigned int x = j/32;
		unsigned int y = j%32;
		unsigned int mask = 1 << y;
		m_tableData[jj][x] |= mask;
	}

	inline void setVal(unsigned int i, bool b)
	{
		unsigned int jj = i / _BLOCKSIZE_;
		unsigned int j = i % _BLOCKSIZE_;
		unsigned int x = j/32;
		unsigned int y = j%32;
		unsigned int mask = 1 << y;

		if (b)
			m_tableData[jj][x] |= mask;
		else
			m_tableData[jj][x] &= ~mask;
	}

	/**
	 * get a const reference on a elt
	 * @param i index of element
	 */
	inline bool operator[](unsigned int i) const
	{
		unsigned int jj = i / _BLOCKSIZE_;
		unsigned int j = i % _BLOCKSIZE_;
		unsigned int x = j/32;
		unsigned int y = j%32;

		unsigned int mask = 1 << y;

		return (m_tableData[jj][x] & mask) != 0;
	}

	/**
	 * Get the addresses of each block of data
	 */
	unsigned int getBlocksPointers(std::vector<void*>& addr, unsigned int& /*byteBlockSize*/) const
	{
		CGoGNerr << "DO NOT USE getBlocksPointers with bool attribute"<< CGoGNendl;
		addr.reserve(m_tableData.size());
		addr.clear();

		for (auto it = m_tableData.begin(); it != m_tableData.end(); ++it)
			addr.push_back(NULL );

		return addr.size();
	}


	/**************************************
	 *          LINES MANAGEMENT          *
	 **************************************/

	inline void initElt(unsigned int id)
	{
		setFalse(id);
	}

	inline void copyElt(unsigned int dst, unsigned int src)
	{
		setVal(dst,this->operator [](src));
	}


	inline void swapElt(unsigned int id1, unsigned int id2)
	{
		bool data = this->operator [](id1);
		setVal(id1,this->operator [](id2));
		setVal(id2,data);
	}

	/**
	* swap two elements in container (useful for compact function)
	* @param src_b  block index of source element
	* @param src_id index in block of source element
	* @param dst_b  block index of destination element
	* @param dst_id index in block of destination element
	*/
	void overwrite(unsigned int src_b, unsigned int src_id, unsigned int dst_b, unsigned int dst_id)
	{
Sylvain Thery's avatar
Sylvain Thery committed
291
		bool b = (m_tableData[src_b][src_id/32] & (1 << (src_id%32))) != 0;
Sylvain Thery's avatar
Sylvain Thery committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

		unsigned int mask = 1 << (dst_id%32);

		if (b)
			m_tableData[dst_b][dst_id/32] |= mask;
		else
			m_tableData[dst_b][dst_id/32] &= ~mask;

	}

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

	/**
	 * Sauvegarde binaire
	 * @param fs filestream
	 * @param id id of mv
	 */
	void saveBin(CGoGNostream& fs, unsigned int id)
	{
		unsigned int nbs[3];
		nbs[0] = id;
		int len1 = m_attrName.size()+1;
		int len2 = m_typeName.size()+1;
		nbs[1] = len1;
		nbs[2] = len2;
		fs.write(reinterpret_cast<const char*>(nbs),3*sizeof(unsigned int));
		// store names
		char buffer[256];
		const char* s1 = m_attrName.c_str();
		memcpy(buffer,s1,len1);
		const char* s2 = m_typeName.c_str();
		memcpy(buffer+len1,s2,len2);
		fs.write(reinterpret_cast<const char*>(buffer),(len1+len2)*sizeof(char));

		nbs[0] = m_tableData.size();
		nbs[1] = nbs[0] * _BLOCKSIZE_/32;
		fs.write(reinterpret_cast<const char*>(nbs),2*sizeof(unsigned int));

		for (auto ptrIt = m_tableData.begin(); ptrIt!=m_tableData.end(); ++ptrIt)
			fs.write(reinterpret_cast<const char*>(*ptrIt),_BLOCKSIZE_/8);
	}


	/**
	 * lecture binaire
	 * @param fs filestream
	 */
	bool loadBin(CGoGNistream& fs)
	{
		unsigned int nbs[2];
		fs.read(reinterpret_cast<char*>(nbs), 2*sizeof(unsigned int));

		unsigned int nb = nbs[0];

		// load data blocks
		m_tableData.resize(nb);
350

Sylvain Thery's avatar
Sylvain Thery committed
351 352 353
		for(unsigned int i = 0; i < nb; ++i)
		{
			m_tableData[i] = new unsigned int[_BLOCKSIZE_/32];
354
			fs.read(reinterpret_cast<char*>(m_tableData[i]),_BLOCKSIZE_/8);
Sylvain Thery's avatar
Sylvain Thery committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
		}

		return true;
	}

	/**
	 * lecture binaire
	 * @param fs filestream
	 */
	virtual void dump(unsigned int i) const
	{
		CGoGNout << this->operator[](i);
	}
};



Sylvain Thery's avatar
Sylvain Thery committed
372 373
} // namespace CGoGN