holeblockref.cpp 4.63 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 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)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
46 47
	m_tableFree = new unsigned int[_BLOCKSIZE_];
	m_refCount = new unsigned int[_BLOCKSIZE_];
Pierre Kraemer's avatar
Pierre Kraemer committed
48 49 50 51
}

HoleBlockRef::~HoleBlockRef()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
52 53
	delete[] m_tableFree;
	delete[] m_refCount;
Pierre Kraemer's avatar
Pierre Kraemer committed
54 55 56 57
}

void HoleBlockRef::swap(HoleBlockRef& hb)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
58
	unsigned int temp = m_nbfree;
Pierre Kraemer's avatar
Pierre Kraemer committed
59 60 61 62 63 64 65 66 67 68 69
	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
70
	unsigned int* ptr = m_tableFree;
Pierre Kraemer's avatar
Pierre Kraemer committed
71 72 73
	m_tableFree = hb.m_tableFree;
	hb.m_tableFree = ptr;

Pierre Kraemer's avatar
Pierre Kraemer committed
74
	unsigned int* ptr2 = m_refCount;
Pierre Kraemer's avatar
Pierre Kraemer committed
75 76 77 78
	m_refCount = hb.m_refCount;
	hb.m_refCount = ptr2;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
79
unsigned int HoleBlockRef::newRefElt(unsigned int& nbEltsMax)
Pierre Kraemer's avatar
Pierre Kraemer committed
80 81 82 83
{
	// no hole then add a line at the end of block
	if (m_nbfree == 0)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
84
		unsigned int nbElts = m_nbref;
Pierre Kraemer's avatar
Pierre Kraemer committed
85 86 87 88 89 90 91 92

 		m_refCount[m_nbref++] = 1;

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

Pierre Kraemer's avatar
Pierre Kraemer committed
93
	unsigned int index = m_tableFree[--m_nbfree];
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

	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
112
void HoleBlockRef::overwrite(unsigned int i, HoleBlockRef *bf, unsigned int j)
Pierre Kraemer's avatar
Pierre Kraemer committed
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
{
	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)
{
//	std::cout << "save bf "<< m_nb<< " / "<< m_nbref<< " / "<< m_nbfree << std::endl;

	// on sauve les trois nombres;
149
	unsigned int numbers[3];
Pierre Kraemer's avatar
Pierre Kraemer committed
150 151 152
	numbers[0] = m_nb;
	numbers[1] = m_nbref;
	numbers[2] = m_nbfree;
153
	fs.write(reinterpret_cast<const char*>(numbers), 3*sizeof(unsigned int) );
Pierre Kraemer's avatar
Pierre Kraemer committed
154 155

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

	// sauve les free lines
159
	fs.write(reinterpret_cast<const char*>(m_tableFree), m_nbfree*sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
160 161 162 163
}

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

166
	fs.read(reinterpret_cast<char*>(numbers), 3*sizeof(unsigned int));
Pierre Kraemer's avatar
Pierre Kraemer committed
167 168 169 170
	m_nb = numbers[0];
	m_nbref = numbers[1];
	m_nbfree = numbers[2];

171 172
	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
173 174 175 176

	return true;
}

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