Adapt smallvector

- Add doc ( public header )
- Fix type error
pull/3085/head
Kim Kulling 2020-04-21 08:59:40 +02:00 committed by GitHub
parent 788f2f244e
commit 428b91154a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 4 deletions

View File

@ -53,12 +53,16 @@ Based on CppCon 2016: Chandler Carruth "High Performance Code 201: Hybrid Data S
namespace Assimp { namespace Assimp {
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
/** \brief Small vector with inplace storage. Reduces heap allocations when list is shorter /// @brief Small vector with inplace storage.
than initial capasity ///
*/ /// Reduces heap allocations when list is shorter. It uses a small array for a dedicated size.
/// When the growing gets bigger than this small cache a dynamic growing algorithm will be
/// used.
// --------------------------------------------------------------------------------------------
template<typename T, unsigned int Capacity> template<typename T, unsigned int Capacity>
class SmallVector { class SmallVector {
public: public:
/// @brief The default class constructor.
SmallVector() : SmallVector() :
mStorage(mInplaceStorage), mStorage(mInplaceStorage),
mSize(0), mSize(0),
@ -66,12 +70,15 @@ public:
// empty // empty
} }
/// @brief The class destructor.
~SmallVector() { ~SmallVector() {
if (mStorage != mInplaceStorage) { if (mStorage != mInplaceStorage) {
delete [] mStorage; delete [] mStorage;
} }
} }
/// @brief Will push a new item. The capacity will grow in case of a too small capacity.
/// @param item [in] The item to push at the end of the vector.
void push_back(const T& item) { void push_back(const T& item) {
if (mSize < mCapacity) { if (mSize < mCapacity) {
mStorage[mSize++] = item; mStorage[mSize++] = item;
@ -81,33 +88,50 @@ public:
push_back_and_grow(item); push_back_and_grow(item);
} }
void resize(unsigned int newSize) { /// @brief Will resize the vector.
/// @param newSize [in] The new size.
void resize(size_t newSize) {
if (newSize > mCapacity) { if (newSize > mCapacity) {
grow(newSize); grow(newSize);
} }
mSize = newSize; mSize = newSize;
} }
/// @brief Returns the current size of the vector.
/// @return The current size.
size_t size() const { size_t size() const {
return mSize; return mSize;
} }
/// @brief Returns a pointer to the first item.
/// @return The first item as a pointer.
T* begin() { T* begin() {
return mStorage; return mStorage;
} }
/// @brief Returns a pointer to the end.
/// @return The end as a pointer.
T* end() { T* end() {
return &mStorage[mSize]; return &mStorage[mSize];
} }
/// @brief Returns a const pointer to the first item.
/// @return The first item as a const pointer.
T* begin() const { T* begin() const {
return mStorage; return mStorage;
} }
/// @brief Returns a const pointer to the end.
/// @return The end as a const pointer.
T* end() const { T* end() const {
return &mStorage[mSize]; return &mStorage[mSize];
} }
SmallVector(const SmallVector &) = delete;
SmallVector(SmallVector &&) = delete;
SmallVector &operator = (const SmallVector &) = delete;
SmallVector &operator = (SmallVector &&) = delete;
private: private:
void grow( size_t newCapacity) { void grow( size_t newCapacity) {
T* oldStorage = mStorage; T* oldStorage = mStorage;