3DCoat Core API
The 3DCoat API documentation.
Loading...
Searching...
No Matches
bigdynarray.h
1#pragma once
2
3APICALL void* _ExMalloc(int Size);
4APICALL void _ExFree(void* Ptr);
5#ifdef _DEBUG
6#define STRICT_CHECK
7#endif
8void _error_box(const char* text);
9#ifdef STRICT_CHECK
10#define _strict_check(x) if(!(x))_error_box(#x)
11#else
12#define _strict_check(x)
13#endif
14
15template <class Elem,int CellSize=1024>
17{
18 Elem** Values;
19 int NValues;
20 int MaxValues;
21 int MaxCells;
22public:
23
24 BigDynArray(const BigDynArray& b);
25 inline void EnsureCapacity(int N);
26 inline BigDynArray();
27 inline BigDynArray(int Size, Elem Filling);
28 inline ~BigDynArray();
29 inline int Add(const Elem& V);
30 inline int Add(const Elem& V,int NTimes);
31 template <class array>
32 inline void AddRange(array& a);
33 inline Elem& operator [](int pos);
34 inline const Elem& operator [](int pos) const;
35 inline int Count()const;
36 inline void Clear();
37 inline void FastClear();
38 inline void Del(int pos,int num);
39 inline void uSet(int pos, const Elem& e, const Elem& Default);
40 inline Elem uGet(int pos, const Elem& Default);;
41
42 Elem& ExpandTo(int pos,Elem def);
43 bool IsEmpty() const;
44
45 void RemoveAt(const int Index, const int Count = 1);
46 void Insert(const int Index, const Elem& Value, const int Count = 1);
47 template <class array>
48 void Copy(const array& Src);
49 template <class array>
50 void AddRange(const array& Src);
51 void SetCount(const int Count); // Sets the number of elements actually contained in the "cList"
52 void SetCount(int Count, const Elem& Value);
53 int IndexOf(const Elem& e);
54 void RemoveLast();
55 const Elem& GetLast() const;
56 Elem& GetLast();
57 void FastCopyFrom(const BigDynArray& src);
58 void FastZeroMem();
59 void Reverse();
60 template <class Reader>
61 void FromBS(Reader& BS, int count);
62 template <class Writer>
63 void ToBS(Writer& BS);
64
65 struct iterator {
66 int pos;
67 BigDynArray* array;
68
69 iterator() {}
70 iterator(int v, BigDynArray* b) : pos(v), array(b) {}
71 void operator ++() {
72 pos++;
73 }
74 bool operator != (const iterator& itr) const {
75 return pos != itr.pos;
76 }
77 Elem& operator*() {
78 return (*array)[pos];
79 }
80 Elem* operator->() {
81 return &(*array)[pos];
82 }
83 };
84 iterator begin() {
85 return iterator(0, this);
86 }
87 iterator end() {
88 return iterator(NValues, this);
89 }
90 void operator = (const BigDynArray& b);
91 bool operator == (const BigDynArray& b) const;
92};
93
94template <class Elem, int CellSize>
96 if (b.Count()) {
97 if (std::is_trivially_copy_constructible<Elem>()) {
98 FastCopyFrom(b);
99 }
100 else {
101 EnsureCapacity(b.Count());
102 NValues = b.Count();
103 for (int i = 0; i < b.Count(); i++) {
104 (*this)[i] = b[i];
105 }
106 }
107 }
108}
109
110template <class Elem, int CellSize>
112 if (NValues != b.Count())return false;
113 for(int i=0;i<NValues;i++) {
114 if ((*this)[i] != b[i])return false;
115 }
116 return true;
117}
118
119template <class Elem, int CellSize>
121 NValues = MaxCells = MaxValues = 0;
122 Values = nullptr;
123 operator=(b);
124}
125
126template <class Elem, int CellSize>
128 if(N>=MaxValues){
129 int n=1+(N/CellSize);
130 int n0=MaxCells;
131 if(n>=MaxCells){
132 MaxCells=std::max(MaxCells+512,n+1);
133 Elem** np=(Elem**)_ExMalloc(MaxCells*sizeof(Elem*));
134 memset(np,0,MaxCells*sizeof(Elem*));
135 if(Values){
136 memcpy(np,Values,n0*sizeof(Elem*));
137 _ExFree(Values);
138 }
139 Values=np;
140 }
141 for(int i=MaxValues/CellSize;i<n;i++){
142 Values[i]=(Elem*)_ExMalloc(CellSize*sizeof(Elem));
143 }
144 MaxValues=n*CellSize;
145 }
146}
147
148template <class Elem, int CellSize>
150 Values=NULL;
151 NValues=0;
152 MaxValues=0;
153 MaxCells=0;
154}
155
156template <class Elem, int CellSize>
157BigDynArray<Elem, CellSize>::BigDynArray(int Size, Elem Filling) {
158 Values=NULL;
159 NValues=0;
160 MaxValues=0;
161 MaxCells=0;
162 Add(Filling,Size);
163}
164
165template <class Elem, int CellSize>
167 Clear();
168}
169
170template <class Elem, int CellSize>
171int BigDynArray<Elem, CellSize>::Add(const Elem& V) {
172 EnsureCapacity(NValues+1);
173 int p1=NValues/CellSize;
174 Values[p1][NValues%CellSize]=V;
175 NValues++;
176 return NValues-1;
177}
178
179template <class Elem, int CellSize>
180int BigDynArray<Elem, CellSize>::Add(const Elem& V, int NTimes) {
181 int r=NValues-1;
182 for(int i=0;i<NTimes;i++){
183 r=Add(V);
184 }
185 return r;
186}
187
188template <class Elem, int CellSize>
189template <class array>
191 int N = a.Count();
192 for (int i = 0; i < N; i++) {
193 Add(a[i]);
194 }
195}
196
197template <class Elem, int CellSize>
199 _strict_check(pos >= 0 && pos < NValues);
200 return Values[pos/CellSize][pos%CellSize];
201}
202
203template <class Elem, int CellSize>
204const Elem& BigDynArray<Elem, CellSize>::operator[](int pos) const {
205 _strict_check(pos >= 0 && pos < NValues);
206 return Values[pos/CellSize][pos%CellSize];
207}
208
209template <class Elem, int CellSize>
211 return NValues;
212}
213
214template <class Elem, int CellSize>
216 if(Values){
217 if (std::is_compound<Elem>()) {
218 for (int i = 0; i < NValues; i++) {
219 (*this)[i].~Elem();
220 }
221 }
222 for(int i=0;i<MaxCells;i++){
223 if(Values[i])_ExFree(Values[i]);
224 }
225 _ExFree(Values);
226 }
227 Values=NULL;
228 NValues=0;
229 MaxValues=0;
230 MaxCells=0;
231}
232
233template <class Elem, int CellSize>
235 NValues=0;
236}
237
238template <class Elem, int CellSize>
239void BigDynArray<Elem, CellSize>::Del(int pos, int num) {
240 if(pos<0) {
241 num += pos;
242 pos = 0;
243 }
244 if (pos + num > NValues) {
245 num = NValues - pos;
246 }
247 if (num <= 0)return;
248 for (int p = pos + num; p < NValues; p++) {
249 (*this)[p - num] = (*this)[p];
250 }
251 NValues -= num;
252}
253
254template <class Elem, int CellSize>
255void BigDynArray<Elem, CellSize>::uSet(int pos, const Elem& e, const Elem& Default) {
256 if (pos >= Count())Add(Default, pos - Count() + 1);
257 (*this)[pos] = e;
258}
259
260template <class Elem, int CellSize>
261Elem BigDynArray<Elem, CellSize>::uGet(int pos, const Elem& Default) {
262 if (pos < 0 || pos >= Count())return Default;
263 return (*this)[pos];
264}
265
266template <class Elem, int CellSize>
267Elem& BigDynArray<Elem, CellSize>::ExpandTo(int pos, Elem def) {
268 if ( pos >= Count() )Add( def, pos - Count() + 1 );
269 return (*this)[ pos ];
270}
271
272template <class Elem, int CellSize>
274 return NValues == 0;
275}
276
277template <class Elem, int CellSize>
278void BigDynArray<Elem, CellSize>::RemoveAt(const int Index, const int Count) {
279 Del(Index, Count);
280}
281
282template <class Elem, int CellSize>
283void BigDynArray<Elem, CellSize>::Insert(const int Index, const Elem& Value, const int Count) {
284 EnsureCapacity(NValues + Count);
285 int start = NValues - 1;
286 NValues += Count;
287 for (int k = start; k >= Index; k--) {
288 (*this)[k + Count] = (*this)[k];
289 }
290 for(int k=0;k<Count;k++) {
291 (*this)[k + Index] = Value;
292 }
293}
294
295template <class Elem, int CellSize>
296template <class array>
297void BigDynArray<Elem, CellSize>::Copy(const array& Src) {
298 int ns = Src.Count();
299 EnsureCapacity(ns);
300 NValues = ns;
301 for (int i = 0; i < ns; i++) {
302 (*this)[i] = Src[i];
303 }
304}
305
306template <class Elem, int CellSize>
307template <class array>
308void BigDynArray<Elem, CellSize>::AddRange(const array& Src) {
309 int ns = Src.Count();
310 EnsureCapacity(ns + Count());
311 for(int k=0;k<ns;k++) {
312 (*this)[NValues++] = Src[k];
313 }
314}
315
316template <class Elem, int CellSize>
317void BigDynArray<Elem, CellSize>::SetCount(const int Count) {
318 EnsureCapacity(Count);
319 NValues = Count;
320}
321
322template <class Elem, int CellSize>
323void BigDynArray<Elem, CellSize>::SetCount(int Count, const Elem& Value) {
324 EnsureCapacity(Count);
325 NValues = Count;
326 for(int i=0;i<Count;i++) {
327 (*this)[i] = Value;
328 }
329}
330
331template <class Elem, int CellSize>
332int BigDynArray<Elem, CellSize>::IndexOf(const Elem& e) {
333 for (int i = 0; i < NValues; i++)if (e == (*this)[i])return i;
334 return -1;
335}
336
337template <class Elem, int CellSize>
339 if (NValues)NValues--;
340}
341
342template <class Elem, int CellSize>
343const Elem& BigDynArray<Elem, CellSize>::GetLast() const {
344 _strict_check(NValues > 0);
345 return (*this)[NValues - 1];
346}
347
348template <class Elem, int CellSize>
350 return (*this)[NValues - 1];
351}
352
353template <class Elem, int CellSize>
355 EnsureCapacity(src.Count());
356 NValues = src.Count();
357 for (int k = 0, p = 0; k < NValues; k += CellSize, p++) {
358 int nel = std::min(CellSize, NValues - k);
359 memcpy(Values[p], src.Values[p], nel * sizeof(Elem));
360 }
361}
362
363template <class Elem, int CellSize>
365 for (int k = 0, p = 0; k < NValues; k += CellSize, p++) {
366 int nel = std::min(CellSize, NValues - k);
367 memset(Values[p], 0, nel * sizeof(Elem));
368 }
369}
370
371template <class Elem, int CellSize>
373 for(int i=0,j=NValues-1;i<j;i++,j--) {
374 std::swap((*this)[i], (*this)[j]);
375 }
376}
377
378template <class Elem, int CellSize>
379template <class Reader>
380void BigDynArray<Elem, CellSize>::FromBS(Reader& BS, int count) {
381 Clear();
382 if (count) {
383 SetCount(count);
384 for (int i = 0;;) {
385 int sz = Count() - i;
386 if (sz > CellSize)sz = CellSize;
387 if (sz > 0)BS.Read(Values[i/CellSize], sz * sizeof(Elem));
388 else break;
389 i += CellSize;
390 }
391 }
392}
393
394template <class Elem, int CellSize>
395template <class Writer>
396void BigDynArray<Elem, CellSize>::ToBS(Writer& BS) {
397 for (int i = 0;;) {
398 int sz = Count() - i;
399 if (sz > CellSize)sz = CellSize;
400 if (sz > 0)BS.Write(Values[i/CellSize], sz * sizeof(Elem));
401 else break;
402 i += CellSize;
403 }
404}
Definition bigdynarray.h:17
Definition bigdynarray.h:65