diff --git a/Sources/libetk/etk/Vector.h b/Sources/libetk/etk/Vector.h index c084ec72..50268c30 100644 --- a/Sources/libetk/etk/Vector.h +++ b/Sources/libetk/etk/Vector.h @@ -67,7 +67,7 @@ namespace etk * ---------------------------------------- * */ - template class Vector + template class Vector { public: class Iterator @@ -210,10 +210,11 @@ namespace etk }; private: - MY_TYPE * m_data; //!< pointer on the curetn table of Data - int32_t m_size; //!< nb Element in the buffer - int32_t m_allocated; //!< Current allocated size - int32_t m_increment; //!< methode of increment + MY_TYPE * m_data; //!< pointer on the curetn table of Data + int32_t m_size; //!< nb Element in the buffer + 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: /** * @brief Create an empty vector @@ -223,7 +224,8 @@ namespace etk m_data(NULL), m_size(0), m_allocated(0), - m_increment(1) + m_increment(1), + m_mode(true) { ChangeAllocation(count); } @@ -240,10 +242,20 @@ namespace etk m_data = NULL; //TK_DEBUG("USE Specific vector allocator ... Evb.m_size=" << Evb.m_size << " Evb.m_increment=" << Evb.m_increment); // allocate all same data - ETK_MALLOC(m_data, m_allocated, MY_TYPE); - TK_ASSERT(NULL!=m_data, "Error in data allocation"); + m_data = new MY_TYPE[m_allocated]; + if (NULL==m_data) { + TK_CRITICAL("Vector : Error in data allocation ... might nor work corectly anymore"); + return; + } // Copy all data ... - memcpy(m_data, Evb.m_data, m_allocated * sizeof(MY_TYPE) ); + if(true == m_mode) { + for(int32_t iii=0; iii= m_size) { + TK_ERROR("Resize does not work corectly ... not added item"); + return; + } + if(true == m_mode) { + for (int32_t iii=0; iiim_size) { - TK_ERROR(" can not insert Element at this position : " << pos << " > " << m_size<< " add it at the end ... "); - PushBack(item); - return; - } - int32_t tmpSize = m_size; - // Request resize of the current buffer - Resize(m_size+1); - // move curent data - int32_t sizeToMove = (tmpSize - pos)*sizeof(MY_TYPE); - if ( 0 < sizeToMove) { - memmove((m_data + pos + 1), (m_data + pos), sizeToMove ); - } - // affectation of the current element - m_data[pos] = item; - } /** * @brief @@ -446,18 +467,81 @@ namespace etk PushBack(item, nbElement); return; } - int32_t tmpSize = m_size; + int32_t idx = m_size; // Request resize of the current buffer Resize(m_size+nbElement); + if (idx>=m_size) { + TK_ERROR("Resize does not work corectly ... not added item"); + return; + } // move curent data (after the position) - int32_t sizeToMove = (tmpSize - pos)*sizeof(MY_TYPE); + int32_t sizeToMove = (idx - pos); if ( 0 < sizeToMove) { - memmove((m_data + pos + nbElement), (m_data + pos), sizeToMove ); + if(true == m_mode) { + for (int32_t iii=1; iii<=sizeToMove; 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 - memcpy(&m_data[pos], item, nbElement*sizeof(MY_TYPE) ); + if(true == m_mode) { + for (int32_t iii=0; iiim_size) { + TK_ERROR(" can not Erase Len Element at this position : " << pos << " > " << m_size); + return; + } + if (pos+nbElement>m_size) { + nbElement = m_size - pos; + } + int32_t idx = m_size; + // move curent data + int32_t sizeToMove = (idx - (pos+nbElement)); + if ( 0 < sizeToMove) { + if(true == m_mode) { + for (int32_t iii=0; iiim_size) { - TK_ERROR(" can not Erase Element at this position : " << pos << " > " << m_size); - return; - } - int32_t tmpSize = m_size; - int32_t sizeToMove = (tmpSize - (pos+1))*sizeof(MY_TYPE); - if ( 0 < sizeToMove) { - // move curent data - memmove((m_data + pos), (m_data + pos + 1), sizeToMove ); - } - // Request resize of the current buffer - Resize(m_size-1); + EraseLen(pos, 1); } /** @@ -503,41 +576,20 @@ namespace etk int32_t nbElement = m_size - pos; int32_t tmpSize = m_size; // move curent data - int32_t sizeToMove = (tmpSize - (pos+nbElement))*sizeof(MY_TYPE); + int32_t sizeToMove = (tmpSize - (pos+nbElement)); if ( 0 < sizeToMove) { - memmove((m_data + pos), (m_data + pos + nbElement), sizeToMove ); + if(true == m_mode) { + for (int32_t iii=0; iiim_size) { - TK_ERROR(" can not Erase Len Element at this position : " << pos << " > " << m_size); - return; - } - if (pos+nbElement>m_size) { - nbElement = m_size - pos; - } - int32_t tmpSize = m_size; - // move curent data - int32_t sizeToMove = (tmpSize - (pos+nbElement))*sizeof(MY_TYPE); - if ( 0 < sizeToMove) { - memmove((m_data + pos), (m_data + pos + nbElement), sizeToMove ); - } - // Request resize of the current buffer - Resize(m_size-nbElement); - } /** * @brief extract data between two point : @@ -545,7 +597,7 @@ namespace etk * @param[in] posEnd End position to extract data * @return the extracted vector */ - Vector Extract(int32_t posStart = 0, int32_t posEnd=0x7FFFFFFF) + Vector Extract(int32_t posStart = 0, int32_t posEnd=0x7FFFFFFF) { Vector out; if (posStart < 0) { @@ -562,21 +614,6 @@ namespace etk return out; } - /** - * @brief Set the minimum allocation in memory for the curent vector ==> reallocate the - * buffer to fit exactly the mumber of element needed - */ - void Fit(void) - { - if (m_size > m_allocated) { - // Reallocate the curent data to the correct size ... - ETK_REALLOC(m_data, m_size, MY_TYPE); - } - // Check result with assert : - TK_ASSERT(NULL!=m_data, "Error in data Fitting"); - m_allocated = m_size; - } - /** * @brief Get an iterator an an specific position * @param[in] pos Requested position of the iterator in the vector @@ -666,13 +703,36 @@ namespace etk // check if something is allocated : if (NULL == m_data) { // no data allocated ==> request an allocation (might be the first) - ETK_MALLOC(m_data, requestSize, MY_TYPE); + m_data = new MY_TYPE[requestSize]; + if (NULL==m_data) { + TK_CRITICAL("Vector : Error in data allocation request allocation:" << requestSize << "*" << (int32_t)(sizeof(MY_TYPE)) << "bytes" ); + return; + } + // no data to copy } else { - // move datas - ETK_REALLOC(m_data, requestSize, MY_TYPE); + // allocate a new pool of data: + MY_TYPE* m_dataTmp = new MY_TYPE[requestSize]; + if (NULL==m_dataTmp) { + TK_CRITICAL("Vector : Error in data allocation request allocation:" << requestSize << "*" << (int32_t)(sizeof(MY_TYPE)) << "bytes" ); + return; + } + // copy data in the new pool + int32_t nbElements = etk_min(requestSize, m_allocated); + if(true == m_mode) { + for(int32_t iii=0; iii class VectorP private Vector + template class List { };