holeblock.hXX 1.89 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 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
#ifndef __HOLE_BLOCK_REF__
#define __HOLE_BLOCK_REF__

#include <fstream>

#include "sizeblock.h"


class HoleBlock
{
protected:
	typedef unsigned int uint;

	/**
	* Table of free index
	*/
	uint* m_tableFree;
	uint m_nbfree;

	/**
	* Reference counter (if 0 it is a hole)
	*/

	char* m_used;
	uint m_nbused;

	/**
	* nb element in block
	*/
	uint m_nb;

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

	/**
	* destructor
	*/
	~HoleBlock();

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


	/**
	* add a element and return its index
	* @param nbEltsMax (IN/OUT) max number of element stored
	* @return index on new element
	*/
	unsigned int newElt(unsigned int& nbEltsMax);

	/**
	* remove an element
	*/
	void removeElt(unsigned int idx);

	/**
	* is the block full
	*/
	inline bool full() const { return m_nb == _BLOCKSIZE_; /*(m_refCount.size() - m_tableFree.size())>=_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 { /*std::cout << "Used["<<i<<"] ? ->"<<m_refCount[i]<<std::endl; */ 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
	*/
	void overwrite(int i, HoleBlockRef *bf, int j);


	bool updateHoles(unsigned int nb);

	void saveBin(std::ofstream& fs);

	bool loadBin(std::ifstream& fs);
};


#endif