Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

holeblockref.h 4.44 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

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

#include <fstream>
#include <iostream>

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

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

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

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

	/**
	* nb element in block
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
54
	unsigned int m_nb;
Pierre Kraemer's avatar
Pierre Kraemer committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

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

	/**
	* 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
77
	unsigned int newRefElt(unsigned int& nbEltsMax);
Pierre Kraemer's avatar
Pierre Kraemer committed
78
79
80
81

	/**
	* remove an element
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
82
	inline void removeElt(unsigned int idx)
Pierre Kraemer's avatar
Pierre Kraemer committed
83
84
	{
		m_nb--;
Pierre Kraemer's avatar
Pierre Kraemer committed
85
86
		m_tableFree[m_nbfree++] = idx;
		m_refCount[idx] = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	}

	/**
	* 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
136
	void overwrite(unsigned int i, HoleBlockRef *bf, unsigned int j);
Pierre Kraemer's avatar
Pierre Kraemer committed
137
138
139
140

	/**
	* increment ref counter of element i
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
141
	inline void ref(unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
142
143
144
145
146
147
148
149
	{
		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
150
	inline bool unref(unsigned int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
151
152
153
154
155
156
157
158
159
160
161
162
163
	{
		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
164
	inline void setNbRefs(unsigned int i, unsigned int nb) { m_refCount[i] = nb; }
Pierre Kraemer's avatar
Pierre Kraemer committed
165
166
167
168
169

	/**
	* number of references of element i
	* @return the number of references (0 = no elements)
	*/
Pierre Kraemer's avatar
Pierre Kraemer committed
170
	inline unsigned int nbRefs(unsigned int i) { return m_refCount[i]; }
Pierre Kraemer's avatar
Pierre Kraemer committed
171

Pierre Kraemer's avatar
Pierre Kraemer committed
172
	bool updateHoles(unsigned int nb);
Pierre Kraemer's avatar
Pierre Kraemer committed
173
174
175
176
177
178

	void saveBin(CGoGNostream& fs);

	bool loadBin(CGoGNistream& fs);
};

Pierre Kraemer's avatar
Pierre Kraemer committed
179
} // namespace CGoGN
Pierre Kraemer's avatar
Pierre Kraemer committed
180
181

#endif