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 void moveTo(BigDynArray& to);
65
66 struct iterator {
67 int pos;
68 BigDynArray* array;
69
70 iterator() {}
71 iterator(int v, BigDynArray* b) : pos(v), array(b) {}
72 void operator ++() {
73 pos++;
74 }
75 bool operator != (const iterator& itr) const {
76 return pos != itr.pos;
77 }
78 Elem& operator*() {
79 return (*array)[pos];
80 }
81 Elem* operator->() {
82 return &(*array)[pos];
83 }
84 };
85 iterator begin() {
86 return iterator(0, this);
87 }
88 iterator end() {
89 return iterator(NValues, this);
90 }
91 void operator = (const BigDynArray& b);
92 bool operator == (const BigDynArray& b) const;
93};
94
95template <class Elem, int CellSize>
97 if (b.Count()) {
98 if (std::is_trivially_copy_constructible<Elem>()) {
99 FastCopyFrom(b);
100 }
101 else {
102 EnsureCapacity(b.Count());
103 NValues = b.Count();
104 for (int i = 0; i < b.Count(); i++) {
105 (*this)[i] = b[i];
106 }
107 }
108 }
109}
110
111template <class Elem, int CellSize>
113 if (NValues != b.Count())return false;
114 for(int i=0;i<NValues;i++) {
115 if ((*this)[i] != b[i])return false;
116 }
117 return true;
118}
119
120template <class Elem, int CellSize>
122 NValues = MaxCells = MaxValues = 0;
123 Values = nullptr;
124 operator=(b);
125}
126
127template <class Elem, int CellSize>
129 if(N>=MaxValues){
130 int n=1+(N/CellSize);
131 int n0=MaxCells;
132 if(n>=MaxCells){
133 MaxCells=std::max(MaxCells+512,n+1);
134 Elem** np=(Elem**)_ExMalloc(MaxCells*sizeof(Elem*));
135 memset(np,0,MaxCells*sizeof(Elem*));
136 if(Values){
137 memcpy(np,Values,n0*sizeof(Elem*));
138 _ExFree(Values);
139 }
140 Values=np;
141 }
142 for(int i=MaxValues/CellSize;i<n;i++){
143 Values[i]=(Elem*)_ExMalloc(CellSize*sizeof(Elem));
144 }
145 MaxValues=n*CellSize;
146 }
147}
148
149template <class Elem, int CellSize>
151 Values=NULL;
152 NValues=0;
153 MaxValues=0;
154 MaxCells=0;
155}
156
157template <class Elem, int CellSize>
158BigDynArray<Elem, CellSize>::BigDynArray(int Size, Elem Filling) {
159 Values=NULL;
160 NValues=0;
161 MaxValues=0;
162 MaxCells=0;
163 Add(Filling,Size);
164}
165
166template <class Elem, int CellSize>
168 Clear();
169}
170
171template <class Elem, int CellSize>
172int BigDynArray<Elem, CellSize>::Add(const Elem& V) {
173 EnsureCapacity(NValues+1);
174 int p1=NValues/CellSize;
175 Values[p1][NValues%CellSize]=V;
176 NValues++;
177 return NValues-1;
178}
179
180template <class Elem, int CellSize>
181int BigDynArray<Elem, CellSize>::Add(const Elem& V, int NTimes) {
182 int r=NValues-1;
183 for(int i=0;i<NTimes;i++){
184 r=Add(V);
185 }
186 return r;
187}
188
189template <class Elem, int CellSize>
190template <class array>
192 int N = a.Count();
193 for (int i = 0; i < N; i++) {
194 Add(a[i]);
195 }
196}
197
198template <class Elem, int CellSize>
200 _strict_check(pos >= 0 && pos < NValues);
201 return Values[pos/CellSize][pos%CellSize];
202}
203
204template <class Elem, int CellSize>
205const Elem& BigDynArray<Elem, CellSize>::operator[](int pos) const {
206 _strict_check(pos >= 0 && pos < NValues);
207 return Values[pos/CellSize][pos%CellSize];
208}
209
210template <class Elem, int CellSize>
212 return NValues;
213}
214
215template <class Elem, int CellSize>
217 if(Values){
218 if (std::is_compound<Elem>()) {
219 for (int i = 0; i < NValues; i++) {
220 (*this)[i].~Elem();
221 }
222 }
223 for(int i=0;i<MaxCells;i++){
224 if(Values[i])_ExFree(Values[i]);
225 }
226 _ExFree(Values);
227 }
228 Values=NULL;
229 NValues=0;
230 MaxValues=0;
231 MaxCells=0;
232}
233
234template <class Elem, int CellSize>
236 NValues=0;
237}
238
239template <class Elem, int CellSize>
240void BigDynArray<Elem, CellSize>::Del(int pos, int num) {
241 if(pos<0) {
242 num += pos;
243 pos = 0;
244 }
245 if (pos + num > NValues) {
246 num = NValues - pos;
247 }
248 if (num <= 0)return;
249 for (int p = pos + num; p < NValues; p++) {
250 (*this)[p - num] = (*this)[p];
251 }
252 NValues -= num;
253}
254
255template <class Elem, int CellSize>
256void BigDynArray<Elem, CellSize>::uSet(int pos, const Elem& e, const Elem& Default) {
257 if (pos >= Count())Add(Default, pos - Count() + 1);
258 (*this)[pos] = e;
259}
260
261template <class Elem, int CellSize>
262Elem BigDynArray<Elem, CellSize>::uGet(int pos, const Elem& Default) {
263 if (pos < 0 || pos >= Count())return Default;
264 return (*this)[pos];
265}
266
267template <class Elem, int CellSize>
268Elem& BigDynArray<Elem, CellSize>::ExpandTo(int pos, Elem def) {
269 if ( pos >= Count() )Add( def, pos - Count() + 1 );
270 return (*this)[ pos ];
271}
272
273template <class Elem, int CellSize>
275 return NValues == 0;
276}
277
278template <class Elem, int CellSize>
279void BigDynArray<Elem, CellSize>::RemoveAt(const int Index, const int Count) {
280 Del(Index, Count);
281}
282
283template <class Elem, int CellSize>
284void BigDynArray<Elem, CellSize>::Insert(const int Index, const Elem& Value, const int Count) {
285 EnsureCapacity(NValues + Count);
286 int start = NValues - 1;
287 NValues += Count;
288 for (int k = start; k >= Index; k--) {
289 (*this)[k + Count] = (*this)[k];
290 }
291 for(int k=0;k<Count;k++) {
292 (*this)[k + Index] = Value;
293 }
294}
295
296template <class Elem, int CellSize>
297template <class array>
298void BigDynArray<Elem, CellSize>::Copy(const array& Src) {
299 int ns = Src.Count();
300 EnsureCapacity(ns);
301 NValues = ns;
302 for (int i = 0; i < ns; i++) {
303 (*this)[i] = Src[i];
304 }
305}
306
307template <class Elem, int CellSize>
308template <class array>
309void BigDynArray<Elem, CellSize>::AddRange(const array& Src) {
310 int ns = Src.Count();
311 EnsureCapacity(ns + Count());
312 for(int k=0;k<ns;k++) {
313 (*this)[NValues++] = Src[k];
314 }
315}
316
317template <class Elem, int CellSize>
318void BigDynArray<Elem, CellSize>::SetCount(const int Count) {
319 EnsureCapacity(Count);
320 NValues = Count;
321}
322
323template <class Elem, int CellSize>
324void BigDynArray<Elem, CellSize>::SetCount(int Count, const Elem& Value) {
325 EnsureCapacity(Count);
326 NValues = Count;
327 for(int i=0;i<Count;i++) {
328 (*this)[i] = Value;
329 }
330}
331
332template <class Elem, int CellSize>
333int BigDynArray<Elem, CellSize>::IndexOf(const Elem& e) {
334 for (int i = 0; i < NValues; i++)if (e == (*this)[i])return i;
335 return -1;
336}
337
338template <class Elem, int CellSize>
340 if (NValues)NValues--;
341}
342
343template <class Elem, int CellSize>
344const Elem& BigDynArray<Elem, CellSize>::GetLast() const {
345 _strict_check(NValues > 0);
346 return (*this)[NValues - 1];
347}
348
349template <class Elem, int CellSize>
351 return (*this)[NValues - 1];
352}
353
354template <class Elem, int CellSize>
356 EnsureCapacity(src.Count());
357 NValues = src.Count();
358 for (int k = 0, p = 0; k < NValues; k += CellSize, p++) {
359 int nel = std::min(CellSize, NValues - k);
360 memcpy(Values[p], src.Values[p], nel * sizeof(Elem));
361 }
362}
363
364template <class Elem, int CellSize>
366 for (int k = 0, p = 0; k < NValues; k += CellSize, p++) {
367 int nel = std::min(CellSize, NValues - k);
368 memset(Values[p], 0, nel * sizeof(Elem));
369 }
370}
371
372template <class Elem, int CellSize>
374 for(int i=0,j=NValues-1;i<j;i++,j--) {
375 std::swap((*this)[i], (*this)[j]);
376 }
377}
378
379template <class Elem, int CellSize>
380template <class Reader>
381void BigDynArray<Elem, CellSize>::FromBS(Reader& BS, int count) {
382 Clear();
383 if (count) {
384 SetCount(count);
385 for (int i = 0;;) {
386 int sz = Count() - i;
387 if (sz > CellSize)sz = CellSize;
388 if (sz > 0)BS.Read(Values[i/CellSize], sz * sizeof(Elem));
389 else break;
390 i += CellSize;
391 }
392 }
393}
394
395template <class Elem, int CellSize>
396template <class Writer>
397void BigDynArray<Elem, CellSize>::ToBS(Writer& BS) {
398 for (int i = 0;;) {
399 int sz = Count() - i;
400 if (sz > CellSize)sz = CellSize;
401 if (sz > 0)BS.Write(Values[i/CellSize], sz * sizeof(Elem));
402 else break;
403 i += CellSize;
404 }
405}
406
407template <class Elem, int CellSize>
409 to.Clear();
410 to.NValues = NValues;
411 to.Values = Values;
412 to.MaxValues = MaxValues;
413 to.MaxCells = MaxCells;
414 NValues = MaxCells = MaxValues = 0;
415 Values = nullptr;
416}
Definition bigdynarray.h:17
Definition bigdynarray.h:66