3DCoat Core API
The 3DCoat API documentation.
Loading...
Searching...
No Matches
stackarray.h
1#pragma once
2
3class BinStream;
4//#define SAFE_ARRAY
5template <class Elem,int DefElmCount=16>
7 Elem* Values;
8 Elem* AllocElem;
9 Elem StackValues[DefElmCount];
10 int NValues;
11 int MaxValues;
12public:
13
14 typedef Elem* iterator;
15 iterator begin() {
16 return Values;
17 }
18 iterator end() {
19 return Values + NValues;
20 }
21
22 StackArray(const StackArray& src);
23 void operator = (const StackArray& src);
24 void Check(int N);
25 Elem* operator + (int idx);
26 StackArray();
27 StackArray(int Size, Elem Filling);
29 int Add(const Elem& V);
30 int Add(const Elem& V,int NTimes);
31 void RemoveAt(int pos,int N);
32 int find(const Elem& v);
33 bool Insert(int pos,const Elem& V);
34 bool Move(int from, int to);
35 Elem& operator [](int pos);
36 Elem operator [](int pos) const;
37 void operator += (const StackArray& A);
38 void operator += (Elem V);
39 Elem* ToPtr()const;
40 void AddRange(Elem* Data,int N);
41 int Count()const;
42 void Clear();
43 void FastClear();
44 void Fill(int Value);
45 Elem& GetFirst();
46 Elem& GetLast();
47 Elem pop_back();
48 void CopyTo(StackArray<Elem>& dest);
49 void MoveArrayTo(StackArray<Elem>& dest);
50 void pop_front();
51 bool AddOnce(Elem V);
52 void ToBS(BinStream& BS);
53 void FromBS(BinStream& BS,int ne);
54 void reverse(int start, int finish);
55};
56
57template <class Elem, int DefElmCount>
59 Values = &StackValues[0];
60 NValues = 0;
61 MaxValues = DefElmCount;
62 AllocElem = NULL;
63 operator = (src);
64}
65
66template <class Elem, int DefElmCount>
68 FastClear();
69 Check(src.Count());
70 for(int i=0;i<src.Count();i++) {
71 Add(src[i]);
72 }
73}
74
75template <class Elem, int DefElmCount>
77 if(N>=MaxValues){
78 MaxValues=N+DefElmCount+MaxValues/2;
79 Elem* tmp=(Elem*)_ExMalloc(MaxValues*sizeof(Elem));
80 if(NValues){
81 memcpy(tmp,Values,NValues*sizeof(Elem));
82 }
83 if(AllocElem)_ExFree(AllocElem);
84 Values=AllocElem=tmp;
85 }
86}
87
88template <class Elem, int DefElmCount>
90 assert(idx>=0&&idx<NValues);
91 assert(NValues);
92 return Values+idx;
93}
94
95template <class Elem, int DefElmCount>
97 Values=&StackValues[0];
98 NValues=0;
99 MaxValues=DefElmCount;
100 AllocElem=NULL;
101}
102
103template <class Elem, int DefElmCount>
104StackArray<Elem, DefElmCount>::StackArray(int Size, Elem Filling) {
105 Values=&StackValues[0];
106 NValues=0;
107 MaxValues=DefElmCount;
108 Add(Filling,Size);
109}
110
111template <class Elem, int DefElmCount>
113 Clear();
114}
115
116template <class Elem, int DefElmCount>
117int StackArray<Elem, DefElmCount>::Add(const Elem& V) {
118 Check(NValues+1);
119 Values[NValues]=V;
120 NValues++;
121 return NValues-1;
122}
123
124template <class Elem, int DefElmCount>
125int StackArray<Elem, DefElmCount>::Add(const Elem& V, int NTimes) {
126 Check(NValues+NTimes);
127 for(int i=0;i<NTimes;i++){
128 memcpy(&Values[NValues],&V,sizeof(Elem));
129 NValues++;
130 }
131 return NValues-1;
132}
133
134template <class Elem, int DefElmCount>
135void StackArray<Elem, DefElmCount>::RemoveAt(int pos, int N) {
136 if(pos<0){
137 N+=pos;
138 pos=0;
139 }
140 if(N<=0||pos>=NValues)return;
141 if(pos+N>NValues)N=NValues-pos;
142 if(pos+N<NValues)memcpy(Values+pos,Values+pos+N,(NValues-pos-N)*sizeof(Elem));
143 NValues-=N;
144}
145
146template <class Elem, int DefElmCount>
147int StackArray<Elem, DefElmCount>::find(const Elem& v) {
148 for(int i=0;i<NValues;i++)if(v==Values[i])return i;
149 return -1;
150}
151
152template <class Elem, int DefElmCount>
153bool StackArray<Elem, DefElmCount>::Insert(int pos, const Elem& V) {
154 if(pos<0||pos>NValues)return false;
155 Check(NValues+1);
156 if(pos<NValues)memmove(Values+pos+1,Values+pos,(NValues-pos)*sizeof(Elem));
157 Values[pos]=V;
158 NValues++;
159 return true;
160}
161
162template <class Elem, int DefElmCount>
163bool StackArray<Elem, DefElmCount>::Move(int from, int to) {
164 if(from>=NValues) from=NValues-1; else if(from<0) from=0;
165 if(to>=NValues) to=NValues-1; else if(to<0) to=0;
166 if(from==to) return false;
167 Elem t=Values[from];
168 if(from<to){
169 memcpy(Values+from,Values+from+1,(to-from)*sizeof(Elem));
170 }else{
171 memmove(Values+to+1,Values+to,(from-to)*sizeof(Elem));
172 }
173 Values[to]=t;
174 return true;
175}
176
177template <class Elem, int DefElmCount>
179 assert(pos>=0&&pos<NValues);
180#ifdef SAFE_ARRAY
181 if(pos<0||pos>=NValues)return Values[0];//DlaBitja
182#endif //SAFE_ARRAY
183 return Values[pos];
184}
185
186template <class Elem, int DefElmCount>
188 assert(pos>=0&&pos<NValues);
189#ifdef SAFE_ARRAY
190 if(pos<0||pos>=NValues)return Values[pos];//DlaBitja;
191#endif //SAFE_ARRAY
192 return Values[pos];
193}
194
195template <class Elem, int DefElmCount>
197 AddRange(A.Values,A.NValues);
198}
199
200template <class Elem, int DefElmCount>
202 Add(V);
203}
204
205template <class Elem, int DefElmCount>
206Elem* StackArray<Elem, DefElmCount>::ToPtr() const {//use only to save data!!!
207 return Values;
208}
209
210template <class Elem, int DefElmCount>
211void StackArray<Elem, DefElmCount>::AddRange(Elem* Data, int N) {
212 Check(NValues+N);
213 memcpy(Values+NValues,Data,N*sizeof(Elem));
214 NValues+=N;
215}
216
217template <class Elem, int DefElmCount>
219 return NValues;
220}
221
222template <class Elem, int DefElmCount>
224 if(AllocElem)_ExFree(AllocElem);
225 Values=&StackValues[0];
226 NValues=0;
227 MaxValues=DefElmCount;
228 AllocElem=NULL;
229}
230
231template <class Elem, int DefElmCount>
233 NValues=0;
234}
235
236template <class Elem, int DefElmCount>
238 memset(Values,Value,NValues*sizeof(Elem));
239}
240
241template <class Elem, int DefElmCount>
243 assert(NValues!=0);
244#ifdef SAFE_ARRAY
245 if(NValues==0)return Values[0];//DlaBitja;
246#endif
247 return Values[0];
248}
249
250template <class Elem, int DefElmCount>
252 assert(NValues!=0);
253 if(NValues==0){
254#ifdef SAFE_ARRAY
255 return Values[0];//DlaBitja;
256#endif
257 }
258 return Values[NValues-1];
259}
260
261template <class Elem, int DefElmCount>
263 if(NValues>0){
264 return Values[--NValues];
265 }else return Values[0];
266}
267
268template <class Elem, int DefElmCount>
270 dest.Check(NValues);
271 memcpy(dest.Values,Values,NValues*sizeof(Elem));
272 dest.NValues=NValues;
273}
274
275template <class Elem, int DefElmCount>
277 dest.Clear();
278 dest.Values=Values;
279 dest.NValues=NValues;
280 Values=NULL;
281 NValues=0;
282}
283
284template <class Elem, int DefElmCount>
286 RemoveAt(0,1);
287}
288
289template <class Elem, int DefElmCount>
291 if(find(V)==-1){
292 Add(V);
293 return true;
294 }
295 return false;
296}
297
298template <class Elem, int DefElmCount>
299void StackArray<Elem, DefElmCount>::reverse(int start, int finish) {
300 if (start >= 0 && start < NValues && finish >= 0 && finish <= NValues) {
301 if (start > finish)std::swap(start, finish);
302 for (int i = start, j = finish; i < j; i++, j--) {
303 std::swap(Values[i], Values[j]);
304 }
305 }
306}
307
Definition stackarray.h:6