Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 09:30:10

0001 /*
0002 Open Asset Import Library (assimp)
0003 ----------------------------------------------------------------------
0004 
0005 Copyright (c) 2006-2024, assimp team
0006 
0007 All rights reserved.
0008 
0009 Redistribution and use of this software in source and binary forms,
0010 with or without modification, are permitted provided that the
0011 following conditions are met:
0012 
0013 * Redistributions of source code must retain the above
0014   copyright notice, this list of conditions and the
0015   following disclaimer.
0016 
0017 * Redistributions in binary form must reproduce the above
0018   copyright notice, this list of conditions and the
0019   following disclaimer in the documentation and/or other
0020   materials provided with the distribution.
0021 
0022 * Neither the name of the assimp team, nor the names of its
0023   contributors may be used to endorse or promote products
0024   derived from this software without specific prior
0025   written permission of the assimp team.
0026 
0027 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0028 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0029 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0030 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0031 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0032 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0033 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0034 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0035 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0036 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0037 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0038 
0039 ----------------------------------------------------------------------
0040 */
0041 
0042 /** @file Defines small vector with inplace storage.
0043 Based on CppCon 2016: Chandler Carruth "High Performance Code 201: Hybrid Data Structures" */
0044 
0045 #pragma once
0046 #ifndef AI_SMALLVECTOR_H_INC
0047 #define AI_SMALLVECTOR_H_INC
0048 
0049 #ifdef __GNUC__
0050 #   pragma GCC system_header
0051 #endif
0052 
0053 namespace Assimp {
0054 
0055 // --------------------------------------------------------------------------------------------
0056 /// @brief Small vector with inplace storage.
0057 ///
0058 /// Reduces heap allocations when list is shorter. It uses a small array for a dedicated size.
0059 /// When the growing gets bigger than this small cache a dynamic growing algorithm will be
0060 /// used.
0061 // --------------------------------------------------------------------------------------------
0062 template<typename T, unsigned int Capacity>
0063 class SmallVector {
0064 public:
0065     /// @brief  The default class constructor.
0066     SmallVector() :
0067             mStorage(mInplaceStorage),
0068             mSize(0),
0069             mCapacity(Capacity) {
0070         // empty
0071     }
0072 
0073     /// @brief  The class destructor.
0074     ~SmallVector() {
0075         if (mStorage != mInplaceStorage) {
0076             delete [] mStorage;
0077         }
0078     }
0079 
0080     /// @brief  Will push a new item. The capacity will grow in case of a too small capacity.
0081     /// @param  item    [in] The item to push at the end of the vector.
0082     void push_back(const T& item) {
0083         if (mSize < mCapacity) {
0084             mStorage[mSize++] = item;
0085             return;
0086         }
0087 
0088         push_back_and_grow(item);
0089     }
0090 
0091     /// @brief  Will resize the vector.
0092     /// @param  newSize     [in] The new size.
0093     void resize(size_t newSize) {
0094         if (newSize > mCapacity) {
0095             grow(newSize);
0096         }
0097         mSize = newSize;
0098     }
0099 
0100     /// @brief  Returns the current size of the vector.
0101     /// @return The current size.
0102     size_t size() const {
0103         return mSize;
0104     }
0105 
0106     /// @brief  Returns a pointer to the first item.
0107     /// @return The first item as a pointer.
0108     T* begin() {
0109         return mStorage;
0110     }
0111 
0112     /// @brief  Returns a pointer to the end.
0113     /// @return The end as a pointer.
0114     T* end() {
0115         return &mStorage[mSize];
0116     }
0117 
0118     /// @brief  Returns a const pointer to the first item.
0119     /// @return The first item as a const pointer.
0120     T* begin() const {
0121         return mStorage;
0122     }
0123 
0124     /// @brief  Returns a const pointer to the end.
0125     /// @return The end as a const pointer.
0126     T* end() const {
0127         return &mStorage[mSize];
0128     }
0129 
0130     SmallVector(const SmallVector &) = delete;
0131     SmallVector(SmallVector &&) = delete;
0132     SmallVector &operator = (const SmallVector &) = delete;
0133     SmallVector &operator = (SmallVector &&) = delete;
0134 
0135 private:
0136     void grow( size_t newCapacity) {
0137         T* oldStorage = mStorage;
0138         T* newStorage = new T[newCapacity];
0139 
0140         std::memcpy(newStorage, oldStorage, mSize * sizeof(T));
0141 
0142         mStorage = newStorage;
0143         mCapacity = newCapacity;
0144 
0145         if (oldStorage != mInplaceStorage) {
0146             delete [] oldStorage;
0147         }
0148     }
0149 
0150     void push_back_and_grow(const T& item) {
0151         grow(mCapacity + Capacity);
0152 
0153         mStorage[mSize++] = item;
0154     }
0155 
0156     T* mStorage;
0157     size_t mSize;
0158     size_t mCapacity;
0159     T mInplaceStorage[Capacity];
0160 };
0161 
0162 } // end namespace Assimp
0163 
0164 #endif // !! AI_SMALLVECTOR_H_INC