📄 vector.h
字号:
void clear(); bool empty() const; iterator end(); const_iterator end() const; iterator erase( iterator itr ); iterator erase( iterator first, iterator last ); /** * expand - Although this implementation does not support automatic * resizing or reallocation, the interface is extended beyond that of * standard STL with this method that allows the maximum storage size * to be increased. This is an expensive operation, and involves the * use of the copy constructor, and Swap. Accordingly, in the event of * an allocation failure, the object will remain untouched, and a debug * ASSERT will be raised. * @param maxSize is a size_type that describes the number of objects * that we wish storage to be reserved for. If the max_size() of 'other' * is greater, then this size will be requested instead. The result will * be reflected by max_size(), and this should be checked against the * requested size and/or other.max_size() **BEFORE THE OBJECT IS USED**. * An assert is also included to trap memory allocation failure in debug * builds. */ void expand(size_type maxSize); /** * resize is a method that allows the 'size()' of the object to be altered. * It achieves this in two ways. If the size is greater than size_, the * elements upto size are constructed, and size is set accordingly. If * the newSize is less than size_, the elemnts between newSize and size_ are * destroyed, and the size_ is trimmed back. THIS OPERATION DOES NOT * RANGE CHECK MAX_SIZE - it is required that the user observes max_size. * @param newSize is a size_type that specifies the new size_ of the * object. */ void resize(size_type newSize); /** * resize is a method that allows the 'size()' of the object to be altered. * It achieves this in two ways. If the size is greater than size_, the * elements upto size are constructed, and size is set accordingly. If * the newSize is less than size_, the elemnts between newSize and size_ are * destroyed, and the size_ is trimmed back. THIS OPERATION DOES NOT * RANGE CHECK MAX_SIZE - it is required that the user observes max_size. * @param newSize is a size_type that specifies the new size_ of the * object. * @param fill_value is a value_type that describes the value which should * be copied into the slots when size_ is increased. */ void resize( size_type newSize, value_type fill_value ); iterator insert( iterator position, const_reference value); iterator insert( iterator position, const_iterator first, const_iterator last ); // bool isComplete() const { return buffer_; } size_type max_size() const; void push_back(const_reference value); void pop_back(); reverse_iterator rbegin(); const_reverse_iterator rbegin() const; reverse_iterator rend(); const_reverse_iterator rend() const; size_type size() const; private: size_type maxSize_; size_type size_; T* buffer_; void Swap( Vector<T, Allocator, false>& other ); }; // }}} // ========================================================================= // ========================================================================= // ========================================================================= // ========================================================================= // Implementation for generalistaion template<typename T, typename Allocator, bool bUseCtor> bool operator == (const Vector<T, Allocator, bUseCtor>& lhs, const Vector<T, Allocator, bUseCtor>& rhs) // {{{ { return (lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin())); } // }}} /** * @ctor * creates a lightweight vector style object with space reserved for * maxSize elements. * @param maxSize is a size_type that describes the number of objects * that we wish storage to be reserved for. This will be reflected by * max_size(), and this should be checked against the requested size * *BEFORE THE OBJECT IS USED**. An assert is also included to trap * memory allocation failure in debug builds. */ template<typename T, typename Allocator, bool bUseCtor> Vector<T, Allocator, bUseCtor>::Vector(size_type maxSize) : maxSize_(maxSize), size_(0), buffer_(0) // {{{ { if ( maxSize_ && !(buffer_ = Allocator::allocate( maxSize_ ) ) ) { TRACE("$R** Vector allocation FAILURE, trying to allocate 0x%08x bytes**\n", maxSize);// ASSERT(0); maxSize_ = 0; } } // }}} /** * @ctor * creates a lightweight vector style object with space reserved for * the larger of maxSize and other.max_size() elements. The body of * the construction in the event of successsful allocation will then * duplicate the data of 'other' in the new object. * @param other is the object from which data will be copied when the * vector is established. * @param maxSize is a size_type that describes the number of objects * that we wish storage to be reserved for. If the max_size() of 'other' * is greater, then this size will be requested instead. The result will * be reflected by max_size(), and this should be checked against the * requested size and/or other.max_size() * *BEFORE THE OBJECT IS USED**. * An assert is also included to trap memory allocation failure in debug * builds. */ template<typename T, typename Allocator, bool bUseCtor> Vector<T, Allocator, bUseCtor>::Vector(const Vector<T, Allocator, bUseCtor>& other, size_type maxSize) : maxSize_(other.max_size() > maxSize ? other.max_size() : maxSize), size_(0), buffer_(0) // {{{ { if ( maxSize_ && !(buffer_ = Allocator::allocate( maxSize_ ) ) ) { TRACE("$R** Vector allocation FAILURE, trying to allocate 0x%08x bytes**\n", maxSize);// ASSERT(0); maxSize_ = 0; } else {#ifndef OXSEMI_NO_EXCEPTIONS try {#endif const_iterator o_itr = other.begin(); const_iterator o_end = other.end(); while (o_itr != o_end) { push_back(*o_itr++); }#ifndef OXSEMI_NO_EXCEPTIONS } catch (...) { clear(); Allocator::deallocate(buffer_); buffer_ = 0; throw; }#endif } } // }}} template<typename T, typename Allocator, bool bUseCtor> Vector<T, Allocator, bUseCtor>& Vector<T, Allocator, bUseCtor>::operator=(const Vector<T, Allocator, bUseCtor>& other) // {{{ { Vector<T, Allocator, bUseCtor> temp(other); Swap(temp); return *this; } // }}} template<typename T, typename Allocator, bool bUseCtor> Vector<T, Allocator, bUseCtor>::~Vector() // {{{ { clear(); Allocator::deallocate(buffer_); } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::reference Vector<T, Allocator, bUseCtor>::operator[](size_type index) // {{{ { ASSERT( index < max_size() ); if (index >= size()) { ASSERT( index < max_size() );//TRACE("$R BLIND VECTOR EXPANSION %ux%u (%u::%u)\n",sizeof(T),index-size()+1,index,max_size()); // Default construct elements from current end to the index requested for (size_type i=size(); i <= index; ++i) { push_back(T()); } } return buffer_[index]; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::const_reference Vector<T, Allocator, bUseCtor>::operator[](size_type index) const // {{{ { //TRACE("$R%d/%d $n", index, size()); ASSERT( index < size() ); return buffer_[index]; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::reference Vector<T, Allocator, bUseCtor>::at(size_type index) // {{{ { return buffer_[index]; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::const_reference Vector<T, Allocator, bUseCtor>::at(size_type index) const // {{{ { return buffer_[index]; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::reference Vector<T, Allocator, bUseCtor>::front() // {{{ { return buffer_[0]; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::const_reference Vector<T, Allocator, bUseCtor>::front() const // {{{ { return buffer_[0]; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::reference Vector<T, Allocator, bUseCtor>::back() // {{{ { return buffer_[size()-1]; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::const_reference Vector<T, Allocator, bUseCtor>::back() const // {{{ { return buffer_[size()-1]; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::iterator Vector<T, Allocator, bUseCtor>::begin() // {{{ { return buffer_; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::const_iterator Vector<T, Allocator, bUseCtor>::begin() const // {{{ { return buffer_; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline void Vector<T, Allocator, bUseCtor>::clear() // {{{ { iterator endOf = this->end(); for (iterator itr = begin(); itr < endOf; ++itr) { itr->~T(); } size_ = 0; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline bool Vector<T, Allocator, bUseCtor>::empty() const // {{{ { return !size_; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::iterator Vector<T, Allocator, bUseCtor>::end() // {{{ { return buffer_ + size_; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::const_iterator Vector<T, Allocator, bUseCtor>::end() const // {{{ { return buffer_ + size_; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::iterator Vector<T, Allocator, bUseCtor>::erase(iterator itr) // {{{ { // Save pointer to element to be removed, as this after erasure this will // point to the element that was after the erased element before erasure iterator next = itr; // Check the element is within the active elements of the array. ASSERT( itr >= begin() ); ASSERT( itr < end() ); // Shift all elements from the erase point to the last filled element one // position towards the start of the container iterator last = end() - 1; while (itr < last) { *itr = *(itr + 1); ++itr; } // Destroy what was the last element in the container, which is now dupli- // cated one position towards the start of the container itr->~T(); // Decrement count of elements in the container --size_; return next; } // }}} template<typename T, typename Allocator, bool bUseCtor> inline typename Vector<T, Allocator, bUseCtor>::iterator Vector<T, Allocator, bUseCtor>::erase( iterator first, iterator last ) // {{{ { // Save pointer to element to be removed, as this after erasure this will // point to the element that was after the erased element before erasure iterator next = first; // Check the range is within the active elements of the array. ASSERT( first >= begin() ); ASSERT( last <= end() );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -