a real simple vector system

This commit is contained in:
Edouard Dupin 2012-08-18 18:22:26 +02:00
parent b59ce366e7
commit 236d450bd5

View File

@ -38,7 +38,7 @@ namespace etk
/** /**
* @brief Vector classes ... * @brief Vector classes ...
* *
* @tparam[in] SIZE Size of the current element. * @tparam[in] MY_TYPE class type of the current element.
* *
* m_data * m_data
* <------------ m_dataSize ------------> * <------------ m_dataSize ------------>
@ -213,8 +213,6 @@ namespace etk
MY_TYPE * m_data; //!< pointer on the curetn table of Data MY_TYPE * m_data; //!< pointer on the curetn table of Data
int32_t m_size; //!< nb Element in the buffer int32_t m_size; //!< nb Element in the buffer
int32_t m_allocated; //!< Current allocated size int32_t m_allocated; //!< Current allocated size
int32_t m_increment; //!< methode of increment
bool m_mode; //!< if true the normal mode is used
public: public:
/** /**
* @brief Create an empty vector * @brief Create an empty vector
@ -223,9 +221,7 @@ namespace etk
Vector(int32_t count = 0): Vector(int32_t count = 0):
m_data(NULL), m_data(NULL),
m_size(0), m_size(0),
m_allocated(0), m_allocated(0)
m_increment(1),
m_mode(true)
{ {
ChangeAllocation(count); ChangeAllocation(count);
} }
@ -238,7 +234,6 @@ namespace etk
{ {
m_allocated = Evb.m_allocated; m_allocated = Evb.m_allocated;
m_size = Evb.m_size; m_size = Evb.m_size;
m_increment = Evb.m_increment;
m_data = NULL; m_data = NULL;
//TK_DEBUG("USE Specific vector allocator ... Evb.m_size=" << Evb.m_size << " Evb.m_increment=" << Evb.m_increment); //TK_DEBUG("USE Specific vector allocator ... Evb.m_size=" << Evb.m_size << " Evb.m_increment=" << Evb.m_increment);
// allocate all same data // allocate all same data
@ -248,13 +243,9 @@ namespace etk
return; return;
} }
// Copy all data ... // Copy all data ...
if(true == m_mode) { for(int32_t iii=0; iii<m_allocated; iii++) {
for(int32_t iii=0; iii<m_allocated; iii++) { // copy operator ...
// copy operator ... m_data[iii] = Evb.m_data[iii];
m_data[iii] = Evb.m_data[iii];
}
} else {
memcpy(m_data, Evb.m_data, m_allocated * sizeof(MY_TYPE) );
} }
} }
@ -269,7 +260,6 @@ namespace etk
} }
m_allocated = 0; m_allocated = 0;
m_size = 0; m_size = 0;
m_increment = 0;
} }
/** /**
@ -289,21 +279,15 @@ namespace etk
// Set the new value // Set the new value
m_allocated = Evb.m_allocated; m_allocated = Evb.m_allocated;
m_size = Evb.m_size; m_size = Evb.m_size;
m_increment = Evb.m_increment;
// allocate all same data // allocate all same data
m_data = new MY_TYPE[m_allocated]; m_data = new MY_TYPE[m_allocated];
if (NULL==m_data) { if (NULL==m_data) {
TK_CRITICAL("Vector : Error in data allocation ... might nor work corectly anymore"); TK_CRITICAL("Vector : Error in data allocation ... might nor work corectly anymore");
return *this; return *this;
} }
if(true == m_mode) { for(int32_t iii=0; iii<m_allocated; iii++) {
for(int32_t iii=0; iii<m_allocated; iii++) { // copy operator ...
// copy operator ... m_data[iii] = Evb.m_data[iii];
m_data[iii] = Evb.m_data[iii];
}
} else {
// Copy all data ...
memcpy(m_data, Evb.m_data, m_allocated * sizeof(MY_TYPE) );
} }
} }
// Return the curent pointer // Return the curent pointer
@ -323,26 +307,14 @@ namespace etk
TK_CRITICAL("allocation error"); TK_CRITICAL("allocation error");
return *this; return *this;
} }
if(true == m_mode) { for(int32_t iii=0; iii<nbElememt; iii++) {
for(int32_t iii=0; iii<nbElememt; iii++) { // copy operator ...
// copy operator ... m_data[idx+iii] = Evb.m_data[iii];
m_data[idx+iii] = Evb.m_data[iii];
}
} else {
memcpy(&m_data[idx], &Evb.m_data[0], nbElememt*sizeof(MY_TYPE) );
} }
// Return the curent pointer // Return the curent pointer
return *this; return *this;
} }
/**
* @brief Set increment mode of this vector (default it match corectly with the number of element inside)
* @param[in] newIncrementNumber methode requested
*/
void SetIncrement(int32_t newIncrementNumber)
{
m_increment = newIncrementNumber;
}
/** /**
* @brief Get the number of element in the vector * @brief Get the number of element in the vector
@ -422,12 +394,8 @@ namespace etk
TK_ERROR("Resize does not work corectly ... not added item"); TK_ERROR("Resize does not work corectly ... not added item");
return; return;
} }
if(true == m_mode) { for (int32_t iii=0; iii<nbElement; iii++) {
for (int32_t iii=0; iii<nbElement; iii++) { m_data[idx+iii] = item[iii];
m_data[idx+iii] = item[iii];
}
} else {
memcpy(&m_data[idx], item, nbElement*sizeof(MY_TYPE));
} }
} }
@ -477,21 +445,13 @@ namespace etk
// move curent data (after the position) // move curent data (after the position)
int32_t sizeToMove = (idx - pos); int32_t sizeToMove = (idx - pos);
if ( 0 < sizeToMove) { if ( 0 < sizeToMove) {
if(true == m_mode) { for (int32_t iii=1; iii<=sizeToMove; iii++) {
for (int32_t iii=1; iii<=sizeToMove; iii++) { m_data[m_size-iii] = m_data[idx-iii];
m_data[m_size-iii] = m_data[idx-iii];
}
} else {
memmove(&m_data[idx-sizeToMove-1], &m_data[m_size-sizeToMove-1], sizeToMove*sizeof(MY_TYPE) );
} }
} }
// affectation of all input element // affectation of all input element
if(true == m_mode) { for (int32_t iii=0; iii<nbElement; iii++) {
for (int32_t iii=0; iii<nbElement; iii++) { m_data[pos+iii] = item[iii];
m_data[pos+iii] = item[iii];
}
} else {
memcpy(&m_data[pos], item, nbElement*sizeof(MY_TYPE) );
} }
} }
@ -530,12 +490,8 @@ namespace etk
// move curent data // move curent data
int32_t sizeToMove = (idx - (pos+nbElement)); int32_t sizeToMove = (idx - (pos+nbElement));
if ( 0 < sizeToMove) { if ( 0 < sizeToMove) {
if(true == m_mode) { for (int32_t iii=0; iii<sizeToMove; iii++) {
for (int32_t iii=0; iii<sizeToMove; iii++) { m_data[pos+iii] = m_data[pos+nbElement+iii];
m_data[pos+iii] = m_data[pos+nbElement+iii];
}
} else {
memmove((m_data + pos), (m_data + pos + nbElement), sizeToMove*sizeof(MY_TYPE) );
} }
} }
// Request resize of the current buffer // Request resize of the current buffer
@ -578,12 +534,8 @@ namespace etk
// move curent data // move curent data
int32_t sizeToMove = (tmpSize - (pos+nbElement)); int32_t sizeToMove = (tmpSize - (pos+nbElement));
if ( 0 < sizeToMove) { if ( 0 < sizeToMove) {
if(true == m_mode) { for (int32_t iii=0; iii<sizeToMove; iii++) {
for (int32_t iii=0; iii<sizeToMove; iii++) { m_data[pos+iii] = m_data[pos+nbElement+iii];
m_data[pos+iii] = m_data[pos+nbElement+iii];
}
} else {
memmove((m_data + pos), (m_data + pos + nbElement), sizeToMove*sizeof(MY_TYPE) );
} }
} }
// Request resize of the current buffer // Request resize of the current buffer
@ -668,31 +620,16 @@ namespace etk
} }
int32_t requestSize = m_allocated; int32_t requestSize = m_allocated;
// set the size with the corect chose type : // set the size with the corect chose type :
if (newSize == m_allocated) { if (newSize == requestSize) {
return; return;
} else if (newSize < requestSize) { } else if (newSize < requestSize) {
// down the size of the vector: // we did not reove data ???
if (0==m_increment) {
// never down size...
} else {
int32_t devide = m_increment;
if (devide == 0) {
devide = 1;
}
int32_t numberOfStep = m_allocated / devide;
if (newSize< ((numberOfStep-2)*devide + devide/2) ) {
//Allow Reallocation of a new size shorter
requestSize = ((newSize / devide)+1) * devide;
}
}
} else { } else {
while(newSize > requestSize) { while(newSize > requestSize) {
if (0 == requestSize) { if (0 == requestSize) {
requestSize = 1; requestSize = 1;
} else if (0==m_increment) {
requestSize = requestSize * 2;
} else { } else {
requestSize = (requestSize + m_increment); requestSize = requestSize * 2;
} }
} }
} }
@ -718,12 +655,8 @@ namespace etk
} }
// copy data in the new pool // copy data in the new pool
int32_t nbElements = etk_min(requestSize, m_allocated); int32_t nbElements = etk_min(requestSize, m_allocated);
if(true == m_mode) { for(int32_t iii=0; iii<nbElements; iii++) {
for(int32_t iii=0; iii<nbElements; iii++) { m_dataTmp[iii] = m_data[iii];
m_dataTmp[iii] = m_data[iii];
}
} else {
memmove(m_dataTmp, m_data, nbElements*sizeof(MY_TYPE) );
} }
// switch pointer: // switch pointer:
MY_TYPE* m_dataTmp2 = m_data; MY_TYPE* m_dataTmp2 = m_data;
@ -738,12 +671,10 @@ namespace etk
} }
}; };
/** /**
* @brief List classes ... * @brief List classes ...
* *
* @tparam[in] T The type of objects to store. * @tparam[in] T The type of objects to store.
* @tparam[in] INC Incrementation mode (0 : Exponential to 200 and increment by stemp of 200)
* *
* m_data * m_data
* ---------- |-----------------------| * ---------- |-----------------------|