gwnavruntime/containers/circulararray.h Source File

circulararray.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 #pragma once
9 
13 
14 namespace Kaim
15 {
16 
17 template <class T, int SID=Stat_Default_Mem>
18 class CircularArray
19 {
21  KY_CLASS_WITHOUT_COPY(CircularArray)
22 public:
23  CircularArray(MemoryHeap* heap = nullptr);
24 
25  ~CircularArray();
26 
27  KyUInt32 GetCount() const;
28  KyUInt32 GetCapacity() const;
29 
30  bool IsEmpty() const;
31  bool IsFull() const;
32 
33  void PushBack(const T& item);
34  void PushFront(const T& item);
35 
36  void PopFront();
37  void PopNFirstElements(KyUInt32 popCount);
38  void PopBack();
39 
40 
41  const T& GetBack() const;
42  T& GetBack();
43 
44  const T& GetFront() const;
45  T& GetFront();
46 
47  T& operator [] (KyUInt32 index);
48  const T& operator [] (KyUInt32 index) const;
49 
50  void Clear();
51  void Reserve(KyUInt32 size);
52 
53 protected:
54  void ReserveInHeap(KyUInt32 size);
55 
56 private:
57  void AutoReserve() { if (IsFull()) Reserve(Max<KyUInt32>(4, 2 * GetCapacity())); }
58  void ReleaseMemory();
59  void IncreaseBeginIdx();
60  void IncreaseEndIdx();
61  void DecreaseBeginIdx();
62  void DecreaseEndIdx();
63 private:
64  T* m_buffer;
65  MemoryHeap* const m_heap;
66  KyUInt32 m_lastValidIdx; // capacity == m_lastValidIdx + 1;
67  KyUInt32 m_beginIdx;
68  KyUInt32 m_endIdx;
69  KyUInt32 m_count;
70 };
71 
72 } // namespace Kaim
73 
75 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:196
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17