gwnavruntime/containers/kyarray.h Source File

kyarray.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 #pragma once
8 
11 
12 namespace Kaim
13 {
14 
15 // Array generic interface
16 //
17 // **************************
18 // - UPInt GetSize() const;
19 // - bool IsEmpty() const;
20 // - UPInt GetCapacity() const;
21 // - UPInt GetNumBytes() const;
22 //
23 // void ClearAndRelease();
24 // void Clear();
25 // void Resize(UPInt newSize);
26 //
27 // void Reserve(UPInt newCapacity);
28 //
29 // // Basic access.
30 // ValueType& At(UPInt index);
31 // const ValueType& At(UPInt index); const
32 // ValueType ValueAt(UPInt index) const;
33 // ValueType& operator [] (UPInt index);
34 // const ValueType& operator [] (UPInt index) const;
35 //
36 // // Raw pointer to the data. Use with caution!
37 // const ValueType* GetDataPtr() const;
38 // ValueType* GetDataPtr();
39 //
40 // // Insert the given element at the end of the array.
41 // void PushBack(const ValueType& val);
42 // template<class S>
43 // void PushBackAlt(const S& val);
44 //
45 // // Remove the last element.
46 // void PopBack(UPInt count = 1);
47 // ValueType& PushDefault();
48 //
49 // ValueType Pop();
50 //
51 // // Access the first element.
52 // ValueType& Front();
53 // const ValueType& Front() const;
54 //
55 // // Access the last element.
56 // ValueType& Back();
57 // const ValueType& Back() const;
58 //
59 // // Array copy. Copies the contents of a into this array.
60 // const SelfType& operator = (const SelfType& a);
61 //
62 // // Removing multiple elements from the array.
63 // void RemoveMultipleAt(UPInt index, UPInt num);
64 //
65 // // Removing an element from the array is an expensive operation!
66 // // It compacts only after removing the last element.
67 // void RemoveAt(UPInt index);
68 // // Insert the given object at the given index shifting all the elements up.
69 // void InsertAt(UPInt index, const ValueType& val = ValueType());
70 // // Insert the given object at the given index shifting all the elements up.
71 // void InsertMultipleAt(UPInt index, UPInt num, const ValueType& val = ValueType());
72 // // Append the given data to the array.
73 // void Append(const SelfType& other);
74 // // Append the given data to the array.
75 // void Append(const ValueType other[], UPInt count);
76 //
77 // bool DoesContain(const ValueType& value) const;
78 // void RemoveConsecutiveDuplicates();
79 //
80 // **************************
81 // // Navigation array different type
82 // - KyArray :
83 // General purpose array for movable objects that require explicit
84 // construction/destruction. Global heap is in use.
85 // - KyArrayPOD
86 // General purpose array for movable objects that DOES NOT require
87 // construction/destruction. Constructors and destructors are not called!
88 // Global heap is in use.
89 // - KyArrayLH
90 // General purpose array for movable objects that require explicit
91 // construction/destruction. Local heap is in use.
92 // - KyArrayLH_POD
93 // General purpose array for movable objects that DOES NOT require
94 // construction/destruction. Constructors and destructors are not called!
95 // Local heap is in use.
96 // - KyArrayDH
97 // General purpose array for movable objects that require explicit
98 // construction/destruction. Dynamic heap is in use.
99 // - KyArrayDH_POD
100 // General purpose array for movable objects that DOES NOT require
101 // construction/destruction. Constructors and destructors are not called!
102 // Dynamic heap is in use.
103 
104 namespace ArrayAlg
105 {
106 
107 template <typename ArrayT, typename ValueT>
108 bool DoesContain(const ArrayT& arr, const ValueT& value)
109 {
110  for (UPInt i = 0; i < arr.GetSize(); ++i)
111  {
112  if (arr[i] == value)
113  return true;
114  }
115  return false;
116 }
117 
118 template <typename ArrayT>
119 void RemoveConsecutiveDuplicates(ArrayT& arr)
120 {
121  if (arr.GetCount() == 0)
122  return;
123 
124  UPInt writeIdx = 0;
125  for (UPInt readIdx = 1; readIdx < arr.GetSize(); ++readIdx)
126  {
127  if (arr[writeIdx] != arr[readIdx])
128  {
129  ++writeIdx;
130  arr[writeIdx] = arr[readIdx];
131  }
132  }
133  arr.Resize(writeIdx + 1);
134 }
135 
136 template<typename T1, typename T2>
137 bool IsEqual(const T1& a1, const T2& a2)
138 {
139  if (a1.GetCount() != a2.GetCount())
140  return false;
141 
142  for (KyUInt32 i = 0; i < a1.GetCount(); ++i)
143  {
144  if (a1[i] != a2[i])
145  return false;
146  }
147 
148  return true;
149 }
150 }
151 
152 // ---- KyArrayDefaultPolicy ----
155 typedef ArrayConstPolicy<0, 4, true> KyArrayDefaultPolicy;
156 
157 
158 // ---- KyArray ----
161 template<class T, int SID=Stat_Default_Mem, class SizePolicy=KyArrayDefaultPolicy>
162 class KyArray : public Array<T, SID, SizePolicy>
163 {
164 public:
165  // ---------------------------------- Public typedef ----------------------------------
166 
167  typedef T ValueType;
169  typedef Array<T, SID, SizePolicy> BaseType;
170 
171  // ------------------------------ Functions -----------------------------
172 
173  KyArray() : BaseType() {}
174  explicit KyArray(int size) : BaseType(size) {}
175  KyArray(const SelfType& a) : BaseType(a) {}
176  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
177 
178  KyUInt32 GetCount() const { return (KyUInt32)BaseType::GetSize(); }
179 
180  bool operator==(const SelfType& other) const { return ArrayAlg::IsEqual(*this, other); }
181  bool operator!=(const SelfType& other) { return !operator==(other); }
182 
183  bool DoesContain(const ValueType& value) const { return ArrayAlg::DoesContain<SelfType,ValueType>(*this, value); }
184  void RemoveConsecutiveDuplicates() { ArrayAlg::RemoveConsecutiveDuplicates<SelfType>(*this); }
185  void StealFrom(SelfType& other) { this->Data.StealFrom(other.Data); }
186 };
187 
188 
189 // ---- KyArrayPOD ----
193 template<class T, int SID=Stat_Default_Mem, class SizePolicy=KyArrayDefaultPolicy>
194 class KyArrayPOD : public ArrayPOD<T, SID, SizePolicy>
195 {
196 public:
197  // ---------------------------------- Public typedef ----------------------------------
198 
199  typedef T ValueType;
201  typedef ArrayPOD<T, SID, SizePolicy> BaseType;
202 
203 
204  // ------------------------------ Functions -----------------------------
205 
206  KyArrayPOD() : BaseType() {}
207  explicit KyArrayPOD(int size) : BaseType(size) {}
208  KyArrayPOD(const SelfType& a) : BaseType(a) {}
209  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
210 
211  KyUInt32 GetCount() const { return (KyUInt32)BaseType::GetSize(); }
212 
213  bool operator==(const SelfType& other) const { return ArrayAlg::IsEqual(*this, other); }
214  bool operator!=(const SelfType& other) { return !operator==(other); }
215 
216  bool DoesContain(const ValueType& value) const { return ArrayAlg::DoesContain<SelfType,ValueType>(*this, value); }
217  void RemoveConsecutiveDuplicates() { ArrayAlg::RemoveConsecutiveDuplicates<SelfType>(*this); }
218  void StealFrom(SelfType& other) { this->Data.StealFrom(other.Data); }
219 };
220 
221 // ---- KyArrayLH ----
224 template<class T, int SID=Stat_Default_Mem, class SizePolicy=KyArrayDefaultPolicy>
225 class KyArrayLH : public ArrayLH<T, SID, SizePolicy>
226 {
227 public:
228  // ---------------------------------- Public typedef ----------------------------------
229 
230  typedef T ValueType;
232  typedef ArrayLH<T, SID, SizePolicy> BaseType;
233 
234 
235  // ------------------------------ Functions -----------------------------
236 
237  KyArrayLH() : BaseType() {}
238  explicit KyArrayLH(int size) : BaseType(size) {}
239  KyArrayLH(const SelfType& a) : BaseType(a) {}
240  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
241 
242  KyUInt32 GetCount() const { return (KyUInt32)BaseType::GetSize(); }
243 
244  bool operator==(const SelfType& other) const { return ArrayAlg::IsEqual(*this, other); }
245  bool operator!=(const SelfType& other) { return !operator==(other); }
246 
247  bool DoesContain(const ValueType& value) const { return ArrayAlg::DoesContain<SelfType,ValueType>(*this, value); }
248  void RemoveConsecutiveDuplicates() { ArrayAlg::RemoveConsecutiveDuplicates<SelfType>(*this); }
249  void StealFrom(SelfType& other) { this->Data.StealFrom(other.Data); }
250 };
251 
252 // ---- KyArrayCPP ----
253 // General purpose fully C++ compliant array. Can be used with non-movable data.
254 // Global heap is in use.
255 template<class T, int SID=Stat_Default_Mem, class SizePolicy=KyArrayDefaultPolicy>
256 class KyArrayCPP : public ArrayCPP<T, SID, SizePolicy>
257 {
258 public:
259  // ---------------------------------- Public typedef ----------------------------------
260 
261  typedef T ValueType;
262  typedef KyArrayCPP<T, SID, SizePolicy> SelfType;
263  typedef ArrayCPP<T, SID, SizePolicy> BaseType;
264 
265 
266  // ------------------------------ Functions -----------------------------
267 
268  KyArrayCPP() : BaseType() {}
269  explicit KyArrayCPP(int size) : BaseType(size) {}
270  KyArrayCPP(const SelfType& a) : BaseType(a) {}
271  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
272 
273  KyUInt32 GetCount() const { return (KyUInt32)BaseType::GetSize(); }
274 
275  bool operator==(const SelfType& other) const { return ArrayAlg::IsEqual(*this, other); }
276  bool operator!=(const SelfType& other) { return !operator==(other); }
277 
278  bool DoesContain(const ValueType& value) const { return ArrayAlg::DoesContain<SelfType,ValueType>(*this, value); }
279  void RemoveConsecutiveDuplicates() { ArrayAlg::RemoveConsecutiveDuplicates<SelfType>(*this); }
280  void StealFrom(SelfType& other) { this->Data.StealFrom(other.Data); }
281 };
282 
283 // ---- KyArrayLH_POD ----
287 template<class T, int SID=Stat_Default_Mem, class SizePolicy=KyArrayDefaultPolicy>
288 class KyArrayLH_POD : public ArrayLH_POD<T, SID, SizePolicy>
289 {
290 public:
291  // ---------------------------------- Public typedef ----------------------------------
292 
293  typedef T ValueType;
294  typedef KyArrayLH_POD<T, SID, SizePolicy> SelfType;
295  typedef ArrayLH_POD<T, SID, SizePolicy> BaseType;
296 
297 
298  // ------------------------------ Functions -----------------------------
299 
300  KyArrayLH_POD() : BaseType() {}
301  explicit KyArrayLH_POD(int size) : BaseType(size) {}
302  KyArrayLH_POD(const SelfType& a) : BaseType(a) {}
303  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
304 
305  KyUInt32 GetCount() const { return (KyUInt32)BaseType::GetSize(); }
306 
307  bool operator==(const SelfType& other) const { return ArrayAlg::IsEqual(*this, other); }
308  bool operator!=(const SelfType& other) { return !operator==(other); }
309 
310  bool DoesContain(const ValueType& value) const { return ArrayAlg::DoesContain<SelfType,ValueType>(*this, value); }
311  void RemoveConsecutiveDuplicates() { ArrayAlg::RemoveConsecutiveDuplicates<SelfType>(*this); }
312  void StealFrom(SelfType& other) { this->Data.StealFrom(other.Data); }
313 };
314 
315 // ---- KyArrayLH_CPP ----
316 // General purpose fully C++ compliant array. Can be used with non-movable data.
317 // Local heap is in use.
318 template<class T, int SID=Stat_Default_Mem, class SizePolicy=KyArrayDefaultPolicy>
319 class KyArrayLH_CPP : public ArrayLH_CPP<T, SID, SizePolicy>
320 {
321 public:
322  // ---------------------------------- Public typedef ----------------------------------
323 
324  typedef T ValueType;
325  typedef KyArrayLH_CPP<T, SID, SizePolicy> SelfType;
326  typedef ArrayLH_CPP<T, SID, SizePolicy> BaseType;
327 
328 
329  // ------------------------------ Functions -----------------------------
330 
331  KyArrayLH_CPP() : BaseType() {}
332  explicit KyArrayLH_CPP(int size) : BaseType(size) {}
333  KyArrayLH_CPP(const SelfType& a) : BaseType(a) {}
334  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
335 
336  KyUInt32 GetCount() const { return (KyUInt32)BaseType::GetSize(); }
337 
338  bool operator==(const SelfType& other) const { return ArrayAlg::IsEqual(*this, other); }
339  bool operator!=(const SelfType& other) { return !operator==(other); }
340 
341  bool DoesContain(const ValueType& value) const { return ArrayAlg::DoesContain<SelfType,ValueType>(*this, value); }
342  void RemoveConsecutiveDuplicates() { ArrayAlg::RemoveConsecutiveDuplicates<SelfType>(*this); }
343  void StealFrom(SelfType& other) { this->Data.StealFrom(other.Data); }
344 };
345 
346 // ---- KyArrayDH ----
349 template<class T, int SID=Stat_Default_Mem, class SizePolicy=KyArrayDefaultPolicy>
350 class KyArrayDH : public ArrayDH<T, SID, SizePolicy>
351 {
352 public:
353  // ---------------------------------- Public typedef ----------------------------------
354 
355  typedef T ValueType;
356  typedef KyArrayDH<T, SID, SizePolicy> SelfType;
357  typedef ArrayDH<T, SID, SizePolicy> BaseType;
358 
359 
360  // ------------------------------ Functions -----------------------------
361 
362  explicit KyArrayDH(MemoryHeap* heap) : BaseType(heap) {}
363  KyArrayDH(MemoryHeap* heap, int size) : BaseType(heap, size) {}
364  KyArrayDH(MemoryHeap* heap, const SelfType& a) : BaseType(heap, a) {}
365  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
366 
367  KyUInt32 GetCount() const { return (KyUInt32)BaseType::GetSize(); }
368 
369  bool operator==(const SelfType& other) const { return ArrayAlg::IsEqual(*this, other); }
370  bool operator!=(const SelfType& other) { return !operator==(other); }
371 
372  bool DoesContain(const ValueType& value) const { return ArrayAlg::DoesContain<SelfType,ValueType>(*this, value); }
373  void RemoveConsecutiveDuplicates() { ArrayAlg::RemoveConsecutiveDuplicates<SelfType>(*this); }
374  void StealFrom(SelfType& other) { this->Data.StealFrom(other.Data); }
375 };
376 
377 
378 // ---- KyArrayDH_POD ----
382 template<class T, int SID=Stat_Default_Mem, class SizePolicy=KyArrayDefaultPolicy>
383 class KyArrayDH_POD : public ArrayDH_POD<T, SID, SizePolicy>
384 {
385 public:
386  // ---------------------------------- Public typedef ----------------------------------
387 
388  typedef T ValueType;
389  typedef KyArrayDH_POD<T, SID, SizePolicy> SelfType;
390  typedef ArrayDH_POD<T, SID, SizePolicy> BaseType;
391 
392 
393  // ------------------------------ Functions -----------------------------
394 
395  explicit KyArrayDH_POD(MemoryHeap* heap) : BaseType(heap) {}
396  KyArrayDH_POD(MemoryHeap* heap, int size) : BaseType(heap, size) {}
397  KyArrayDH_POD(MemoryHeap* heap, const SelfType& a) : BaseType(heap, a) {}
398  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
399 
400  KyUInt32 GetCount() const { return (KyUInt32)BaseType::GetSize(); }
401 
402  bool operator==(const SelfType& other) const { return ArrayAlg::IsEqual(*this, other); }
403  bool operator!=(const SelfType& other) { return !operator==(other); }
404 
405  bool DoesContain(const ValueType& value) const { return ArrayAlg::DoesContain<SelfType,ValueType>(*this, value); }
406  void RemoveConsecutiveDuplicates() { ArrayAlg::RemoveConsecutiveDuplicates<SelfType>(*this); }
407  void StealFrom(SelfType& other) { this->Data.StealFrom(other.Data); }
408 };
409 
410 
411 // ---- KyArrayDH_CPP ----
412 // General purpose fully C++ compliant array. Can be used with non-movable data.
413 // Dynamic heap is in use
414 template<class T, int SID=Stat_Default_Mem, class SizePolicy=KyArrayDefaultPolicy>
415 class KyArrayDH_CPP : public ArrayDH_CPP<T, SID, SizePolicy>
416 {
417 public:
418  // ---------------------------------- Public typedef ----------------------------------
419 
420  typedef T ValueType;
421  typedef KyArrayDH_CPP<T, SID, SizePolicy> SelfType;
422  typedef ArrayDH_CPP<T, SID, SizePolicy> BaseType;
423 
424 
425  // ------------------------------ Functions -----------------------------
426 
427  explicit KyArrayDH_CPP(MemoryHeap* heap) : BaseType(heap) {}
428  KyArrayDH_CPP(MemoryHeap* heap, int size) : BaseType(heap, size) {}
429  KyArrayDH_CPP(MemoryHeap* heap, const SelfType& a) : BaseType(heap, a) {}
430  const SelfType& operator=(const SelfType& a) { BaseType::operator=(a); return *this; }
431 
432  KyUInt32 GetCount() const { return (KyUInt32)BaseType::GetSize(); }
433 
434  bool operator==(const SelfType& other) const { return ArrayAlg::IsEqual(*this, other); }
435  bool operator!=(const SelfType& other) { return !operator==(other); }
436 
437  bool DoesContain(const ValueType& value) const { return ArrayAlg::DoesContain<SelfType,ValueType>(*this, value); }
438  void RemoveConsecutiveDuplicates() { ArrayAlg::RemoveConsecutiveDuplicates<SelfType>(*this); }
439  void StealFrom(SelfType& other) { this->Data.StealFrom(other.Data); }
440 };
441 
442 } // namespace Kaim
443 
General purpose array for movable objects that DOES NOT require construction/destruction.
Definition: kyarray.h:194
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
General purpose array for movable objects that require explicit construction/destruction.
Definition: kyarray.h:162
General purpose array for movable objects that DOES NOT require construction/destruction.
Definition: kyarray.h:383
General purpose array for movable objects that DOES NOT require construction/destruction.
Definition: kyarray.h:288
ArrayConstPolicy< 0, 4, true > KyArrayDefaultPolicy
Default resize behavior for Autodesk Navigation array.
Definition: kyarray.h:155
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
General purpose array for movable objects that require explicit construction/destruction.
Definition: kyarray.h:350
General purpose array for movable objects that require explicit construction/destruction.
Definition: kyarray.h:225