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