gwnavruntime/kernel/SF_Array.h Source File

SF_Array.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 PublicHeader: None
10 Filename : KY_Array.h
11 Content : Template implementation for Array
12 Created : August 20, 2001
13 Authors : Michael Antonov, Maxim Shemanarev
14 
15 **************************************************************************/
16 
17 #ifndef INC_KY_Kernel_Array_H
18 #define INC_KY_Kernel_Array_H
19 
21 
22 #undef new
23 
24 namespace Kaim {
25 
26 // ***** ArrayDefaultPolicy
27 //
28 // Default resize behavior. No minimal capacity, Granularity=4,
29 // Shrinking as needed. ArrayConstPolicy actually is the same as
30 // ArrayDefaultPolicy, but parametrized with constants.
31 // This struct is used only in order to reduce the template "matroska".
32 //------------------------------------------------------------------------
33 struct ArrayDefaultPolicy
34 {
35  ArrayDefaultPolicy() : Capacity(0) {}
36  ArrayDefaultPolicy(const ArrayDefaultPolicy&) : Capacity(0) {}
37 
38  UPInt GetMinCapacity() const { return 0; }
39  UPInt GetGranularity() const { return 4; }
40  bool NeverShrinking() const { return 0; }
41 
42  UPInt GetCapacity() const { return Capacity; }
43  void SetCapacity(UPInt capacity) { Capacity = capacity; }
44 private:
45  UPInt Capacity;
46 };
47 
48 
49 // ***** ArrayConstPolicy
50 //
51 // Statically parametrized resizing behavior:
52 // MinCapacity, Granularity, and Shrinking flag.
53 //------------------------------------------------------------------------
54 template<int MinCapacity=0, int Granularity=4, bool NeverShrink=false>
55 struct ArrayConstPolicy
56 {
57  typedef ArrayConstPolicy<MinCapacity, Granularity, NeverShrink> SelfType;
58 
59  ArrayConstPolicy() : Capacity(0) {}
60  ArrayConstPolicy(const SelfType&) : Capacity(0) {}
61 
62  UPInt GetMinCapacity() const { return MinCapacity; }
63  UPInt GetGranularity() const { return Granularity; }
64  bool NeverShrinking() const { return NeverShrink; }
65 
66  UPInt GetCapacity() const { return Capacity; }
67  void SetCapacity(UPInt capacity) { Capacity = capacity; }
68 private:
69  UPInt Capacity;
70 };
71 
72 
73 // ***** ArrayDataBase
74 //
75 // Basic operations with array data: Reserve, Resize, Free, ArrayPolicy.
76 // For internal use only: ArrayData, ArrayDataDH, ArrayDataCC and others.
77 //------------------------------------------------------------------------
78 template<class T, class Allocator, class SizePolicy> struct ArrayDataBase
79 {
80  typedef T ValueType;
81  typedef Allocator AllocatorType;
82  typedef SizePolicy SizePolicyType;
83  typedef ArrayDataBase<T, Allocator, SizePolicy> SelfType;
84 
85  ArrayDataBase()
86  : Data(0), Size(0), Policy() {}
87 
88  ArrayDataBase(const SizePolicy& p)
89  : Data(0), Size(0), Policy(p) {}
90 
91  ~ArrayDataBase()
92  {
93  Allocator::DestructArray(Data, Size);
94  Allocator::Free(Data);
95  }
96 
97  UPInt GetCapacity() const
98  {
99  return Policy.GetCapacity();
100  }
101 
102  void ClearAndRelease()
103  {
104  Allocator::DestructArray(Data, Size);
105  Allocator::Free(Data);
106  Data = 0;
107  Size = 0;
108  Policy.SetCapacity(0);
109  }
110 
111  void Reserve(const void* pheapAddr, UPInt newCapacity)
112  {
113  if (Policy.NeverShrinking() && newCapacity < GetCapacity())
114  return;
115 
116  if (newCapacity < Policy.GetMinCapacity())
117  newCapacity = Policy.GetMinCapacity();
118 
119  // Resize the buffer.
120  if (newCapacity == 0)
121  {
122  if (Data)
123  {
124  Allocator::Free(Data);
125  Data = 0;
126  }
127  Policy.SetCapacity(0);
128  }
129  else
130  {
131  UPInt gran = Policy.GetGranularity();
132  newCapacity = (newCapacity + gran - 1) / gran * gran;
133  if (Data)
134  {
135  if (Allocator::IsMovable())
136  {
137  // LocalHeap AllocatorBaseLH => Memory::ReallocAutoHeap( Data, sizeof(T) * newCapacity)
138  // DynamicHeap AllocatorBaseDH => Memory::ReallocInHeap((MemoryHeap*)pheapAddr, Data, sizeof(T) * newCapacity);
139  // GlobalHeap AllocatorBaseGH => Memory::Realloc( Data, sizeof(T) * newCapacity);
140  Data = (T*)Allocator::Realloc(pheapAddr, Data, sizeof(T) * newCapacity, __FILE__, __LINE__);
141  }
142  else
143  {
144  T* newData = (T*)Allocator::Alloc(pheapAddr, sizeof(T) * newCapacity, __FILE__, __LINE__);
145  UPInt i, s;
146  s = (Size < newCapacity) ? Size : newCapacity;
147  for (i = 0; i < s; ++i)
148  {
149  Allocator::Construct(&newData[i], Data[i]);
150  Allocator::Destruct(&Data[i]);
151  }
152  for (i = s; i < Size; ++i)
153  {
154  Allocator::Destruct(&Data[i]);
155  }
156  Allocator::Free(Data);
157  Data = newData;
158  }
159  }
160  else
161  {
162  Data = (T*)Allocator::Alloc(pheapAddr, sizeof(T) * newCapacity, __FILE__, __LINE__);
163  //memset(Buffer, 0, (sizeof(ValueType) * newSize)); // Do we need this?
164  }
165  Policy.SetCapacity(newCapacity);
166  // KY_ASSERT(Data); // need to throw (or something) on alloc failure!
167  }
168  }
169 
170  // This version of Resize DOES NOT construct the elements.
171  // It's done to optimize PushBack, which uses a copy constructor
172  // instead of the default constructor and assignment
173  void ResizeNoConstruct(const void* pheapAddr, UPInt newSize)
174  {
175  UPInt oldSize = Size;
176 
177  if (newSize < oldSize)
178  {
179  Allocator::DestructArray(Data + newSize, oldSize - newSize);
180  if (newSize < (Policy.GetCapacity() >> 1))
181  {
182  Reserve(pheapAddr, newSize);
183  }
184  }
185  else if(newSize > Policy.GetCapacity())
186  {
187  Reserve(pheapAddr, newSize + (newSize >> 2));
188  }
189  // IMPORTANT to modify Size only after Reserve completes, because garbage collectable
190  // array may use this array and may traverse it during Reserve (in the case, if
191  // collection occurs because of heap limit exceeded).
192  Size = newSize;
193  }
194 
195  ValueType* Data;
196  UPInt Size;
197  SizePolicy Policy;
198 };
199 
200 
201 
202 
203 // ***** ArrayData
204 //
205 // General purpose array data.
206 // For internal use only in Array, ArrayLH, ArrayPOD and so on.
207 //------------------------------------------------------------------------
208 template<class T, class Allocator, class SizePolicy> struct ArrayData :
209 ArrayDataBase<T, Allocator, SizePolicy>
210 {
211  typedef T ValueType;
212  typedef Allocator AllocatorType;
213  typedef SizePolicy SizePolicyType;
214  typedef ArrayDataBase<T, Allocator, SizePolicy> BaseType;
215  typedef ArrayData <T, Allocator, SizePolicy> SelfType;
216 
217  ArrayData()
218  : BaseType() { }
219 
220  ArrayData(int size)
221  : BaseType() { Resize(size); }
222 
223  ArrayData(const SelfType& a)
224  : BaseType(a.Policy) { Append(a.Data, a.Size); }
225 
226  void Reserve(UPInt newCapacity)
227  {
228  BaseType::Reserve(this, newCapacity);
229  }
230 
231  void Resize(UPInt newSize)
232  {
233  UPInt oldSize = this->Size;
234  BaseType::ResizeNoConstruct(this, newSize);
235  if(newSize > oldSize)
236  Allocator::ConstructArray(this->Data + oldSize, newSize - oldSize);
237  }
238 
239  void PushBack(const ValueType& val)
240  {
241  BaseType::ResizeNoConstruct(this, this->Size + 1);
242  Allocator::Construct(this->Data + this->Size - 1, val);
243  }
244 
245  template<class S>
246  void PushBackAlt(const S& val)
247  {
248  BaseType::ResizeNoConstruct(this, this->Size + 1);
249  Allocator::ConstructAlt(this->Data + this->Size - 1, val);
250  }
251 
252  // Append the given data to the array.
253  void Append(const ValueType other[], UPInt count)
254  {
255  if (count)
256  {
257  UPInt oldSize = this->Size;
258  BaseType::ResizeNoConstruct(this, this->Size + count);
259  Allocator::ConstructArray(this->Data + oldSize, count, other);
260  }
261  }
262 };
263 
264 
265 
266 
267 // ***** ArrayDataDH
268 //
269 // General purpose array data with a heap pointer
270 // For internal use only in ArrayDH, ArrayDH_POD and so on.
271 //------------------------------------------------------------------------
272 template<class T, class Allocator, class SizePolicy> struct ArrayDataDH :
273 ArrayDataBase<T, Allocator, SizePolicy>
274 {
275  typedef T ValueType;
276  typedef Allocator AllocatorType;
277  typedef SizePolicy SizePolicyType;
278  typedef ArrayDataBase<T, Allocator, SizePolicy> BaseType;
279  typedef ArrayDataDH <T, Allocator, SizePolicy> SelfType;
280 
281  ArrayDataDH(MemoryHeap* heap)
282  : BaseType(), pHeap(heap) { }
283 
284  ArrayDataDH(MemoryHeap* heap, int size)
285  : BaseType(), pHeap(heap) { Resize(size); }
286 
287  ArrayDataDH(const SelfType& a)
288  : BaseType(a.Policy), pHeap(a.pHeap) { Append(a.Data, a.Size); }
289 
290  void Reserve(UPInt newCapacity)
291  {
292  BaseType::Reserve(pHeap, newCapacity);
293  }
294 
295  void Resize(UPInt newSize)
296  {
297  UPInt oldSize = this->Size;
298  BaseType::ResizeNoConstruct(pHeap, newSize);
299  if(newSize > oldSize)
300  Allocator::ConstructArray(this->Data + oldSize, newSize - oldSize);
301  }
302 
303  void PushBack(const ValueType& val)
304  {
305  BaseType::ResizeNoConstruct(pHeap, this->Size + 1);
306  Allocator::Construct(this->Data + this->Size - 1, val);
307  }
308 
309  template<class S>
310  void PushBackAlt(const S& val)
311  {
312  BaseType::ResizeNoConstruct(pHeap, this->Size + 1);
313  Allocator::ConstructAlt(this->Data + this->Size - 1, val);
314  }
315 
316  // Append the given data to the array.
317  void Append(const ValueType other[], UPInt count)
318  {
319  if (count > 0)
320  {
321  UPInt oldSize = this->Size;
322  BaseType::ResizeNoConstruct(pHeap, this->Size + count);
323  Allocator::ConstructArray(this->Data + oldSize, count, other);
324  }
325  }
326 
327  const MemoryHeap* pHeap;
328 };
329 
330 
331 
332 
333 // ***** ArrayDataCC
334 //
335 // A modification of ArrayData that always copy-constructs new elements
336 // using a specified DefaultValue. For internal use only in ArrayCC.
337 //------------------------------------------------------------------------
338 template<class T, class Allocator, class SizePolicy> struct ArrayDataCC :
339 ArrayDataBase<T, Allocator, SizePolicy>
340 {
341  typedef T ValueType;
342  typedef Allocator AllocatorType;
343  typedef SizePolicy SizePolicyType;
344  typedef ArrayDataBase<T, Allocator, SizePolicy> BaseType;
345  typedef ArrayDataCC <T, Allocator, SizePolicy> SelfType;
346 
347  ArrayDataCC(const ValueType& defval)
348  : BaseType(), DefaultValue(defval) { }
349 
350  ArrayDataCC(const ValueType& defval, int size)
351  : BaseType(), DefaultValue(defval) { Resize(size); }
352 
353  ArrayDataCC(const SelfType& a)
354  : BaseType(a.Policy), DefaultValue(a.DefaultValue) { Append(a.Data, a.Size); }
355 
356  void Reserve(UPInt newCapacity)
357  {
358  BaseType::Reserve(this, newCapacity);
359  }
360 
361  void Resize(UPInt newSize)
362  {
363  UPInt oldSize = this->Size;
364  BaseType::ResizeNoConstruct(this, newSize);
365  if(newSize > oldSize)
366  Allocator::ConstructArray(this->Data + oldSize, newSize - oldSize, DefaultValue);
367  }
368 
369  void PushBack(const ValueType& val)
370  {
371  BaseType::ResizeNoConstruct(this, this->Size + 1);
372  Allocator::Construct(this->Data + this->Size - 1, val);
373  }
374 
375  template<class S>
376  void PushBackAlt(const S& val)
377  {
378  BaseType::ResizeNoConstruct(this, this->Size + 1);
379  Allocator::ConstructAlt(this->Data + this->Size - 1, val);
380  }
381 
382  // Append the given data to the array.
383  void Append(const ValueType other[], UPInt count)
384  {
385  if (count)
386  {
387  UPInt oldSize = this->Size;
388  BaseType::ResizeNoConstruct(this, this->Size + count);
389  Allocator::ConstructArray(this->Data + oldSize, count, other);
390  }
391  }
392 
393  ValueType DefaultValue;
394 };
395 
396 
397 
398 
399 
400 
401 
402 
403 
404 
405 
406 // ***** ArrayBase
407 //
408 // Resizable array. The behavior can be POD (suffix _POD) and
409 // Movable (no suffix) depending on the allocator policy.
410 // In case of _POD the constructors and destructors are not called.
411 //
412 // Arrays can't handle non-movable objects! Don't put anything in here
413 // that can't be moved around by bitwise copy.
414 //
415 // The addresses of elements are not persistent! Don't keep the address
416 // of an element; the array contents will move around as it gets resized.
417 //------------------------------------------------------------------------
418 template<class ArrayData> class ArrayBase
419 {
420 public:
421  typedef typename ArrayData::ValueType ValueType;
422  typedef typename ArrayData::AllocatorType AllocatorType;
423  typedef typename ArrayData::SizePolicyType SizePolicyType;
424  typedef ArrayBase<ArrayData> SelfType;
425 
426  KY_MEMORY_REDEFINE_NEW(ArrayBase, AllocatorType::StatId)
427 
428  ArrayBase()
429  : Data() {}
430  ArrayBase(int size)
431  : Data(size) {}
432  ArrayBase(const SelfType& a)
433  : Data(a.Data) {}
434 
435  ArrayBase(MemoryHeap* heap)
436  : Data(heap) {}
437  ArrayBase(MemoryHeap* heap, int size)
438  : Data(heap, size) {}
439 
440  ArrayBase(const ValueType& defval)
441  : Data(defval) {}
442  ArrayBase(const ValueType& defval, int size)
443  : Data(defval, size) {}
444 
445  SizePolicyType* GetSizePolicy() const { return Data.Policy; }
446  void SetSizePolicy(const SizePolicyType& p) { Data.Policy = p; }
447 
448  bool NeverShrinking()const { return Data.Policy.NeverShrinking(); }
449  UPInt GetSize() const { return Data.Size; }
450  bool IsEmpty() const { return Data.Size == 0; }
451  UPInt GetCapacity() const { return Data.GetCapacity(); }
452  UPInt GetNumBytes() const { return Data.GetCapacity() * sizeof(ValueType); }
453 
454  void ClearAndRelease() { Data.ClearAndRelease(); }
455  void Clear() { Data.Resize(0); }
456  void Resize(UPInt newSize) { Data.Resize(newSize); }
457 
458  // Reserve can only increase the capacity
459  void Reserve(UPInt newCapacity)
460  {
461  if (newCapacity > Data.GetCapacity())
462  Data.Reserve(newCapacity);
463  }
464 
465  // Basic access.
466  ValueType& At(UPInt index)
467  {
468  KY_ASSERT(index < Data.Size);
469  return Data.Data[index];
470  }
471  const ValueType& At(UPInt index) const
472  {
473  KY_ASSERT(index < Data.Size);
474  return Data.Data[index];
475  }
476 
477  ValueType ValueAt(UPInt index) const
478  {
479  KY_ASSERT(index < Data.Size);
480  return Data.Data[index];
481  }
482 
483  // Basic access.
484  ValueType& operator [] (UPInt index)
485  {
486  KY_ASSERT(index < Data.Size);
487  return Data.Data[index];
488  }
489  const ValueType& operator [] (UPInt index) const
490  {
491  KY_ASSERT(index < Data.Size);
492  return Data.Data[index];
493  }
494 
495  // Raw pointer to the data. Use with caution!
496  const ValueType* GetDataPtr() const { return Data.Data; }
497  ValueType* GetDataPtr() { return Data.Data; }
498 
499  // Insert the given element at the end of the array.
500  void PushBack(const ValueType& val)
501  {
502  // DO NOT pass elements of your own vector into
503  // push_back()! Since we're using references,
504  // resize() may munge the element storage!
505  // KY_ASSERT(&val < &Buffer[0] || &val > &Buffer[BufferSize]);
506  Data.PushBack(val);
507  }
508 
509  template<class S>
510  void PushBackAlt(const S& val)
511  {
512  Data.PushBackAlt(val);
513  }
514 
515  // Remove the last element.
516  void PopBack(UPInt count = 1)
517  {
518  KY_ASSERT(Data.Size >= count);
519  Data.Resize(Data.Size - count);
520  }
521 
522  ValueType& PushDefault()
523  {
524  Data.PushBack(ValueType());
525  return Back();
526  }
527 
528  ValueType Pop()
529  {
530  ValueType t = Back();
531  PopBack();
532  return t;
533  }
534 
535 
536  // Access the first element.
537  ValueType& Front() { return At(0); }
538  const ValueType& Front() const { return At(0); }
539 
540  // Access the last element.
541  ValueType& Back() { return At(Data.Size - 1); }
542  const ValueType& Back() const { return At(Data.Size - 1); }
543 
544  // Array copy. Copies the contents of a into this array.
545  const SelfType& operator = (const SelfType& a)
546  {
547  Resize(a.GetSize());
548  for (UPInt i = 0; i < Data.Size; i++) {
549  *(Data.Data + i) = a[i];
550  }
551  return *this;
552  }
553 
554  // Removing multiple elements from the array.
555  void RemoveMultipleAt(UPInt index, UPInt num)
556  {
557  KY_ASSERT(index + num <= Data.Size);
558  if (Data.Size == num)
559  {
560  Clear();
561  }
562  else
563  {
564  AllocatorType::DestructArray(Data.Data + index, num);
565  AllocatorType::CopyArrayForward(
566  Data.Data + index,
567  Data.Data + index + num,
568  Data.Size - num - index);
569  Data.Size -= num;
570  }
571  }
572 
573  // Removing an element from the array is an expensive operation!
574  // It compacts only after removing the last element.
575  void RemoveAt(UPInt index)
576  {
577  KY_ASSERT(index < Data.Size);
578  if (Data.Size == 1)
579  {
580  Clear();
581  }
582  else
583  {
584  AllocatorType::Destruct(Data.Data + index);
585  AllocatorType::CopyArrayForward(
586  Data.Data + index,
587  Data.Data + index + 1,
588  Data.Size - 1 - index);
589  --Data.Size;
590  }
591  }
592 
593  // Insert the given object at the given index shifting all the elements up.
594  void InsertAt(UPInt index, const ValueType& val = ValueType())
595  {
596  KY_ASSERT(index <= Data.Size);
597 
598  Data.Resize(Data.Size + 1);
599  if (index < Data.Size - 1)
600  {
601  AllocatorType::CopyArrayBackward(
602  Data.Data + index + 1,
603  Data.Data + index,
604  Data.Size - 1 - index);
605  }
606  AllocatorType::Construct(Data.Data + index, val);
607  }
608 
609  // Insert the given object at the given index shifting all the elements up.
610  void InsertMultipleAt(UPInt index, UPInt num, const ValueType& val = ValueType())
611  {
612  KY_ASSERT(index <= Data.Size);
613 
614  Data.Resize(Data.Size + num);
615  if (index < Data.Size - num)
616  {
617  AllocatorType::CopyArrayBackward(
618  Data.Data + index + num,
619  Data.Data + index,
620  Data.Size - num - index);
621  }
622  for (UPInt i = 0; i < num; ++i)
623  AllocatorType::Construct(Data.Data + index + i, val);
624  }
625 
626  // Append the given data to the array.
627  void Append(const SelfType& other)
628  {
629  Append(other.Data.Data, other.GetSize());
630  }
631 
632  // Append the given data to the array.
633  void Append(const ValueType other[], UPInt count)
634  {
635  Data.Append(other, count);
636  }
637 
638  class Iterator
639  {
640  SelfType* pArray;
641  SPInt CurIndex;
642 
643  public:
644  Iterator() : pArray(0), CurIndex(-1) {}
645  Iterator(SelfType* parr, SPInt idx = 0) : pArray(parr), CurIndex(idx) {}
646 
647  bool operator==(const Iterator& it) const { return pArray == it.pArray && CurIndex == it.CurIndex; }
648  bool operator!=(const Iterator& it) const { return pArray != it.pArray || CurIndex != it.CurIndex; }
649 
650  Iterator& operator++()
651  {
652  if (pArray)
653  {
654  if (CurIndex < (SPInt)pArray->GetSize())
655  ++CurIndex;
656  }
657  return *this;
658  }
659  Iterator operator++(int)
660  {
661  Iterator it(*this);
662  operator++();
663  return it;
664  }
665  Iterator& operator--()
666  {
667  if (pArray)
668  {
669  if (CurIndex >= 0)
670  --CurIndex;
671  }
672  return *this;
673  }
674  Iterator operator--(int)
675  {
676  Iterator it(*this);
677  operator--();
678  return it;
679  }
680  Iterator operator+(int delta) const
681  {
682  return Iterator(pArray, CurIndex + delta);
683  }
684  Iterator operator-(int delta) const
685  {
686  return Iterator(pArray, CurIndex - delta);
687  }
688  SPInt operator-(const Iterator& right) const
689  {
690  KY_ASSERT(pArray == right.pArray);
691  return CurIndex - right.CurIndex;
692  }
693  ValueType& operator*() const { KY_ASSERT(pArray); return (*pArray)[CurIndex]; }
694  ValueType* operator->() const { KY_ASSERT(pArray); return &(*pArray)[CurIndex]; }
695  ValueType* GetPtr() const { KY_ASSERT(pArray); return &(*pArray)[CurIndex]; }
696 
697  bool IsFinished() const { return !pArray || CurIndex < 0 || CurIndex >= (int)pArray->GetSize(); }
698 
699  void Remove()
700  {
701  if (!IsFinished())
702  pArray->RemoveAt(CurIndex);
703  }
704 
705  SPInt GetIndex() const { return CurIndex; }
706  };
707 
708  Iterator Begin() { return Iterator(this); }
709  Iterator End() { return Iterator(this, (SPInt)GetSize()); }
710  Iterator Last() { return Iterator(this, (SPInt)GetSize() - 1); }
711 
712  class ConstIterator
713  {
714  const SelfType* pArray;
715  SPInt CurIndex;
716 
717  public:
718  ConstIterator() : pArray(0), CurIndex(-1) {}
719  ConstIterator(const SelfType* parr, SPInt idx = 0) : pArray(parr), CurIndex(idx) {}
720 
721  bool operator==(const ConstIterator& it) const { return pArray == it.pArray && CurIndex == it.CurIndex; }
722  bool operator!=(const ConstIterator& it) const { return pArray != it.pArray || CurIndex != it.CurIndex; }
723 
724  ConstIterator& operator++()
725  {
726  if (pArray)
727  {
728  if (CurIndex < (int)pArray->GetSize())
729  ++CurIndex;
730  }
731  return *this;
732  }
733  ConstIterator operator++(int)
734  {
735  ConstIterator it(*this);
736  operator++();
737  return it;
738  }
739  ConstIterator& operator--()
740  {
741  if (pArray)
742  {
743  if (CurIndex >= 0)
744  --CurIndex;
745  }
746  return *this;
747  }
748  ConstIterator operator--(int)
749  {
750  ConstIterator it(*this);
751  operator--();
752  return it;
753  }
754  ConstIterator operator+(int delta) const
755  {
756  return ConstIterator(pArray, CurIndex + delta);
757  }
758  ConstIterator operator-(int delta) const
759  {
760  return ConstIterator(pArray, CurIndex - delta);
761  }
762  SPInt operator-(const ConstIterator& right) const
763  {
764  KY_ASSERT(pArray == right.pArray);
765  return CurIndex - right.CurIndex;
766  }
767  const ValueType& operator*() const { KY_ASSERT(pArray); return (*pArray)[CurIndex]; }
768  const ValueType* operator->() const { KY_ASSERT(pArray); return &(*pArray)[CurIndex]; }
769  const ValueType* GetPtr() const { KY_ASSERT(pArray); return &(*pArray)[CurIndex]; }
770 
771  bool IsFinished() const { return !pArray || CurIndex < 0 || CurIndex >= (int)pArray->GetSize(); }
772 
773  SPInt GetIndex() const { return CurIndex; }
774  };
775  ConstIterator Begin() const { return ConstIterator(this); }
776  ConstIterator End() const { return ConstIterator(this, (SPInt)GetSize()); }
777  ConstIterator Last() const { return ConstIterator(this, (SPInt)GetSize() - 1); }
778 
779 protected:
780  ArrayData Data;
781 };
782 
783 
784 
785 
786 // ***** Array
787 //
788 // General purpose array for movable objects that require explicit
789 // construction/destruction. Global heap is in use.
790 //------------------------------------------------------------------------
791 template<class T, int SID=Stat_Default_Mem, class SizePolicy=ArrayDefaultPolicy>
792 class Array : public ArrayBase<ArrayData<T, AllocatorGH<T, SID>, SizePolicy> >
793 {
794 public:
795  typedef T ValueType;
796  typedef AllocatorGH<T, SID> AllocatorType;
797  typedef SizePolicy SizePolicyType;
798  typedef Array<T, SID, SizePolicy> SelfType;
799  typedef ArrayBase<ArrayData<T, AllocatorGH<T, SID>, SizePolicy> > BaseType;
800 
801  Array() : BaseType() {}
802  Array(int size) : BaseType(size) {}
803  Array(const SizePolicyType& p) : BaseType() { SetSizePolicy(p); }
804  Array(const SelfType& a) : BaseType(a) {}
805  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
806 };
807 
808 
809 // ***** ArrayPOD
810 //
811 // General purpose array for movable objects that DOES NOT require
812 // construction/destruction. Constructors and destructors are not called!
813 // Global heap is in use.
814 //------------------------------------------------------------------------
815 template<class T, int SID=Stat_Default_Mem, class SizePolicy=ArrayDefaultPolicy>
816 class ArrayPOD : public ArrayBase<ArrayData<T, AllocatorGH_POD<T, SID>, SizePolicy> >
817 {
818 public:
819  typedef T ValueType;
820  typedef AllocatorGH<T, SID> AllocatorType;
821  typedef SizePolicy SizePolicyType;
822  typedef ArrayPOD<T, SID, SizePolicy> SelfType;
823  typedef ArrayBase<ArrayData<T, AllocatorGH_POD<T, SID>, SizePolicy> > BaseType;
824 
825  ArrayPOD() : BaseType() {}
826  ArrayPOD(int size) : BaseType(size) {}
827  ArrayPOD(const SizePolicyType& p) : BaseType() { SetSizePolicy(p); }
828  ArrayPOD(const SelfType& a) : BaseType(a) {}
829  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
830 };
831 
832 
833 // ***** ArrayCPP
834 //
835 // General purpose, fully C++ compliant array. Can be used with non-movable data.
836 // Global heap is in use.
837 //------------------------------------------------------------------------
838 template<class T, int SID=Stat_Default_Mem, class SizePolicy=ArrayDefaultPolicy>
839 class ArrayCPP : public ArrayBase<ArrayData<T, AllocatorGH_CPP<T, SID>, SizePolicy> >
840 {
841 public:
842  typedef T ValueType;
843  typedef AllocatorGH<T, SID> AllocatorType;
844  typedef SizePolicy SizePolicyType;
845  typedef ArrayCPP<T, SID, SizePolicy> SelfType;
846  typedef ArrayBase<ArrayData<T, AllocatorGH_CPP<T, SID>, SizePolicy> > BaseType;
847 
848  ArrayCPP() : BaseType() {}
849  ArrayCPP(int size) : BaseType(size) {}
850  ArrayCPP(const SizePolicyType& p) : BaseType() { SetSizePolicy(p); }
851  ArrayCPP(const SelfType& a) : BaseType(a) {}
852  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
853 };
854 
855 
856 // ***** ArrayLH
857 //
858 // General purpose array for movable objects that require explicit
859 // construction/destruction. Local heap is in use.
860 //------------------------------------------------------------------------
861 template<class T, int SID=Stat_Default_Mem, class SizePolicy=ArrayDefaultPolicy>
862 class ArrayLH : public ArrayBase<ArrayData<T, AllocatorLH<T, SID>, SizePolicy> >
863 {
864 public:
865  typedef T ValueType;
866  typedef AllocatorLH<T, SID> AllocatorType;
867  typedef SizePolicy SizePolicyType;
868  typedef ArrayLH<T, SID, SizePolicy> SelfType;
869  typedef ArrayBase<ArrayData<T, AllocatorLH<T, SID>, SizePolicy> > BaseType;
870 
871  ArrayLH() : BaseType() {}
872  ArrayLH(int size) : BaseType(size) {}
873  ArrayLH(const SizePolicyType& p) : BaseType() { SetSizePolicy(p); }
874  ArrayLH(const SelfType& a) : BaseType(a) {}
875  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
876 };
877 
878 // ***** ArrayLH_CPP
879 //
880 // General purpose fully C++ compliant array. Can be used with non-movable data.
881 // Local heap is in use.
882 //------------------------------------------------------------------------
883 template<class T, int SID=Stat_Default_Mem, class SizePolicy=ArrayDefaultPolicy>
884 class ArrayLH_CPP : public ArrayBase<ArrayData<T, AllocatorLH_CPP<T, SID>, SizePolicy> >
885 {
886 public:
887  typedef T ValueType;
888  typedef AllocatorLH<T, SID> AllocatorType;
889  typedef SizePolicy SizePolicyType;
890  typedef ArrayLH_CPP<T, SID, SizePolicy> SelfType;
891  typedef ArrayBase<ArrayData<T, AllocatorLH_CPP<T, SID>, SizePolicy> > BaseType;
892 
893  ArrayLH_CPP() : BaseType() {}
894  ArrayLH_CPP(int size) : BaseType(size) {}
895  ArrayLH_CPP(const SizePolicyType& p) : BaseType() { SetSizePolicy(p); }
896  ArrayLH_CPP(const SelfType& a) : BaseType(a) {}
897  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
898 };
899 
900 // ***** ArrayLH_POD
901 //
902 // General purpose array for movable objects that DOES NOT require
903 // construction/destruction. Constructors and destructors are not called!
904 // Local heap is in use.
905 //------------------------------------------------------------------------
906 template<class T, int SID=Stat_Default_Mem, class SizePolicy=ArrayDefaultPolicy>
907 class ArrayLH_POD : public ArrayBase<ArrayData<T, AllocatorLH_POD<T, SID>, SizePolicy> >
908 {
909 public:
910  typedef T ValueType;
911  typedef AllocatorLH<T, SID> AllocatorType;
912  typedef SizePolicy SizePolicyType;
913  typedef ArrayLH_POD<T, SID, SizePolicy> SelfType;
914  typedef ArrayBase<ArrayData<T, AllocatorLH_POD<T, SID>, SizePolicy> > BaseType;
915 
916  ArrayLH_POD() : BaseType() {}
917  ArrayLH_POD(int size) : BaseType(size) {}
918  ArrayLH_POD(const SizePolicyType& p) : BaseType() { SetSizePolicy(p); }
919  ArrayLH_POD(const SelfType& a) : BaseType(a) {}
920  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
921 };
922 
923 // ***** ArrayDH
924 //
925 // General purpose array for movable objects that require explicit
926 // construction/destruction. Dynamic heap is in use.
927 //------------------------------------------------------------------------
928 template<class T, int SID=Stat_Default_Mem, class SizePolicy=ArrayDefaultPolicy>
929 class ArrayDH : public ArrayBase<ArrayDataDH<T, AllocatorDH<T, SID>, SizePolicy> >
930 {
931 public:
932  typedef T ValueType;
933  typedef AllocatorDH<T, SID> AllocatorType;
934  typedef SizePolicy SizePolicyType;
935  typedef ArrayDH<T, SID, SizePolicy> SelfType;
936  typedef ArrayBase<ArrayDataDH<T, AllocatorDH<T, SID>, SizePolicy> > BaseType;
937 
938  ArrayDH(MemoryHeap* heap) : BaseType(heap) {}
939  ArrayDH(MemoryHeap* heap, int size) : BaseType(heap, size) {}
940  ArrayDH(MemoryHeap* heap, const SizePolicyType& p) : BaseType(heap) { SetSizePolicy(p); }
941  ArrayDH(const SelfType& a) : BaseType(a) {}
942  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
943 };
944 
945 
946 // ***** ArrayDH_CPP
947 //
948 // General purpose fully C++ compliant array. Can be used with non-movable data.
949 // Dynamic heap is in use.
950 //------------------------------------------------------------------------
951 template<class T, int SID=Stat_Default_Mem, class SizePolicy=ArrayDefaultPolicy>
952 class ArrayDH_CPP : public ArrayBase<ArrayDataDH<T, AllocatorDH_CPP<T, SID>, SizePolicy> >
953 {
954 public:
955  typedef T ValueType;
956  typedef AllocatorDH_CPP<T, SID> AllocatorType;
957  typedef SizePolicy SizePolicyType;
958  typedef ArrayDH_CPP<T, SID, SizePolicy> SelfType;
959  typedef ArrayBase<ArrayDataDH<T, AllocatorDH_CPP<T, SID>, SizePolicy> > BaseType;
960 
961  ArrayDH_CPP(MemoryHeap* heap) : BaseType(heap) {}
962  ArrayDH_CPP(MemoryHeap* heap, int size) : BaseType(heap, size) {}
963  ArrayDH_CPP(MemoryHeap* heap, const SizePolicyType& p) : BaseType(heap) { SetSizePolicy(p); }
964  ArrayDH_CPP(const SelfType& a) : BaseType(a) {}
965  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
966 };
967 
968 // ***** ArrayDH_POD
969 //
970 // General purpose array for movable objects that DOES NOT require
971 // construction/destruction. Constructors and destructors are not called!
972 // Dynamic heap is in use.
973 //------------------------------------------------------------------------
974 template<class T, int SID=Stat_Default_Mem, class SizePolicy=ArrayDefaultPolicy>
975 class ArrayDH_POD : public ArrayBase<ArrayDataDH<T, AllocatorDH_POD<T, SID>, SizePolicy> >
976 {
977 public:
978  typedef T ValueType;
979  typedef AllocatorDH<T, SID> AllocatorType;
980  typedef SizePolicy SizePolicyType;
981  typedef ArrayDH_POD<T, SID, SizePolicy> SelfType;
982  typedef ArrayBase<ArrayDataDH<T, AllocatorDH_POD<T, SID>, SizePolicy> > BaseType;
983 
984  ArrayDH_POD(MemoryHeap* heap) : BaseType(heap) {}
985  ArrayDH_POD(MemoryHeap* heap, int size) : BaseType(heap, size) {}
986  ArrayDH_POD(MemoryHeap* heap, const SizePolicyType& p) : BaseType(heap) { SetSizePolicy(p); }
987  ArrayDH_POD(const SelfType& a) : BaseType(a) {}
988  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
989 };
990 
991 
992 // ***** ArrayCC
993 //
994 // A modification of the array that uses the given default value to
995 // construct the elements. The constructors and destructors are
996 // properly called, the objects must be movable.
997 // Local heap is in use.
998 //------------------------------------------------------------------------
999 template<class T, int SID=Stat_Default_Mem, class SizePolicy=ArrayDefaultPolicy>
1000 class ArrayCC : public ArrayBase<ArrayDataCC<T, AllocatorLH<T, SID>, SizePolicy> >
1001 {
1002 public:
1003  typedef T ValueType;
1004  typedef AllocatorLH<T, SID> AllocatorType;
1005  typedef SizePolicy SizePolicyType;
1006  typedef ArrayCC<T, SID, SizePolicy> SelfType;
1007  typedef ArrayBase<ArrayDataCC<T, AllocatorLH<T, SID>, SizePolicy> > BaseType;
1008 
1009  ArrayCC(const ValueType& defval) : BaseType(defval) {}
1010  ArrayCC(const ValueType& defval, int size) : BaseType(defval, size) {}
1011  ArrayCC(const ValueType& defval, const SizePolicyType& p) : BaseType(defval) { SetSizePolicy(p); }
1012  ArrayCC(const SelfType& a) : BaseType(a) {}
1013  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
1014 };
1015 
1016 } // Scaleform
1017 
1018 // Redefine operator 'new' if necessary.
1019 #if defined(KY_DEFINE_NEW)
1020 #define new KY_DEFINE_NEW
1021 #endif
1022 
1023 #endif
Definition: gamekitcrowddispersion.h:20
Vec2f operator*(KyFloat32 s, const Vec2f &v)
Multiplies the X and Y coordinates of v by s.
Definition: vec2f.h:184