OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 #ifndef XFA_SRC_FGAS_CRT_FGAS_UTILS_H_ | |
8 #define XFA_SRC_FGAS_CRT_FGAS_UTILS_H_ | |
9 | |
10 #include "core/include/fxcrt/fx_coordinates.h" | |
11 #include "xfa/src/fgas/crt/fgas_memory.h" | |
12 | |
13 class FX_BASEARRAYDATA; | |
14 | |
15 template <class baseType> | |
16 class CFX_CPLTree; | |
17 | |
18 class CFX_BaseArray : public CFX_Target { | |
19 protected: | |
20 CFX_BaseArray(int32_t iGrowSize, int32_t iBlockSize); | |
21 ~CFX_BaseArray(); | |
22 int32_t GetSize() const; | |
23 int32_t GetBlockSize() const; | |
24 uint8_t* AddSpaceTo(int32_t index); | |
25 uint8_t* GetAt(int32_t index) const; | |
26 uint8_t* GetBuffer() const; | |
27 int32_t Append(const CFX_BaseArray& src, | |
28 int32_t iStart = 0, | |
29 int32_t iCount = -1); | |
30 int32_t Copy(const CFX_BaseArray& src, | |
31 int32_t iStart = 0, | |
32 int32_t iCount = -1); | |
33 int32_t RemoveLast(int32_t iCount = -1); | |
34 void RemoveAll(FX_BOOL bLeaveMemory = FALSE); | |
35 | |
36 FX_BASEARRAYDATA* m_pData; | |
37 }; | |
38 | |
39 template <class baseType> | |
40 class CFX_BaseArrayTemplate : public CFX_BaseArray { | |
41 public: | |
42 CFX_BaseArrayTemplate(int32_t iGrowSize = 100) | |
43 : CFX_BaseArray(iGrowSize, sizeof(baseType)) {} | |
44 CFX_BaseArrayTemplate(int32_t iGrowSize, int32_t iBlockSize) | |
45 : CFX_BaseArray(iGrowSize, iBlockSize) {} | |
46 int32_t GetSize() const { return CFX_BaseArray::GetSize(); } | |
47 int32_t GetBlockSize() const { return CFX_BaseArray::GetBlockSize(); } | |
48 baseType* AddSpace() { | |
49 return (baseType*)CFX_BaseArray::AddSpaceTo(CFX_BaseArray::GetSize()); | |
50 } | |
51 int32_t Add(const baseType& element) { | |
52 int32_t index = CFX_BaseArray::GetSize(); | |
53 *(baseType*)CFX_BaseArray::AddSpaceTo(index) = element; | |
54 return index; | |
55 } | |
56 baseType* GetBuffer() const { return (baseType*)CFX_BaseArray::GetBuffer(); } | |
57 baseType& GetAt(int32_t index) const { | |
58 return *(baseType*)CFX_BaseArray::GetAt(index); | |
59 } | |
60 baseType* GetPtrAt(int32_t index) const { | |
61 return (baseType*)CFX_BaseArray::GetAt(index); | |
62 } | |
63 void SetAt(int32_t index, const baseType& element) { | |
64 *(baseType*)CFX_BaseArray::GetAt(index) = element; | |
65 } | |
66 void SetAtGrow(int32_t index, const baseType& element) { | |
67 *(baseType*)CFX_BaseArray::AddSpaceTo(index) = element; | |
68 } | |
69 int32_t Append(const CFX_BaseArrayTemplate& src, | |
70 int32_t iStart = 0, | |
71 int32_t iCount = -1) { | |
72 return CFX_BaseArray::Append(src, iStart, iCount); | |
73 } | |
74 int32_t Copy(const CFX_BaseArrayTemplate& src, | |
75 int32_t iStart = 0, | |
76 int32_t iCount = -1) { | |
77 return CFX_BaseArray::Copy(src, iStart, iCount); | |
78 } | |
79 int32_t RemoveLast(int32_t iCount = -1) { | |
80 return CFX_BaseArray::RemoveLast(iCount); | |
81 } | |
82 void RemoveAll(FX_BOOL bLeaveMemory = FALSE) { | |
83 CFX_BaseArray::RemoveAll(bLeaveMemory); | |
84 } | |
85 }; | |
86 typedef CFX_BaseArrayTemplate<void*> CFDE_PtrArray; | |
87 typedef CFX_BaseArrayTemplate<FX_DWORD> CFDE_DWordArray; | |
88 typedef CFX_BaseArrayTemplate<FX_WORD> CFDE_WordArray; | |
89 | |
90 template <class baseType> | |
91 class CFX_ObjectBaseArrayTemplate : public CFX_BaseArray { | |
92 public: | |
93 CFX_ObjectBaseArrayTemplate(int32_t iGrowSize = 100) | |
94 : CFX_BaseArray(iGrowSize, sizeof(baseType)) {} | |
95 ~CFX_ObjectBaseArrayTemplate() { RemoveAll(FALSE); } | |
96 int32_t GetSize() const { return CFX_BaseArray::GetSize(); } | |
97 int32_t GetBlockSize() const { return CFX_BaseArray::GetBlockSize(); } | |
98 int32_t Add(const baseType& element) { | |
99 int32_t index = CFX_BaseArray::GetSize(); | |
100 baseType* p = (baseType*)CFX_BaseArray::AddSpaceTo(index); | |
101 new ((void*)p) baseType(element); | |
102 return index; | |
103 } | |
104 baseType& GetAt(int32_t index) const { | |
105 return *(baseType*)CFX_BaseArray::GetAt(index); | |
106 } | |
107 baseType* GetPtrAt(int32_t index) const { | |
108 return (baseType*)CFX_BaseArray::GetAt(index); | |
109 } | |
110 int32_t Append(const CFX_ObjectBaseArrayTemplate& src, | |
111 int32_t iStart = 0, | |
112 int32_t iCount = -1) { | |
113 FXSYS_assert(GetBlockSize() == src.GetBlockSize()); | |
114 if (iCount == 0) { | |
115 return 0; | |
116 } | |
117 int32_t iSize = src.GetSize(); | |
118 FXSYS_assert(iStart > -1 && iStart < iSize); | |
119 if (iCount < 0) { | |
120 iCount = iSize; | |
121 } | |
122 if (iStart + iCount > iSize) { | |
123 iCount = iSize - iStart; | |
124 } | |
125 if (iCount < 1) { | |
126 return 0; | |
127 } | |
128 iSize = CFX_BaseArray::GetSize(); | |
129 CFX_BaseArray::AddSpaceTo(iSize + iCount - 1); | |
130 uint8_t** pStart = CFX_BaseArray::GetAt(iSize); | |
131 int32_t iBlockSize = CFX_BaseArray::GetBlockSize(); | |
132 iSize = iStart + iCount; | |
133 for (int32_t i = iStart; i < iSize; i++) { | |
134 FXTARGET_NewWith((void*)pStart) baseType(src.GetAt(i)); | |
135 pStart += iBlockSize; | |
136 } | |
137 return iCount; | |
138 } | |
139 int32_t Copy(const CFX_ObjectBaseArrayTemplate& src, | |
140 int32_t iStart = 0, | |
141 int32_t iCount = -1) { | |
142 FXSYS_assert(GetBlockSize() == src.GetBlockSize()); | |
143 if (iCount == 0) { | |
144 return 0; | |
145 } | |
146 int32_t iSize = src.GetSize(); | |
147 FXSYS_assert(iStart > -1 && iStart < iSize); | |
148 if (iCount < 0) { | |
149 iCount = iSize; | |
150 } | |
151 if (iStart + iCount > iSize) { | |
152 iCount = iSize - iStart; | |
153 } | |
154 if (iCount < 1) { | |
155 return 0; | |
156 } | |
157 RemoveAll(TRUE); | |
158 CFX_BaseArray::AddSpaceTo(iCount - 1); | |
159 uint8_t** pStart = CFX_BaseArray::GetAt(0); | |
160 int32_t iBlockSize = CFX_BaseArray::GetBlockSize(); | |
161 iSize = iStart + iCount; | |
162 for (int32_t i = iStart; i < iSize; i++) { | |
163 new ((void*)pStart) baseType(src.GetAt(i)); | |
164 pStart += iBlockSize; | |
165 } | |
166 return iCount; | |
167 } | |
168 int32_t RemoveLast(int32_t iCount = -1) { | |
169 int32_t iSize = CFX_BaseArray::GetSize(); | |
170 if (iCount < 0 || iCount > iSize) { | |
171 iCount = iSize; | |
172 } | |
173 if (iCount == 0) { | |
174 return iSize; | |
175 } | |
176 for (int32_t i = iSize - iCount; i < iSize; i++) { | |
177 ((baseType*)GetPtrAt(i))->~baseType(); | |
178 } | |
179 return CFX_BaseArray::RemoveLast(iCount); | |
180 } | |
181 void RemoveAll(FX_BOOL bLeaveMemory = FALSE) { | |
182 int32_t iSize = CFX_BaseArray::GetSize(); | |
183 for (int32_t i = 0; i < iSize; i++) { | |
184 ((baseType*)GetPtrAt(i))->~baseType(); | |
185 } | |
186 CFX_BaseArray::RemoveAll(bLeaveMemory); | |
187 } | |
188 }; | |
189 | |
190 class CFX_BaseMassArrayImp : public CFX_Target { | |
191 public: | |
192 CFX_BaseMassArrayImp(int32_t iChunkSize, int32_t iBlockSize); | |
193 ~CFX_BaseMassArrayImp(); | |
194 uint8_t* AddSpace() { return AddSpaceTo(m_iBlockCount); } | |
195 uint8_t* AddSpaceTo(int32_t index); | |
196 uint8_t* GetAt(int32_t index) const; | |
197 int32_t Append(const CFX_BaseMassArrayImp& src, | |
198 int32_t iStart = 0, | |
199 int32_t iCount = -1); | |
200 int32_t Copy(const CFX_BaseMassArrayImp& src, | |
201 int32_t iStart = 0, | |
202 int32_t iCount = -1); | |
203 int32_t RemoveLast(int32_t iCount = -1); | |
204 void RemoveAll(FX_BOOL bLeaveMemory = FALSE); | |
205 int32_t m_iChunkSize; | |
206 int32_t m_iBlockSize; | |
207 int32_t m_iChunkCount; | |
208 int32_t m_iBlockCount; | |
209 CFX_PtrArray* m_pData; | |
210 | |
211 protected: | |
212 void Append(int32_t iDstStart, | |
213 const CFX_BaseMassArrayImp& src, | |
214 int32_t iSrcStart = 0, | |
215 int32_t iSrcCount = -1); | |
216 }; | |
217 | |
218 class CFX_BaseMassArray : public CFX_Target { | |
219 protected: | |
220 CFX_BaseMassArray(int32_t iChunkSize, int32_t iBlockSize); | |
221 ~CFX_BaseMassArray(); | |
222 int32_t GetSize() const; | |
223 uint8_t* AddSpaceTo(int32_t index); | |
224 uint8_t* GetAt(int32_t index) const; | |
225 int32_t Append(const CFX_BaseMassArray& src, | |
226 int32_t iStart = 0, | |
227 int32_t iCount = -1); | |
228 int32_t Copy(const CFX_BaseMassArray& src, | |
229 int32_t iStart = 0, | |
230 int32_t iCount = -1); | |
231 int32_t RemoveLast(int32_t iCount = -1); | |
232 void RemoveAll(FX_BOOL bLeaveMemory = FALSE); | |
233 CFX_BaseMassArrayImp* m_pData; | |
234 }; | |
235 | |
236 template <class baseType> | |
237 class CFX_MassArrayTemplate : public CFX_BaseMassArray { | |
238 public: | |
239 CFX_MassArrayTemplate(int32_t iChunkSize = 100) | |
240 : CFX_BaseMassArray(iChunkSize, sizeof(baseType)) {} | |
241 CFX_MassArrayTemplate(int32_t iChunkSize, int32_t iBlockSize) | |
242 : CFX_BaseMassArray(iChunkSize, iBlockSize) {} | |
243 int32_t GetSize() const { return CFX_BaseMassArray::GetSize(); } | |
244 baseType* AddSpace() { | |
245 return (baseType*)CFX_BaseMassArray::AddSpaceTo( | |
246 CFX_BaseMassArray::GetSize()); | |
247 } | |
248 int32_t Add(const baseType& element) { | |
249 int32_t index = CFX_BaseMassArray::GetSize(); | |
250 *(baseType*)CFX_BaseMassArray::AddSpaceTo(index) = element; | |
251 return index; | |
252 } | |
253 baseType& GetAt(int32_t index) const { | |
254 return *(baseType*)CFX_BaseMassArray::GetAt(index); | |
255 } | |
256 baseType* GetPtrAt(int32_t index) const { | |
257 return (baseType*)CFX_BaseMassArray::GetAt(index); | |
258 } | |
259 void SetAt(int32_t index, const baseType& element) { | |
260 *(baseType*)CFX_BaseMassArray::GetAt(index) = element; | |
261 } | |
262 void SetAtGrow(int32_t index, const baseType& element) { | |
263 *(baseType*)CFX_BaseMassArray::AddSpaceTo(index) = element; | |
264 } | |
265 int32_t Append(const CFX_MassArrayTemplate& src, | |
266 int32_t iStart = 0, | |
267 int32_t iCount = -1) { | |
268 return CFX_BaseMassArray::Append(src, iStart, iCount); | |
269 } | |
270 int32_t Copy(const CFX_MassArrayTemplate& src, | |
271 int32_t iStart = 0, | |
272 int32_t iCount = -1) { | |
273 return CFX_BaseMassArray::Copy(src, iStart, iCount); | |
274 } | |
275 int32_t RemoveLast(int32_t iCount = -1) { | |
276 return CFX_BaseMassArray::RemoveLast(iCount); | |
277 } | |
278 void RemoveAll(FX_BOOL bLeaveMemory = FALSE) { | |
279 CFX_BaseMassArray::RemoveAll(bLeaveMemory); | |
280 } | |
281 }; | |
282 typedef CFX_MassArrayTemplate<void*> CFX_PtrMassArray; | |
283 typedef CFX_MassArrayTemplate<int32_t> CFX_Int32MassArray; | |
284 typedef CFX_MassArrayTemplate<FX_DWORD> CFX_DWordMassArray; | |
285 typedef CFX_MassArrayTemplate<FX_WORD> CFX_WordMassArray; | |
286 typedef CFX_MassArrayTemplate<CFX_Rect> CFX_RectMassArray; | |
287 typedef CFX_MassArrayTemplate<CFX_RectF> CFX_RectFMassArray; | |
288 | |
289 template <class baseType> | |
290 class CFX_ObjectMassArrayTemplate : public CFX_BaseMassArray { | |
291 public: | |
292 CFX_ObjectMassArrayTemplate(int32_t iChunkSize = 100) | |
293 : CFX_BaseMassArray(iChunkSize, sizeof(baseType)) {} | |
294 ~CFX_ObjectMassArrayTemplate() { RemoveAll(FALSE); } | |
295 int32_t GetSize() const { return CFX_BaseMassArray::GetSize(); } | |
296 int32_t Add(const baseType& element) { | |
297 int32_t index = CFX_BaseMassArray::GetSize(); | |
298 baseType* p = (baseType*)CFX_BaseMassArray::AddSpaceTo(index); | |
299 new ((void*)p) baseType(element); | |
300 return index; | |
301 } | |
302 baseType& GetAt(int32_t index) const { | |
303 return *(baseType*)CFX_BaseMassArray::GetAt(index); | |
304 } | |
305 baseType* GetPtrAt(int32_t index) const { | |
306 return (baseType*)CFX_BaseMassArray::GetAt(index); | |
307 } | |
308 int32_t Append(const CFX_ObjectMassArrayTemplate& src, | |
309 int32_t iStart = 0, | |
310 int32_t iCount = -1) { | |
311 if (iCount == 0) { | |
312 return CFX_BaseMassArray::GetSize(); | |
313 } | |
314 int32_t iSize = src.GetSize(); | |
315 FXSYS_assert(iStart > -1 && iStart < iSize); | |
316 if (iCount < 0) { | |
317 iCount = iSize; | |
318 } | |
319 int32_t iEnd = iStart + iCount; | |
320 if (iEnd > iSize) { | |
321 iEnd = iSize; | |
322 } | |
323 for (int32_t i = iStart; i < iEnd; i++) { | |
324 Add(src.GetAt(i)); | |
325 } | |
326 return CFX_BaseMassArray::GetSize(); | |
327 } | |
328 int32_t Copy(const CFX_ObjectMassArrayTemplate& src, | |
329 int32_t iStart = 0, | |
330 int32_t iCount = -1) { | |
331 if (iCount == 0) { | |
332 return CFX_BaseMassArray::GetSize(); | |
333 } | |
334 int32_t iSize = src.GetSize(); | |
335 FXSYS_assert(iStart > -1 && iStart < iSize); | |
336 if (iCount < 0) { | |
337 iCount = iSize; | |
338 } | |
339 int32_t iEnd = iStart + iCount; | |
340 if (iEnd > iSize) { | |
341 iEnd = iSize; | |
342 } | |
343 RemoveAll(TRUE); | |
344 for (int32_t i = iStart; i < iEnd; i++) { | |
345 Add(src.GetAt(i)); | |
346 } | |
347 return CFX_BaseMassArray::GetSize(); | |
348 } | |
349 int32_t RemoveLast(int32_t iCount = -1) { | |
350 int32_t iSize = CFX_BaseMassArray::GetSize(); | |
351 if (iCount < 0 || iCount > iSize) { | |
352 iCount = iSize; | |
353 } | |
354 if (iCount == 0) { | |
355 return iSize; | |
356 } | |
357 for (int32_t i = iSize - iCount; i < iSize; i++) { | |
358 ((baseType*)GetPtrAt(i))->~baseType(); | |
359 } | |
360 return CFX_BaseMassArray::RemoveLast(iCount); | |
361 } | |
362 void RemoveAll(FX_BOOL bLeaveMemory = FALSE) { | |
363 int32_t iSize = CFX_BaseMassArray::GetSize(); | |
364 for (int32_t i = 0; i < iSize; i++) { | |
365 ((baseType*)GetPtrAt(i))->~baseType(); | |
366 } | |
367 CFX_BaseMassArray::RemoveAll(bLeaveMemory); | |
368 } | |
369 }; | |
370 | |
371 class CFX_BaseDiscreteArray : public CFX_Target { | |
372 protected: | |
373 CFX_BaseDiscreteArray(int32_t iChunkSize, int32_t iBlockSize); | |
374 ~CFX_BaseDiscreteArray(); | |
375 uint8_t* AddSpaceTo(int32_t index); | |
376 uint8_t* GetAt(int32_t index) const; | |
377 void RemoveAll(); | |
378 void* m_pData; | |
379 }; | |
380 | |
381 template <class baseType> | |
382 class CFX_DiscreteArrayTemplate : public CFX_BaseDiscreteArray { | |
383 public: | |
384 CFX_DiscreteArrayTemplate(int32_t iChunkSize = 100) | |
385 : CFX_BaseDiscreteArray(iChunkSize, sizeof(baseType)) {} | |
386 baseType& GetAt(int32_t index, const baseType& defValue) const { | |
387 baseType* p = (baseType*)CFX_BaseDiscreteArray::GetAt(index); | |
388 return p == NULL ? (baseType&)defValue : *p; | |
389 } | |
390 baseType* GetPtrAt(int32_t index) const { | |
391 return (baseType*)CFX_BaseDiscreteArray::GetAt(index); | |
392 } | |
393 void SetAtGrow(int32_t index, const baseType& element) { | |
394 *(baseType*)CFX_BaseDiscreteArray::AddSpaceTo(index) = element; | |
395 } | |
396 void RemoveAll() { CFX_BaseDiscreteArray::RemoveAll(); } | |
397 }; | |
398 typedef CFX_DiscreteArrayTemplate<void*> CFX_PtrDiscreteArray; | |
399 typedef CFX_DiscreteArrayTemplate<FX_DWORD> CFX_DWordDiscreteArray; | |
400 typedef CFX_DiscreteArrayTemplate<FX_WORD> CFX_WordDiscreteArray; | |
401 | |
402 class CFX_BaseStack : public CFX_Target { | |
403 protected: | |
404 CFX_BaseStack(int32_t iChunkSize, int32_t iBlockSize); | |
405 ~CFX_BaseStack(); | |
406 uint8_t* Push(); | |
407 void Pop(); | |
408 uint8_t* GetTopElement() const; | |
409 int32_t GetSize() const; | |
410 uint8_t* GetAt(int32_t index) const; | |
411 void RemoveAll(FX_BOOL bLeaveMemory = FALSE); | |
412 CFX_BaseMassArrayImp* m_pData; | |
413 }; | |
414 | |
415 template <class baseType> | |
416 class CFX_StackTemplate : public CFX_BaseStack { | |
417 public: | |
418 CFX_StackTemplate(int32_t iChunkSize = 100) | |
419 : CFX_BaseStack(iChunkSize, sizeof(baseType)) {} | |
420 int32_t Push(const baseType& element) { | |
421 int32_t index = CFX_BaseStack::GetSize(); | |
422 *(baseType*)CFX_BaseStack::Push() = element; | |
423 return index; | |
424 } | |
425 void Pop() { CFX_BaseStack::Pop(); } | |
426 baseType* GetTopElement() const { | |
427 return (baseType*)CFX_BaseStack::GetTopElement(); | |
428 } | |
429 int32_t GetSize() const { return CFX_BaseStack::GetSize(); } | |
430 baseType* GetAt(int32_t index) const { | |
431 return (baseType*)CFX_BaseStack::GetAt(index); | |
432 } | |
433 void RemoveAll(FX_BOOL bLeaveMemory = FALSE) { | |
434 CFX_BaseStack::RemoveAll(bLeaveMemory); | |
435 } | |
436 }; | |
437 typedef CFX_StackTemplate<void*> CFX_PtrStack; | |
438 typedef CFX_StackTemplate<FX_DWORD> CFX_DWordStack; | |
439 typedef CFX_StackTemplate<FX_WORD> CFX_WordStack; | |
440 typedef CFX_StackTemplate<int32_t> CFX_Int32Stack; | |
441 | |
442 template <class baseType> | |
443 class CFX_ObjectStackTemplate : public CFX_BaseStack { | |
444 public: | |
445 CFX_ObjectStackTemplate(int32_t iChunkSize = 100) | |
446 : CFX_BaseStack(iChunkSize, sizeof(baseType)) {} | |
447 ~CFX_ObjectStackTemplate() { RemoveAll(); } | |
448 int32_t Push(const baseType& element) { | |
449 int32_t index = CFX_BaseStack::GetSize(); | |
450 baseType* p = (baseType*)CFX_BaseStack::Push(); | |
451 new ((void*)p) baseType(element); | |
452 return index; | |
453 } | |
454 void Pop() { | |
455 baseType* p = (baseType*)CFX_BaseStack::GetTopElement(); | |
456 if (p != NULL) { | |
457 p->~baseType(); | |
458 } | |
459 CFX_BaseStack::Pop(); | |
460 } | |
461 baseType* GetTopElement() const { | |
462 return (baseType*)CFX_BaseStack::GetTopElement(); | |
463 } | |
464 int32_t GetSize() const { return CFX_BaseStack::GetSize(); } | |
465 baseType* GetAt(int32_t index) const { | |
466 return (baseType*)CFX_BaseStack::GetAt(index); | |
467 } | |
468 void RemoveAll(FX_BOOL bLeaveMemory = FALSE) { | |
469 int32_t iSize = CFX_BaseStack::GetSize(); | |
470 for (int32_t i = 0; i < iSize; i++) { | |
471 ((baseType*)CFX_BaseStack::GetAt(i))->~baseType(); | |
472 } | |
473 CFX_BaseStack::RemoveAll(bLeaveMemory); | |
474 } | |
475 int32_t Copy(const CFX_ObjectStackTemplate& src, | |
476 int32_t iStart = 0, | |
477 int32_t iCount = -1) { | |
478 if (iCount == 0) { | |
479 return CFX_BaseStack::GetSize(); | |
480 } | |
481 int32_t iSize = src.GetSize(); | |
482 FXSYS_assert(iStart > -1 && iStart < iSize); | |
483 if (iCount < 0) { | |
484 iCount = iSize; | |
485 } | |
486 int32_t iEnd = iStart + iCount; | |
487 if (iEnd > iSize) { | |
488 iEnd = iSize; | |
489 } | |
490 RemoveAll(TRUE); | |
491 for (int32_t i = iStart; i < iEnd; i++) { | |
492 Push(*src.GetAt(i)); | |
493 } | |
494 return CFX_BaseStack::GetSize(); | |
495 } | |
496 }; | |
497 | |
498 template <class baseType> | |
499 class CFX_CPLTreeNode : public CFX_Target { | |
500 public: | |
501 typedef CFX_CPLTreeNode<baseType> CPLTreeNode; | |
502 CFX_CPLTreeNode() | |
503 : m_pParentNode(NULL), | |
504 m_pChildNode(NULL), | |
505 m_pPrevNode(NULL), | |
506 m_pNextNode(NULL), | |
507 m_Data() {} | |
508 enum TreeNode { | |
509 Root = 0, | |
510 Parent, | |
511 FirstSibling, | |
512 PreviousSibling, | |
513 NextSibling, | |
514 LastSibling, | |
515 FirstNeighbor, | |
516 PreviousNeighbor, | |
517 NextNeighbor, | |
518 LastNeighbor, | |
519 FirstChild, | |
520 LastChild | |
521 }; | |
522 CPLTreeNode* GetNode(TreeNode eNode) const { | |
523 switch (eNode) { | |
524 case Root: { | |
525 CPLTreeNode* pParent = (CPLTreeNode*)this; | |
526 CPLTreeNode* pTemp; | |
527 while ((pTemp = pParent->m_pParentNode) != NULL) { | |
528 pParent = pTemp; | |
529 } | |
530 return pParent; | |
531 } | |
532 case Parent: | |
533 return m_pParentNode; | |
534 case FirstSibling: { | |
535 CPLTreeNode* pNode = (CPLTreeNode*)this; | |
536 CPLTreeNode* pTemp; | |
537 while ((pTemp = pNode->m_pPrevNode) != NULL) { | |
538 pNode = pTemp; | |
539 } | |
540 return pNode == (CPLTreeNode*)this ? NULL : pNode; | |
541 } | |
542 case PreviousSibling: | |
543 return m_pPrevNode; | |
544 case NextSibling: | |
545 return m_pNextNode; | |
546 case LastSibling: { | |
547 CPLTreeNode* pNode = (CPLTreeNode*)this; | |
548 CPLTreeNode* pTemp; | |
549 while ((pTemp = pNode->m_pNextNode) != NULL) { | |
550 pNode = pTemp; | |
551 } | |
552 return pNode == (CPLTreeNode*)this ? NULL : pNode; | |
553 } | |
554 case FirstNeighbor: { | |
555 CPLTreeNode* pParent = (CPLTreeNode*)this; | |
556 CPLTreeNode* pTemp; | |
557 while ((pTemp = pParent->m_pParentNode) != NULL) { | |
558 pParent = pTemp; | |
559 } | |
560 return pParent == (CPLTreeNode*)this ? NULL : pParent; | |
561 } | |
562 case PreviousNeighbor: { | |
563 if (m_pPrevNode == NULL) { | |
564 return m_pParentNode; | |
565 } | |
566 CPLTreeNode* pNode = m_pPrevNode; | |
567 CPLTreeNode* pTemp; | |
568 while ((pTemp = pNode->m_pChildNode) != NULL) { | |
569 pNode = pTemp; | |
570 while ((pTemp = pNode->m_pNextNode) != NULL) { | |
571 pNode = pTemp; | |
572 } | |
573 } | |
574 return pNode; | |
575 } | |
576 case NextNeighbor: { | |
577 if (m_pChildNode != NULL) { | |
578 return m_pChildNode; | |
579 } | |
580 if (m_pNextNode != NULL) { | |
581 return m_pNextNode; | |
582 } | |
583 CPLTreeNode* pNode = m_pParentNode; | |
584 while (pNode != NULL) { | |
585 if (pNode->m_pNextNode != NULL) { | |
586 return pNode->m_pNextNode; | |
587 } | |
588 pNode = pNode->m_pParentNode; | |
589 } | |
590 return NULL; | |
591 } | |
592 case LastNeighbor: { | |
593 CPLTreeNode* pNode = (CPLTreeNode*)this; | |
594 CPLTreeNode* pTemp; | |
595 while ((pTemp = pNode->m_pParentNode) != NULL) { | |
596 pNode = pTemp; | |
597 } | |
598 while (TRUE) { | |
599 CPLTreeNode* pTemp; | |
600 while ((pTemp = pNode->m_pNextNode) != NULL) { | |
601 pNode = pTemp; | |
602 } | |
603 if (pNode->m_pChildNode == NULL) { | |
604 break; | |
605 } | |
606 pNode = pNode->m_pChildNode; | |
607 } | |
608 return pNode == (CPLTreeNode*)this ? NULL : pNode; | |
609 } | |
610 case FirstChild: | |
611 return m_pChildNode; | |
612 case LastChild: { | |
613 if (m_pChildNode == NULL) { | |
614 return NULL; | |
615 } | |
616 CPLTreeNode* pChild = m_pChildNode; | |
617 CPLTreeNode* pTemp; | |
618 while ((pTemp = pChild->m_pNextNode) != NULL) { | |
619 pChild = pTemp; | |
620 } | |
621 return pChild; | |
622 } | |
623 default: | |
624 break; | |
625 } | |
626 return NULL; | |
627 } | |
628 void SetParentNode(CPLTreeNode* pNode) { m_pParentNode = pNode; } | |
629 int32_t CountChildNodes() const { | |
630 int32_t iCount = 0; | |
631 CPLTreeNode* pNode = m_pChildNode; | |
632 while (pNode) { | |
633 iCount++; | |
634 pNode = pNode->m_pNextNode; | |
635 } | |
636 return iCount; | |
637 } | |
638 CPLTreeNode* GetChildNode(int32_t iIndex) const { | |
639 int32_t iCount = 0; | |
640 CPLTreeNode* pNode = m_pChildNode; | |
641 while (pNode) { | |
642 if (iIndex == iCount) { | |
643 return pNode; | |
644 } | |
645 iCount++; | |
646 pNode = pNode->m_pNextNode; | |
647 } | |
648 return NULL; | |
649 } | |
650 int32_t GetNodeIndex() const { | |
651 int32_t index = 0; | |
652 CPLTreeNode* pNode = m_pPrevNode; | |
653 while (pNode != NULL) { | |
654 index++; | |
655 pNode = pNode->m_pPrevNode; | |
656 } | |
657 return index; | |
658 } | |
659 FX_BOOL IsParentNode(const CPLTreeNode* pNode) const { | |
660 CPLTreeNode* pParent = m_pParentNode; | |
661 while (pParent != NULL) { | |
662 if (pParent == pNode) { | |
663 return TRUE; | |
664 } | |
665 pParent = pParent->GetTreeNode(Parent); | |
666 } | |
667 return FALSE; | |
668 } | |
669 FX_BOOL IsChildNode(const CPLTreeNode* pNode) const { | |
670 if (pNode == NULL) { | |
671 return FALSE; | |
672 } | |
673 return pNode->IsParentNode((const CPLTreeNode*)this); | |
674 } | |
675 void SetChildNode(CPLTreeNode* pNode) { m_pChildNode = pNode; } | |
676 void SetPrevNode(CPLTreeNode* pNode) { m_pPrevNode = pNode; } | |
677 void SetNextNode(CPLTreeNode* pNode) { m_pNextNode = pNode; } | |
678 int32_t GetNodeLevel() const { | |
679 int32_t iLevel = 0; | |
680 CPLTreeNode* pNode = (CPLTreeNode*)this; | |
681 while ((pNode = pNode->m_pParentNode) != NULL) { | |
682 iLevel++; | |
683 } | |
684 return iLevel; | |
685 } | |
686 bool IsRootNode() const { return !m_pParentNode; } | |
687 baseType GetData() const { return m_Data; } | |
688 void SetData(baseType data) { m_Data = data; } | |
689 | |
690 protected: | |
691 CPLTreeNode* m_pParentNode; | |
692 CPLTreeNode* m_pChildNode; | |
693 CPLTreeNode* m_pPrevNode; | |
694 CPLTreeNode* m_pNextNode; | |
695 baseType m_Data; | |
696 friend class CFX_CPLTree<baseType>; | |
697 }; | |
698 | |
699 template <class baseType> | |
700 class CFX_CPLTree { | |
701 public: | |
702 typedef CFX_CPLTreeNode<baseType> CPLTreeNode; | |
703 CFX_CPLTree() : m_Root() {} | |
704 ~CFX_CPLTree() { | |
705 CPLTreeNode* pNode = m_Root.GetNode(CPLTreeNode::LastNeighbor); | |
706 while (pNode != NULL) { | |
707 if (pNode->IsRootNode()) { | |
708 break; | |
709 } | |
710 CPLTreeNode* pTemp = pNode->GetNode(CPLTreeNode::PreviousNeighbor); | |
711 delete pNode; | |
712 pNode = pTemp; | |
713 } | |
714 } | |
715 CPLTreeNode* GetRoot() { return &m_Root; } | |
716 CPLTreeNode* AddChild(baseType data, CPLTreeNode* pParent = NULL) { | |
717 if (pParent == NULL) { | |
718 pParent = &m_Root; | |
719 } | |
720 CPLTreeNode* pChild = new CPLTreeNode; | |
721 pChild->SetParentNode(pParent); | |
722 pChild->SetData(data); | |
723 if (pParent->m_pChildNode == NULL) { | |
724 pParent->m_pChildNode = pChild; | |
725 } else { | |
726 CPLTreeNode* pLast = pParent->GetNode(CPLTreeNode::LastChild); | |
727 pChild->SetPrevNode(pLast); | |
728 pLast->SetNextNode(pChild); | |
729 } | |
730 return pChild; | |
731 } | |
732 | |
733 protected: | |
734 CPLTreeNode m_Root; | |
735 }; | |
736 | |
737 #endif // XFA_SRC_FGAS_CRT_FGAS_UTILS_H_ | |
OLD | NEW |