From b8ad01984df54254c8849967a1cf9e244c87a718 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Fri, 13 Oct 2017 00:12:05 +0200 Subject: [PATCH] [DEV] remove deprecated code --- etk/Vector.hpp | 453 +++++++++++++++---------------------------------- 1 file changed, 138 insertions(+), 315 deletions(-) diff --git a/etk/Vector.hpp b/etk/Vector.hpp index 363bbdd..861d19e 100644 --- a/etk/Vector.hpp +++ b/etk/Vector.hpp @@ -8,8 +8,6 @@ #include //#include #include -// better mode of vector ==> increate the code complexity -#define ETK_ENABLE_PLACEMENT_NEW 1 //#define ETK_VECTOR_DEBUG(...) printf(__VA_ARGS__) #define ETK_VECTOR_DEBUG(...) do {} while (false) @@ -275,38 +273,20 @@ namespace etk { * @brief Re-copy constructor (copy all needed data) * @param[in] _obj Vector that might be copy */ - #ifndef ETK_ENABLE_PLACEMENT_NEW - Vector(const etk::Vector& _obj): - m_data(nullptr), - m_size(_obj.m_size), - m_allocated(_obj.m_allocated) { - // allocate all same data - m_data = new ETK_VECTOR_TYPE[m_allocated]; - if (m_data == nullptr) { - return; - } - // Copy all data ... - for(size_t iii=0; iii& _obj): + m_data(nullptr), + m_size(0), + m_allocated(0) { + reserve(_obj.m_size); + for(size_t iii=0; iii<_obj.m_size; iii++) { + #if 0 + pushBack(_obj.m_data[iii]); + #else + new ((char*)&m_data[iii]) ETK_VECTOR_TYPE(etk::move(_obj.m_data[iii])); + #endif } - #else - Vector(const etk::Vector& _obj): - m_data(nullptr), - m_size(0), - m_allocated(0) { - reserve(_obj.m_size); - for(size_t iii=0; iii<_obj.m_size; iii++) { - #if 0 - pushBack(_obj.m_data[iii]); - #else - new ((char*)&m_data[iii]) ETK_VECTOR_TYPE(etk::move(_obj.m_data[iii])); - #endif - } - m_size = _obj.m_size; - } - #endif + m_size = _obj.m_size; + } /** * @brief Move operator of elements * @param[in] _obj Object to move @@ -324,21 +304,17 @@ namespace etk { */ ~Vector() { if (m_data != nullptr) { - #ifndef ETK_ENABLE_PLACEMENT_NEW - delete[] m_data; - #else - for(size_t iii=0; iii& _obj) { - if (this != &_obj) { - if (m_data != nullptr) { - delete[] m_data; - m_data = nullptr; - } - // Set the new value - m_allocated = _obj.m_allocated; - m_size = _obj.m_size; - // allocate all same data - m_data = new ETK_VECTOR_TYPE[m_allocated]; - if (m_data == nullptr) { - return *this; - } - for(size_t iii=0; iii& _obj) { + // remove all previous elements + clear(); + // Force a specicfic size + reserve(_obj.m_size); + for(size_t iii=0; iii<_obj.m_size; iii++) { + #if 0 + pushBack(_obj.m_data[iii]); + #else + new ((char*)&m_data[iii]) ETK_VECTOR_TYPE(etk::move(_obj.m_data[iii])); + #endif } - #else - Vector& operator=(const etk::Vector& _obj) { - // remove all previous elements - clear(); - // Force a specicfic size - reserve(_obj.m_size); - for(size_t iii=0; iii<_obj.m_size; iii++) { - #if 0 - pushBack(_obj.m_data[iii]); - #else - new ((char*)&m_data[iii]) ETK_VECTOR_TYPE(etk::move(_obj.m_data[iii])); - #endif - } - m_size = _obj.m_size; - // Return the current pointer - return *this; - } - #endif - + m_size = _obj.m_size; + // Return the current pointer + return *this; + } /** * @brief Add at the Last position of the Vector * @param[in] _obj Element to add at the end of vector */ Vector& operator+= (const etk::Vector& _obj) { - #ifndef ETK_ENABLE_PLACEMENT_NEW - size_t numberElement = _obj.size(); - size_t idElement = m_size; - resize(m_size+numberElement); - if (m_size <= idElement) { - //TK_CRITICAL("allocation error"); - return *this; - } - for(size_t iii=0; iii m_size) { - //TK_ERROR("Resize does not work correctly ... not added item"); - return; - } - for (size_t iii=0; iii<_nbElement; iii++) { - m_data[idElement+iii] = _item[iii]; - } - #else - reserve(m_size+_nbElement); - if (m_size > m_allocated) { - //TK_ERROR("Resize does not work correctly ... not added item"); - return; - } - for (size_t iii=0; iii<_nbElement; iii++) { - new ((char*)&m_data[m_size+iii]) ETK_VECTOR_TYPE(_item[iii]); - m_size += 1; - } - #endif + reserve(m_size+_nbElement); + if (m_size > m_allocated) { + //TK_ERROR("Resize does not work correctly ... not added item"); + return; + } + for (size_t iii=0; iii<_nbElement; iii++) { + new ((char*)&m_data[m_size+iii]) ETK_VECTOR_TYPE(_item[iii]); + m_size += 1; + } } private: void pushBackN(const ETK_VECTOR_TYPE& _value) { @@ -641,57 +545,31 @@ namespace etk { pushBack(_item, _nbElement); return; } - #ifndef ETK_ENABLE_PLACEMENT_NEW - size_t idElement = m_size; - // Request resize of the current buffer - resize(m_size+_nbElement); - if (idElement >= m_size) { - //TK_ERROR("Resize does not work correctly ... not added item"); - return; + // move current data (after the position) + size_t sizeToMove = (m_size - _pos); + // Request resize of the current buffer + reserve(m_size+_nbElement); + if (sizeToMove > 0) { + for (size_t iii=1; iii<=sizeToMove; iii++) { + // placement allocate of the element + new ((char*)&m_data[m_size+_nbElement-iii]) ETK_VECTOR_TYPE(etk::move(m_data[m_size-iii])); + // Remove previous element ==> simplify code. + m_data[m_size-iii].~ETK_VECTOR_TYPE(); + #ifdef DEBUG + // we place bad data to permit to detect stipid thing that is done in C++ code when developement is in progress. + // Only in debug this is really slow ... and for the real allocation of memory + for (size_t kkk=(m_size-iii)*sizeof(ETK_VECTOR_TYPE); kkk 0) { - for (size_t iii=1; iii<=sizeToMove; iii++) { - // tODO: better explicite the swap... - #ifndef ETK_ENABLE_PLACEMENT_NEW - m_data[m_size-iii] = etk::move(m_data[idElement-iii]); - #else - etk::swap(m_data[m_size-iii], m_data[idElement-iii]); - #endif - } - } - // affectation of all input element - for (size_t iii=0; iii<_nbElement; iii++) { - m_data[_pos+iii] = etk::move(_item[iii]); - } - #else - // move current data (after the position) - size_t sizeToMove = (m_size - _pos); - // Request resize of the current buffer - reserve(m_size+_nbElement); - if (sizeToMove > 0) { - for (size_t iii=1; iii<=sizeToMove; iii++) { - // placement allocate of the element - new ((char*)&m_data[m_size+_nbElement-iii]) ETK_VECTOR_TYPE(etk::move(m_data[m_size-iii])); - // Remove previous element ==> simplify code. - m_data[m_size-iii].~ETK_VECTOR_TYPE(); - #ifdef DEBUG - // we place bad data to permit to detect stipid thing that is done in C++ code when developement is in progress. - // Only in debug this is really slow ... and for the real allocation of memory - for (size_t kkk=(m_size-iii)*sizeof(ETK_VECTOR_TYPE); kkk idElement) { - // initialize data ... - for(size_t iii=idElement; iii<_newSize; iii++) { - m_data[iii] = _basicElement; - } - } - #else - // Reallocate memory - if (_newSize > m_size) { - if (_newSize > m_allocated) { - changeAllocation(_newSize); - } - for (size_t iii=m_size; iii<_newSize; ++iii) { - new ((char*)&m_data[iii]) ETK_VECTOR_TYPE(_basicElement); - } - m_size = _newSize; - return; - } else if (_newSize == m_size) { - return; + // Reallocate memory + if (_newSize > m_size) { + if (_newSize > m_allocated) { + changeAllocation(_newSize); } for (size_t iii=m_size; iii<_newSize; ++iii) { - m_data[iii].~ETK_VECTOR_TYPE(); - #ifdef DEBUG - // we place bad data to permit to detect stipid thing that is done in C++ code when developement is in progress. - // Only in debug this is really slow ... and for the real allocation of memory - for (size_t kkk=iii*sizeof(ETK_VECTOR_TYPE); kkk %zu\n", m_size, _newSize); - #ifndef ETK_ENABLE_PLACEMENT_NEW - // Reallocate memory + // Reallocate memory + if (_newSize > m_size) { if (_newSize > m_allocated) { changeAllocation(_newSize); - } else if (_newSize == m_allocated) { - return; } - for - m_size = _newSize; - #else - // Reallocate memory - if (_newSize > m_size) { - if (_newSize > m_allocated) { - changeAllocation(_newSize); + for (size_t iii=m_size; iii<_newSize; ++iii) { + new ((char*)&m_data[iii]) ETK_VECTOR_TYPE(); + } + } else if (_newSize == m_size) { + return; + } + ETK_VECTOR_DEBUG("Reduce %zu => %zu\n", m_size, _newSize); + for (size_t iii=_newSize; iii %zu\n", m_size, _newSize); - for (size_t iii=_newSize; iii %zu\n", m_size, _newSize); // Reallocate memory @@ -961,7 +804,6 @@ namespace etk { } m_size = _newSize; } - #endif public: /** * @brief Force the container to have a minimum size in memory allocation @@ -1002,11 +844,7 @@ namespace etk { // check if something is allocated : if (m_data == nullptr) { // no data allocated ==> request an allocation (might be the first) - #ifndef ETK_ENABLE_PLACEMENT_NEW - m_data = new ETK_VECTOR_TYPE[requestSize]; - #else - m_data = (ETK_VECTOR_TYPE*)(new char[sizeof(ETK_VECTOR_TYPE)*requestSize]); - #endif + m_data = (ETK_VECTOR_TYPE*)(new char[sizeof(ETK_VECTOR_TYPE)*requestSize]); if (m_data == nullptr) { //TK_CRITICAL("Vector : Error in data allocation request allocation:" << requestSize << "*" << (int32_t)(sizeof(ETK_VECTOR_TYPE)) << "bytes" ); m_allocated = 0; @@ -1021,16 +859,9 @@ namespace etk { #endif } else { // allocate a new pool of data: - #ifndef ETK_ENABLE_PLACEMENT_NEW - ETK_VECTOR_TYPE* dataTmp = new ETK_VECTOR_TYPE[requestSize]; - #else - ETK_VECTOR_TYPE* dataTmp = (ETK_VECTOR_TYPE*)(new char[sizeof(ETK_VECTOR_TYPE)*requestSize]); - #endif + ETK_VECTOR_TYPE* dataTmp = (ETK_VECTOR_TYPE*)(new char[sizeof(ETK_VECTOR_TYPE)*requestSize]); if (dataTmp == nullptr) { //TK_CRITICAL("Vector : Error in data allocation request allocation:" << requestSize << "*" << (int32_t)(sizeof(ETK_VECTOR_TYPE)) << "bytes" ); - #if 0 - m_allocated = 0; - #endif return; } #ifdef DEBUG @@ -1043,20 +874,16 @@ namespace etk { // copy data in the new pool size_t nbElements = etk::min(requestSize, m_allocated); for(size_t iii=0; iii