gwnavruntime/kernel/SF_Array.h Source File

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