holeblockref.h 4.6 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                                        *
*                                                                              *
*******************************************************************************/

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

#include <fstream>
#include <iostream>
30
#include <assert.h>
Pierre Kraemer's avatar
Pierre Kraemer committed
31

Pierre Kraemer's avatar
Pierre Kraemer committed
32
#include "Container/sizeblock.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
33 34 35

namespace CGoGN
{
Pierre Kraemer's avatar
Pierre Kraemer committed
36

Pierre Kraemer's avatar
Pierre Kraemer committed
37 38 39 40 41 42
class HoleBlockRef
{
protected:
	/**
	* Table of free index
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
43 44
	unsigned int* m_tableFree;
	unsigned int m_nbfree;
Pierre Kraemer's avatar
Pierre Kraemer committed
45 46 47 48

	/**
	* Reference counter (if 0 it is a hole)
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
49 50
	unsigned int* m_refCount;
	unsigned int m_nbref;
Pierre Kraemer's avatar
Pierre Kraemer committed
51 52

	/**
53
	* nb elements in block
Pierre Kraemer's avatar
Pierre Kraemer committed
54
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
55
	unsigned int m_nb;
Pierre Kraemer's avatar
Pierre Kraemer committed
56 57 58 59 60 61 62

public:
	/**
	* constructor
	*/
	HoleBlockRef();

63 64 65 66 67
	/**
	 * copy constructor
	 */
	HoleBlockRef(const HoleBlockRef& hb);

Pierre Kraemer's avatar
Pierre Kraemer committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	/**
	* destructor
	*/
	~HoleBlockRef();

	/**
	 * swapping
	 */
	void swap(HoleBlockRef& hb);

	/**
	* add a element and return its index (refCount = 1)
	* @param nbEltsMax (IN/OUT) max number of element stored
	* @return index on new element
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
83
	unsigned int newRefElt(unsigned int& nbEltsMax);
Pierre Kraemer's avatar
Pierre Kraemer committed
84 85 86 87

	/**
	* remove an element
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
88
	inline void removeElt(unsigned int idx)
Pierre Kraemer's avatar
Pierre Kraemer committed
89 90
	{
		m_nb--;
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92
		m_tableFree[m_nbfree++] = idx;
		m_refCount[idx] = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	}

	/**
	* is the block full
	*/
	inline bool full() const { return m_nb == _BLOCKSIZE_;  }

	/**
	*  is the block empty
	*/
	inline bool empty() const { return m_nb == 0; }

	/**
	* is this index used or not
	*/
	inline bool used(unsigned int i) const { return m_refCount[i] != 0; }

	/**
	* use with caution: from compress only !
	*/
	inline void incNb() { m_nb++; }

	/**
	* use with caution: from compress only !
	*/
	inline void decNb() { m_nb--; }

	/**
	* return the size of table
	*/
	inline unsigned int sizeTable() { return m_nbref; }

	/**
	* compress the free value (use only in compress )
	* @return true if it is empty
	*/
	bool compressFree();

	/**
	* clear the container of free block
	*/
	void clear();

	/**
	* overwrite a line with another (called from compact)
	* @param i index of line in the block
	* @param bf ptr on the block of other line
	* @param j index of the other line in bf
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
142
	void overwrite(unsigned int i, HoleBlockRef *bf, unsigned int j);
Pierre Kraemer's avatar
Pierre Kraemer committed
143 144 145 146

	/**
	* increment ref counter of element i
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
147
	inline void ref(unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
148 149 150 151 152 153 154 155
	{
		m_refCount[i]++;
	}

	/**
	* decrement ref counter of element i
	* @return true if ref=0 and element has been destroyed
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
156
	inline bool unref(unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
157
	{
158
//		assert(m_refCount[i] > 1);
Pierre Kraemer's avatar
Pierre Kraemer committed
159 160 161 162 163 164 165 166 167 168 169 170
		m_refCount[i]--;
		if (m_refCount[i] == 1)
		{
			removeElt(i);
			return true;
		}
		return false;
	}

	/**
	* set ref counter of element i with j
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
171
	inline void setNbRefs(unsigned int i, unsigned int nb) { m_refCount[i] = nb; }
Pierre Kraemer's avatar
Pierre Kraemer committed
172 173 174

	/**
	* number of references of element i
175
	* @return the number of references +1 (stored as n+1, 0 = not used, 1 used but not refs, ...)
Pierre Kraemer's avatar
Pierre Kraemer committed
176
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
177
	inline unsigned int nbRefs(unsigned int i) { return m_refCount[i]; }
Pierre Kraemer's avatar
Pierre Kraemer committed
178

Pierre Kraemer's avatar
Pierre Kraemer committed
179
	bool updateHoles(unsigned int nb);
Pierre Kraemer's avatar
Pierre Kraemer committed
180 181 182 183 184 185

	void saveBin(CGoGNostream& fs);

	bool loadBin(CGoGNistream& fs);
};

Pierre Kraemer's avatar
Pierre Kraemer committed
186
} // namespace CGoGN
Pierre Kraemer's avatar
Pierre Kraemer committed
187 188

#endif