SmallVector.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Defines small vector with inplace storage.
  34. Based on CppCon 2016: Chandler Carruth "High Performance Code 201: Hybrid Data Structures" */
  35. #pragma once
  36. #ifndef AI_SMALLVECTOR_H_INC
  37. #define AI_SMALLVECTOR_H_INC
  38. #ifdef __GNUC__
  39. # pragma GCC system_header
  40. #endif
  41. namespace Assimp {
  42. // --------------------------------------------------------------------------------------------
  43. /// @brief Small vector with inplace storage.
  44. ///
  45. /// Reduces heap allocations when list is shorter. It uses a small array for a dedicated size.
  46. /// When the growing gets bigger than this small cache a dynamic growing algorithm will be
  47. /// used.
  48. // --------------------------------------------------------------------------------------------
  49. template<typename T, unsigned int Capacity>
  50. class SmallVector {
  51. public:
  52. /// @brief The default class constructor.
  53. SmallVector() :
  54. mStorage(mInplaceStorage),
  55. mSize(0),
  56. mCapacity(Capacity) {
  57. // empty
  58. }
  59. /// @brief The class destructor.
  60. ~SmallVector() {
  61. if (mStorage != mInplaceStorage) {
  62. delete [] mStorage;
  63. }
  64. }
  65. /// @brief Will push a new item. The capacity will grow in case of a too small capacity.
  66. /// @param item [in] The item to push at the end of the vector.
  67. void push_back(const T& item) {
  68. if (mSize < mCapacity) {
  69. mStorage[mSize++] = item;
  70. return;
  71. }
  72. push_back_and_grow(item);
  73. }
  74. /// @brief Will resize the vector.
  75. /// @param newSize [in] The new size.
  76. void resize(size_t newSize) {
  77. if (newSize > mCapacity) {
  78. grow(newSize);
  79. }
  80. mSize = newSize;
  81. }
  82. /// @brief Returns the current size of the vector.
  83. /// @return The current size.
  84. size_t size() const {
  85. return mSize;
  86. }
  87. /// @brief Returns a pointer to the first item.
  88. /// @return The first item as a pointer.
  89. T* begin() {
  90. return mStorage;
  91. }
  92. /// @brief Returns a pointer to the end.
  93. /// @return The end as a pointer.
  94. T* end() {
  95. return &mStorage[mSize];
  96. }
  97. /// @brief Returns a const pointer to the first item.
  98. /// @return The first item as a const pointer.
  99. T* begin() const {
  100. return mStorage;
  101. }
  102. /// @brief Returns a const pointer to the end.
  103. /// @return The end as a const pointer.
  104. T* end() const {
  105. return &mStorage[mSize];
  106. }
  107. SmallVector(const SmallVector &) = delete;
  108. SmallVector(SmallVector &&) = delete;
  109. SmallVector &operator = (const SmallVector &) = delete;
  110. SmallVector &operator = (SmallVector &&) = delete;
  111. private:
  112. void grow( size_t newCapacity) {
  113. T* oldStorage = mStorage;
  114. T* newStorage = new T[newCapacity];
  115. std::memcpy(newStorage, oldStorage, mSize * sizeof(T));
  116. mStorage = newStorage;
  117. mCapacity = newCapacity;
  118. if (oldStorage != mInplaceStorage) {
  119. delete [] oldStorage;
  120. }
  121. }
  122. void push_back_and_grow(const T& item) {
  123. grow(mCapacity + Capacity);
  124. mStorage[mSize++] = item;
  125. }
  126. T* mStorage;
  127. size_t mSize;
  128. size_t mCapacity;
  129. T mInplaceStorage[Capacity];
  130. };
  131. } // end namespace Assimp
  132. #endif // !! AI_SMALLVECTOR_H_INC