holeblockref.cpp 4.99 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "Container/holeblockref.h"

#include <map>
#include <string>
#include <cassert>
#include <stdio.h>
#include <string.h>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#include <libxml/parser.h>

#include <iostream>
#include <sstream>
#include <fstream>

namespace CGoGN
{

HoleBlockRef::HoleBlockRef():
m_nbfree(0),m_nbref(0),m_nb(0)
{
46
	m_tableFree = new unsigned int[_BLOCKSIZE_ + 10];
Pierre Kraemer's avatar
Pierre Kraemer committed
47
	m_refCount = new unsigned int[_BLOCKSIZE_];
Pierre Kraemer's avatar
Pierre Kraemer committed
48 49
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63
HoleBlockRef::HoleBlockRef(const HoleBlockRef& hb)
{
	m_nbfree = hb.m_nbfree;
	m_nbref = hb.m_nbref;
	m_nb = hb.m_nb;

	m_tableFree = new unsigned int[_BLOCKSIZE_ +10];
	memcpy(m_tableFree, hb.m_tableFree, (_BLOCKSIZE_+ 10) * sizeof(unsigned int));

	m_refCount = new unsigned int[_BLOCKSIZE_];
	memcpy(m_refCount, hb.m_refCount, _BLOCKSIZE_ * sizeof(unsigned int));

}

Pierre Kraemer's avatar
Pierre Kraemer committed
64 65
HoleBlockRef::~HoleBlockRef()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
66 67
	delete[] m_tableFree;
	delete[] m_refCount;
Pierre Kraemer's avatar
Pierre Kraemer committed
68 69 70 71
}

void HoleBlockRef::swap(HoleBlockRef& hb)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
72
	unsigned int temp = m_nbfree;
Pierre Kraemer's avatar
Pierre Kraemer committed
73 74 75 76 77 78 79 80 81 82 83
	m_nbfree = hb.m_nbfree;
	hb.m_nbfree = temp;

	temp = m_nbref;
	m_nbref = hb.m_nbref;
	hb.m_nbref = temp;

	temp = m_nb;
	m_nb = hb.m_nb;
	hb.m_nb = temp;

Pierre Kraemer's avatar
Pierre Kraemer committed
84
	unsigned int* ptr = m_tableFree;
Pierre Kraemer's avatar
Pierre Kraemer committed
85 86 87
	m_tableFree = hb.m_tableFree;
	hb.m_tableFree = ptr;

Pierre Kraemer's avatar
Pierre Kraemer committed
88
	unsigned int* ptr2 = m_refCount;
Pierre Kraemer's avatar
Pierre Kraemer committed
89 90 91 92
	m_refCount = hb.m_refCount;
	hb.m_refCount = ptr2;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
93
unsigned int HoleBlockRef::newRefElt(unsigned int& nbEltsMax)
Pierre Kraemer's avatar
Pierre Kraemer committed
94 95 96 97
{
	// no hole then add a line at the end of block
	if (m_nbfree == 0)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
98
		unsigned int nbElts = m_nbref;
Pierre Kraemer's avatar
Pierre Kraemer committed
99 100 101 102 103 104 105 106

 		m_refCount[m_nbref++] = 1;

		m_nb++;
		nbEltsMax++;
		return nbElts;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
107
	unsigned int index = m_tableFree[--m_nbfree];
Pierre Kraemer's avatar
Pierre Kraemer committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

	m_refCount[index] = 1;

	m_nb++;
	return index;
}

bool  HoleBlockRef::compressFree()
{
	if (m_nb)
	{
		m_nbfree = 0;
		m_nbref = m_nb;
		return false;
	}
	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
126
void HoleBlockRef::overwrite(unsigned int i, HoleBlockRef *bf, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
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
{
	m_refCount[i] = bf->m_refCount[j];
	bf->m_refCount[j] = 0;

	incNb();
	bf->decNb();
}

void HoleBlockRef::clear()
{
	m_nb = 0;
	m_nbfree = 0;
	m_nbref = 0;
}

bool HoleBlockRef::updateHoles(unsigned int nb)
{
	m_nbfree = 0;
	m_nbref = nb;
	bool notfull = false;
	for (unsigned int i = 0; i < nb; ++i)
	{
		if (m_refCount[i] == 0)
		{
			m_tableFree[m_nbfree++] = i;
			notfull = true;
		}
	}
	return notfull;
}

void HoleBlockRef::saveBin(CGoGNostream& fs)
{
160
//	CGoGNout << "save bf "<< m_nb<< " / "<< m_nbref<< " / "<< m_nbfree << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
161 162

	// on sauve les trois nombres;
163
	unsigned int numbers[3];
Pierre Kraemer's avatar
Pierre Kraemer committed
164 165 166
	numbers[0] = m_nb;
	numbers[1] = m_nbref;
	numbers[2] = m_nbfree;
167
	fs.write(reinterpret_cast<const char*>(numbers), 3*sizeof(unsigned int) );
Pierre Kraemer's avatar
Pierre Kraemer committed
168 169

	// sauve les ref count
170
	fs.write(reinterpret_cast<const char*>(m_refCount), _BLOCKSIZE_*sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
171 172

	// sauve les free lines
173
	fs.write(reinterpret_cast<const char*>(m_tableFree), m_nbfree*sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
174 175 176 177
}

bool HoleBlockRef::loadBin(CGoGNistream& fs)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
178
	unsigned int numbers[3];
Pierre Kraemer's avatar
Pierre Kraemer committed
179

180
	fs.read(reinterpret_cast<char*>(numbers), 3*sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182 183 184
	m_nb = numbers[0];
	m_nbref = numbers[1];
	m_nbfree = numbers[2];

185 186
	fs.read(reinterpret_cast<char*>(m_refCount), _BLOCKSIZE_*sizeof(unsigned int));
	fs.read(reinterpret_cast<char*>(m_tableFree), m_nbfree*sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
187 188 189 190

	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
191
} // namespace CGoGN