holeblockref.h 4.77 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

Sylvain Thery's avatar
Sylvain Thery committed
34

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

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

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

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	/**
	* 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
84
	unsigned int newRefElt(unsigned int& nbEltsMax);
Pierre Kraemer's avatar
Pierre Kraemer committed
85 86 87 88

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

	/**
	* 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();

Sylvain Thery's avatar
Sylvain Thery committed
132 133 134 135 136 137 138 139

	inline void compressFull(unsigned int nb)
	{
		m_nbfree = 0;
		m_nbref = nb;
		m_nb = nb;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
140 141 142 143 144 145 146 147 148 149 150
	/**
	* 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
151
	void overwrite(unsigned int i, HoleBlockRef *bf, unsigned int j);
Pierre Kraemer's avatar
Pierre Kraemer committed
152 153 154 155

	/**
	* increment ref counter of element i
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
156
	inline void ref(unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
157 158 159 160 161 162 163 164
	{
		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
165
	inline bool unref(unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
166
	{
167
//		assert(m_refCount[i] > 1);
Pierre Kraemer's avatar
Pierre Kraemer committed
168 169 170 171 172 173 174 175 176 177 178 179
		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
180
	inline void setNbRefs(unsigned int i, unsigned int nb) { m_refCount[i] = nb; }
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182 183

	/**
	* number of references of element i
184
	* @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
185
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
186
	inline unsigned int nbRefs(unsigned int i) { return m_refCount[i]; }
Pierre Kraemer's avatar
Pierre Kraemer committed
187

Pierre Kraemer's avatar
Pierre Kraemer committed
188
	bool updateHoles(unsigned int nb);
Pierre Kraemer's avatar
Pierre Kraemer committed
189 190 191 192

	void saveBin(CGoGNostream& fs);

	bool loadBin(CGoGNistream& fs);
Sylvain Thery's avatar
Sylvain Thery committed
193 194

	unsigned int* getTableFree(unsigned int & nb) {nb =m_nbfree; return m_tableFree;}
Pierre Kraemer's avatar
Pierre Kraemer committed
195 196
};

Pierre Kraemer's avatar
Pierre Kraemer committed
197
} // namespace CGoGN
Pierre Kraemer's avatar
Pierre Kraemer committed
198 199

#endif