Vector.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifndef Spine_Vector_h
  30. #define Spine_Vector_h
  31. #include <spine/extension.h>
  32. #include <spine/SpineObject.h>
  33. #include <spine/SpineString.h>
  34. #include <assert.h>
  35. namespace spine {
  36. template<typename T>
  37. class SP_API Vector : public SpineObject {
  38. public:
  39. Vector() : _size(0), _capacity(0), _buffer(NULL) {
  40. }
  41. Vector(const Vector &inVector) : _size(inVector._size), _capacity(inVector._capacity), _buffer(NULL) {
  42. if (_capacity > 0) {
  43. _buffer = allocate(_capacity);
  44. for (size_t i = 0; i < _size; ++i) {
  45. construct(_buffer + i, inVector._buffer[i]);
  46. }
  47. }
  48. }
  49. ~Vector() {
  50. clear();
  51. deallocate(_buffer);
  52. }
  53. inline void clear() {
  54. for (size_t i = 0; i < _size; ++i) {
  55. destroy(_buffer + (_size - 1 - i));
  56. }
  57. _size = 0;
  58. }
  59. inline size_t getCapacity() const {
  60. return _capacity;
  61. }
  62. inline size_t size() const {
  63. return _size;
  64. }
  65. inline void setSize(size_t newSize, const T &defaultValue) {
  66. assert(newSize >= 0);
  67. size_t oldSize = _size;
  68. _size = newSize;
  69. if (_capacity < newSize) {
  70. _capacity = (int) (_size * 1.75f);
  71. if (_capacity < 8) _capacity = 8;
  72. _buffer = spine::SpineExtension::realloc<T>(_buffer, _capacity, __FILE__, __LINE__);
  73. }
  74. if (oldSize < _size) {
  75. for (size_t i = oldSize; i < _size; i++) {
  76. construct(_buffer + i, defaultValue);
  77. }
  78. }
  79. }
  80. inline void ensureCapacity(size_t newCapacity = 0) {
  81. if (_capacity >= newCapacity) return;
  82. _capacity = newCapacity;
  83. _buffer = SpineExtension::realloc<T>(_buffer, newCapacity, __FILE__, __LINE__);
  84. }
  85. inline void add(const T &inValue) {
  86. if (_size == _capacity) {
  87. // inValue might reference an element in this buffer
  88. // When we reallocate, the reference becomes invalid.
  89. // We thus need to create a defensive copy before
  90. // reallocating.
  91. T valueCopy = inValue;
  92. _capacity = (int) (_size * 1.75f);
  93. if (_capacity < 8) _capacity = 8;
  94. _buffer = spine::SpineExtension::realloc<T>(_buffer, _capacity, __FILE__, __LINE__);
  95. construct(_buffer + _size++, valueCopy);
  96. } else {
  97. construct(_buffer + _size++, inValue);
  98. }
  99. }
  100. inline void addAll(Vector<T> &inValue) {
  101. ensureCapacity(this->size() + inValue.size());
  102. for (size_t i = 0; i < inValue.size(); i++) {
  103. add(inValue[i]);
  104. }
  105. }
  106. inline void clearAndAddAll(Vector<T> &inValue) {
  107. this->clear();
  108. this->addAll(inValue);
  109. }
  110. inline void removeAt(size_t inIndex) {
  111. assert(inIndex < _size);
  112. --_size;
  113. if (inIndex != _size) {
  114. for (size_t i = inIndex; i < _size; ++i) {
  115. T tmp(_buffer[i]);
  116. _buffer[i] = _buffer[i + 1];
  117. _buffer[i + 1] = tmp;
  118. }
  119. }
  120. destroy(_buffer + _size);
  121. }
  122. inline bool contains(const T &inValue) {
  123. for (size_t i = 0; i < _size; ++i) {
  124. if (_buffer[i] == inValue) {
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. inline int indexOf(const T &inValue) {
  131. for (size_t i = 0; i < _size; ++i) {
  132. if (_buffer[i] == inValue) {
  133. return (int)i;
  134. }
  135. }
  136. return -1;
  137. }
  138. inline T &operator[](size_t inIndex) {
  139. assert(inIndex < _size);
  140. return _buffer[inIndex];
  141. }
  142. inline friend bool operator==(Vector<T> &lhs, Vector<T> &rhs) {
  143. if (lhs.size() != rhs.size()) {
  144. return false;
  145. }
  146. for (size_t i = 0, n = lhs.size(); i < n; ++i) {
  147. if (lhs[i] != rhs[i]) {
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. inline friend bool operator!=(Vector<T> &lhs, Vector<T> &rhs) {
  154. return !(lhs == rhs);
  155. }
  156. inline T *buffer() {
  157. return _buffer;
  158. }
  159. private:
  160. size_t _size;
  161. size_t _capacity;
  162. T *_buffer;
  163. inline T *allocate(size_t n) {
  164. assert(n > 0);
  165. T *ptr = SpineExtension::calloc<T>(n, __FILE__, __LINE__);
  166. assert(ptr);
  167. return ptr;
  168. }
  169. inline void deallocate(T *buffer) {
  170. if (_buffer) {
  171. SpineExtension::free(buffer, __FILE__, __LINE__);
  172. }
  173. }
  174. inline void construct(T *buffer, const T &val) {
  175. new(buffer) T(val);
  176. }
  177. inline void destroy(T *buffer) {
  178. buffer->~T();
  179. }
  180. // Vector &operator=(const Vector &inVector) {};
  181. };
  182. }
  183. #endif /* Spine_Vector_h */