Blame view

src/Parameters/Container.hh 2.49 KB
fbe3c2bb   Benjamin Renard   First commit
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
/*
 * Container.hh
 *
 *  Created on: Dec 10, 2012
 *      Author: f.casimir
 */

#ifndef CONTAINER_HH_
#define CONTAINER_HH_

#define NB_DATA_BY_BLOCK 100

namespace AMDA {
namespace Parameters {
/**
 * @brief To contain Data of an implementation of paramData
 * @detail use for stored time and data in ParamData
 * @see ParamData
 */
template <typename T>
class Container {
public:
	/**
	 * Constructor.
	 */
	Container() : _size(0) {}

	/**
	 * Destructor.
	 */
	virtual ~Container() {
		for(typename BlockDataTab::iterator it = _dataBlock.begin(); it != _dataBlock.end(); ++it) {
				if(*it) delete[] (*it);
				*it = NULL;
		}
	}

	/**
	 * @return the number of element of the container.
	 */
	unsigned int size() const { return _size; }

	/**
	 * @return element at index.
	 */
	const T& operator [] (unsigned int index) const {
		return _dataBlock[index / NB_DATA_BY_BLOCK][index % NB_DATA_BY_BLOCK];
	}

	/**
	 * @return element at index.
	 */
	const T& at(unsigned int index) const {
		return _dataBlock.at(index / NB_DATA_BY_BLOCK)[index % NB_DATA_BY_BLOCK];
	}

	/**
	 * To add one Element.
	 */
	void push_back(const T& data) {
		resize(1);
		unsigned int indexBlock = _size / (NB_DATA_BY_BLOCK );
		unsigned int indexinBlock = _size % (NB_DATA_BY_BLOCK );
		*(_dataBlock[indexBlock] + indexinBlock) = data;
		++_size;
	}

263de286   Benjamin Renard   Apply time restri...
68
69
70
71
72
73
	void replace(const T& data, unsigned int index) {
		unsigned int indexBlock = index / (NB_DATA_BY_BLOCK );
		unsigned int indexinBlock = index % (NB_DATA_BY_BLOCK );
		*(_dataBlock[indexBlock] + indexinBlock) = data;
	}

fbe3c2bb   Benjamin Renard   First commit
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
	/**
	 * To increase the container capacity.
	 */
	void resize(unsigned int dataNumber) {
		///while reallocation is necessary
		while(_dataBlock.size() * NB_DATA_BY_BLOCK < (_size + dataNumber) ) {
			T *pt = new T[NB_DATA_BY_BLOCK];
			_dataBlock.push_back(pt);
		}
	}

	/**
	 * To release data bloc previous the index.
	 */
	void freeBefore(unsigned int pIndexMin) {
		//LOG4CXX_DEBUG(_logger, "freeBefore of ParamData: "<< (void *)this << " before index : "<< pIndexMin << " nb Data " << size());
		unsigned int lIndexBlock = 0;
		unsigned lIndexBlockMin = pIndexMin  / NB_DATA_BY_BLOCK;
		while(lIndexBlock  < lIndexBlockMin ) {
			if(_dataBlock[lIndexBlock]) delete[] _dataBlock[lIndexBlock];
			_dataBlock[lIndexBlock] = NULL;
			++lIndexBlock;
		}
	}

	void pop() { --_size; }

private:
	/**
	 * number of data
	 */
	unsigned int _size;
	typedef std::vector<T*> BlockDataTab;
	BlockDataTab _dataBlock;
};

} /* namespace Parameters */
} /* namespace AMDA */
#endif /* CONTAINER_HH_ */