gwnavruntime/kernel/SF_ArrayUnsafe.h Source File

SF_ArrayUnsafe.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_ArrayUnsafe.h
11 Content : Template implementation for GArrayUnsafe
12 Created : 2008
13 Authors : MaximShemanarev
14 
15 **************************************************************************/
16 
17 #ifndef INC_KY_Kernel_ArrayUnsafe_H
18 #define INC_KY_Kernel_ArrayUnsafe_H
19 
21 
22 namespace Kaim {
23 
24 // ***** ArrayUnsafeBase
25 //
26 // A simple and unsafe modification of the array. It DOES NOT automatically
27 // resizes the array on PushBack, Append and so on!
28 //
29 // Functions Reserve() and Resize() always result in losing all data!
30 // It's used in performance critical algorithms, such as tessellation,
31 // where the maximal number of elements is known in advance. There's no
32 // check for memory overrun, but the address persistence is guaranteed.
33 // Use with CAUTION!
34 //
35 // The code of this class template was taken from the Anti-Grain Geometry
36 // Project and modified for the use by Scaleform.
37 // Permission to use without restrictions is hereby granted to
38 // Scaleform Corp. by the author of Anti-Grain Geometry Project.
39 //------------------------------------------------------------------------
40 template<class T, class Allocator> class ArrayUnsafeBase
41 {
42 public:
43  typedef ArrayUnsafeBase<T, Allocator> SelfType;
44  typedef T ValueType;
45  typedef Allocator AllocatorType;
46 
47  ~ArrayUnsafeBase()
48  {
49  Allocator::DestructArray(Data, Size);
50  Allocator::Free(Data);
51  }
52 
53  ArrayUnsafeBase() : Data(0), Size(0), Capacity(0) {}
54 
55  ArrayUnsafeBase(UPInt capacity) :
56  Data((T*)Allocator::Alloc(this, Capacity * sizeof(T), __FILE__, __LINE__)),
57  Size(0),
58  Capacity(capacity)
59  {}
60 
61  ArrayUnsafeBase(const SelfType& v) :
62  Data(Capacity ? (T*)Allocator::Alloc(this, Capacity * sizeof(T), __FILE__, __LINE__) : 0),
63  Size(v.Size),
64  Capacity(v.Capacity)
65  {
66  if(Size)
67  Allocator::CopyArrayForward(Data, v.Data, Size);
68  }
69 
70  void Clear()
71  {
72  Allocator::DestructArray(Data, Size);
73  Size = 0;
74  }
75 
76  void ClearAndRelease()
77  {
78  Allocator::DestructArray(Data, Size);
79  Allocator::Free(Data);
80  Size = Capacity = 0;
81  Data = 0;
82  }
83 
84  void CutAt(UPInt newSize)
85  {
86  if(newSize < Size)
87  {
88  Allocator::DestructArray(Data + newSize, Size - newSize);
89  Size = newSize;
90  }
91  }
92 
93  // Set new capacity. All data is lost, size is set to zero.
94  void Reserve(UPInt cap, UPInt extraTail=0)
95  {
96  Allocator::DestructArray(Data, Size);
97  if(cap > Capacity)
98  {
99  Allocator::Free(Data);
100  Capacity = cap + extraTail;
101  Data = Capacity ? (T*)Allocator::Alloc(this, Capacity * sizeof(T), __FILE__, __LINE__) : 0;
102  }
103  Size = 0;
104  }
105 
106  UPInt GetCapacity() const
107  {
108  return Capacity;
109  }
110 
111  UPInt GetNumBytes() const
112  {
113  return GetCapacity() * sizeof(ValueType);
114  }
115 
116  // Allocate n elements. All data is lost,
117  // but elements can be accessed in range 0...size-1.
118  void Resize(UPInt size, UPInt extraTail=0)
119  {
120  Reserve(size, extraTail);
121  Size = size;
122  Allocator::ConstructArray(Data, size);
123  }
124 
125  void Zero()
126  {
127  memset(Data, 0, sizeof(T) * Size);
128  }
129 
130  void PushBack(const T& v)
131  {
132  Allocator::Construct(&Data[Size++], v);
133  }
134 
135  template<class S>
136  void PushBackAlt(const S& val)
137  {
138  Allocator::ConstructAlt(&Data[Size++], val);
139  }
140 
141  void InsertAt(UPInt pos, const T& val)
142  {
143  if(pos >= Size)
144  {
145  PushBack(val);
146  }
147  else
148  {
149  Allocator::Construct(Data + Size);
150  Allocator::CopyArrayBackward(Data + pos + 1, Data + pos, Size - pos);
151  Allocator::Construct(Data + pos, val);
152  ++Size;
153  }
154  }
155 
156  // Removing an element from the array is an expensive operation!
157  // It compacts only after removing the last element.
158  void RemoveAt(UPInt pos)
159  {
160  if (Size == 1)
161  {
162  Clear();
163  }
164  else
165  {
166  Allocator::Destruct(Data + pos);
167  Allocator::CopyArrayForward(
168  Data + pos,
169  Data + pos + 1,
170  Size - 1 - pos);
171  --Size;
172  }
173  }
174 
175  UPInt GetSize() const
176  {
177  return Size;
178  }
179 
180  const SelfType& operator = (const SelfType& v)
181  {
182  if(&v != this)
183  {
184  Resize(v.Size);
185  if(Size) Allocator::CopyArrayForward(Data, v.Data, Size);
186  }
187  return *this;
188  }
189 
190  const T& operator [] (UPInt i) const { return Data[i]; }
191  T& operator [] (UPInt i) { return Data[i]; }
192  const T& At(UPInt i) const { return Data[i]; }
193  T& At(UPInt i) { return Data[i]; }
194  T ValueAt(UPInt i) const { return Data[i]; }
195 
196  const T* GetDataPtr() const { return Data; }
197  T* GetDataPtr() { return Data; }
198 
199 private:
200  T* Data;
201  UPInt Size;
202  UPInt Capacity;
203 };
204 
205 
206 
207 // ***** ArrayUnsafePOD
208 //
209 // General purpose "unsafe" array for movable objects that DOES NOT require
210 // construction/destruction. Constructors and destructors are not called!
211 // Global heap is in use.
212 //------------------------------------------------------------------------
213 template<class T, int SID=Stat_Default_Mem>
214 class ArrayUnsafePOD :
215  public ArrayUnsafeBase<T, AllocatorGH_POD<T, SID> >
216 {
217 public:
218  typedef T ValueType;
219  typedef AllocatorGH_POD<T, SID> AllocatorType;
220  typedef ArrayUnsafeBase<T, AllocatorType> BaseType;
221  typedef ArrayUnsafePOD<T, SID> SelfType;
222  ArrayUnsafePOD() : BaseType() {}
223  ArrayUnsafePOD(UPInt capacity) : BaseType(capacity) {}
224  ArrayUnsafePOD(const SelfType& v) : BaseType(v) {}
225  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
226 };
227 
228 
229 // ***** ArrayUnsafe
230 //
231 // General purpose "unsafe" array for movable objects that require explicit
232 // construction/destruction. Global heap is in use.
233 //------------------------------------------------------------------------
234 template<class T, int SID=Stat_Default_Mem>
235 class ArrayUnsafe :
236  public ArrayUnsafeBase<T, AllocatorGH<T, SID> >
237 {
238 public:
239  typedef T ValueType;
240  typedef AllocatorGH<T, SID> AllocatorType;
241  typedef ArrayUnsafeBase<T, AllocatorType> BaseType;
242  typedef ArrayUnsafe<T, SID> SelfType;
243  ArrayUnsafe() : BaseType() {}
244  ArrayUnsafe(UPInt capacity) : BaseType(capacity) {}
245  ArrayUnsafe(const SelfType& v) : BaseType(v) {}
246  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
247 };
248 
249 
250 // ***** ArrayUnsafeBase
251 //
252 // General purpose "unsafe" array for movable objects that DOES NOT require
253 // construction/destruction. Constructors and destructors are not called!
254 // Local heap is in use.
255 //------------------------------------------------------------------------
256 template<class T, int SID=Stat_Default_Mem>
257 class ArrayUnsafeLH_POD :
258  public ArrayUnsafeBase<T, AllocatorLH_POD<T, SID> >
259 {
260 public:
261  typedef T ValueType;
262  typedef AllocatorLH_POD<T, SID> AllocatorType;
263  typedef ArrayUnsafeBase<T, AllocatorType> BaseType;
264  typedef ArrayUnsafeLH_POD<T, SID> SelfType;
265  ArrayUnsafeLH_POD() : BaseType() {}
266  ArrayUnsafeLH_POD(UPInt capacity) : BaseType(capacity) {}
267  ArrayUnsafeLH_POD(const SelfType& v) : BaseType(v) {}
268  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
269 };
270 
271 
272 // ***** ArrayUnsafeLH
273 //
274 // General purpose "unsafe" array for movable objects that require explicit
275 // construction/destruction. Local heap is in use.
276 //------------------------------------------------------------------------
277 template<class T, int SID=Stat_Default_Mem>
278 class ArrayUnsafeLH :
279  public ArrayUnsafeBase<T, AllocatorLH<T, SID> >
280 {
281 public:
282  typedef T ValueType;
283  typedef AllocatorLH<T, SID> AllocatorType;
284  typedef ArrayUnsafeBase<T, AllocatorType> BaseType;
285  typedef ArrayUnsafeLH<T, SID> SelfType;
286  ArrayUnsafeLH() : BaseType() {}
287  ArrayUnsafeLH(UPInt capacity) : BaseType(capacity) {}
288  ArrayUnsafeLH(const SelfType& v) : BaseType(v) {}
289  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
290 };
291 
292 } // Scaleform
293 
294 #endif
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17