TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SmallArray.h
Go to the documentation of this file.
1 
10 #ifndef G3D_SmallArray_h
11 #define G3D_SmallArray_h
12 
13 #include "G3D/platform.h"
14 #include "G3D/Array.h"
15 #include "G3D/MemoryManager.h"
16 
17 namespace G3D {
18 
22 template<class T, int N>
23 class SmallArray {
24 private:
25  int m_size;
26 
28  T m_embedded[N];
29 
32 
33 public:
34 
35  SmallArray() : m_size(0) {}
36 
37  inline int size() const {
38  return m_size;
39  }
40 
41  void resize(int n, bool shrinkIfNecessary = true) {
42  m_rest.resize(std::max(0, n - N), shrinkIfNecessary);
43  m_size = n;
44  }
45 
46  void clear(bool shrinkIfNecessary = true) {
47  resize(0, shrinkIfNecessary);
48  }
49 
51  clear();
52  m_rest.clearAndSetMemoryManager(m);
53  }
54 
55  inline T& operator[](int i) {
56  debugAssert(i < m_size && i >= 0);
57  if (i < N) {
58  return m_embedded[i];
59  } else {
60  return m_rest[i - N];
61  }
62  }
63 
64  inline const T& operator[](int i) const {
65  debugAssert(i < m_size && i >= 0);
66  if (i < N) {
67  return m_embedded[i];
68  } else {
69  return m_rest[i - N];
70  }
71  }
72 
73  inline void push(const T& v) {
74  ++m_size;
75  if (m_size <= N) {
76  m_embedded[m_size - 1] = v;
77  } else {
78  m_rest.append(v);
79  }
80  }
81 
82  inline void append(const T& v) {
83  push(v);
84  }
85 
86  inline void append(const T& v, const T& v2) {
87  push(v);
88  push(v2);
89  }
90 
91  inline void append(const T& v, const T& v2, const T& v3) {
92  push(v);
93  push(v2);
94  push(v3);
95  }
96 
97  inline void append(const T& v, const T& v2, const T& v3, const T& v4) {
98  push(v);
99  push(v2);
100  push(v3);
101  push(v4);
102  }
103 
105  int findIndex(const T& v) {
106  for (int i = 0; i < N; ++i) {
107  if (m_embedded[i] == v) {
108  return i;
109  }
110  }
111 
112  return m_rest.findIndex(v) + N;
113  }
114 
115  void fastRemove(int i, bool shrinkIfNecessary = false) {
116  debugAssert(i < m_size && i >= 0);
117  if (i < N) {
118  if (m_size <= N) {
119  // Exclusively embedded
120  m_embedded[i] = m_embedded[m_size - 1];
121  } else {
122  // Move one down from the rest array
123  m_embedded[i] = m_rest.pop();
124  }
125  } else {
126  // Removing from the rest array
127  m_rest.fastRemove(i - N, shrinkIfNecessary);
128  }
129  --m_size;
130  }
131 
132  T pop() {
133  debugAssert(m_size > 0);
134  if (m_size <= N) {
135  // Popping from embedded, don't need a temporary
136  --m_size;
137  return m_embedded[m_size];
138  } else {
139  // Popping from rest
140  --m_size;
141  return m_rest.pop();
142  }
143  }
144 
145  inline void popDiscard() {
146  debugAssert(m_size > 0);
147  if (m_size > N) {
148  m_rest.popDiscard();
149  }
150  --m_size;
151  }
152 
153  inline T& next() {
154  ++m_size;
155  if (m_size <= N) {
156  return m_embedded[m_size - 1];
157  } else {
158  return m_rest.next();
159  }
160  }
161 
162  bool contains(const T& value) const {
163  for (int i = std::min(m_size, N) - 1; i >= 0; --i) {
164  if (m_embedded[i] == value) {
165  return true;
166  }
167  }
168  return m_rest.contains(value);
169  }
170 
171  template<int MIN_ELEMENTS>
173  resize(src.size());
174  for (int i = 0; i < src.size(); ++i) {
175  (*this)[i] = src[i];
176  }
177  return *this;
178  }
179 
180  inline const T& last() const {
181  return (*this)[size() - 1];
182  }
183 
184  inline T& last() {
185  return (*this)[size() - 1];
186  }
187 };
188 
189 }
190 #endif
void fastRemove(int i, bool shrinkIfNecessary=false)
Definition: SmallArray.h:115
T pop(bool shrinkUnderlyingArrayIfNecessary=true)
Definition: Array.h:837
T m_embedded[N]
Definition: SmallArray.h:28
T & last()
Definition: SmallArray.h:184
void resize(size_t n, bool shrinkIfNecessary=true)
Definition: Array.h:490
Definition: SmallArray.h:23
SmallArray< T, N > & operator=(const Array< T, MIN_ELEMENTS > &src)
Definition: SmallArray.h:172
bool contains(const T &value) const
Definition: SmallArray.h:162
void clear(bool shrinkIfNecessary=true)
Definition: SmallArray.h:46
Definition: AABox.h:25
int findIndex(const T &v)
Definition: SmallArray.h:105
T & next()
Definition: SmallArray.h:153
void popDiscard()
Definition: SmallArray.h:145
shared_ptr< class MemoryManager > Ref
Definition: MemoryManager.h:31
void popDiscard(bool shrinkUnderlyingArrayIfNecessary=false)
Definition: Array.h:846
T max(const T &x, const T &y)
Definition: g3dmath.h:320
T & next()
Definition: Array.h:762
void clearAndSetMemoryManager(MemoryManager::Ref &m)
Definition: SmallArray.h:50
T min(const T &x, const T &y)
Definition: g3dmath.h:305
void fastRemove(int index, bool shrinkIfNecessary=false)
Definition: Array.h:446
void resize(int n, bool shrinkIfNecessary=true)
Definition: SmallArray.h:41
const T & last() const
Definition: SmallArray.h:180
#define debugAssert(exp)
Definition: debugAssert.h:160
const T & operator[](int i) const
Definition: SmallArray.h:64
SmallArray()
Definition: SmallArray.h:35
T pop()
Definition: SmallArray.h:132
void append(const T &v)
Definition: SmallArray.h:82
void clearAndSetMemoryManager(const MemoryManager::Ref &m)
Definition: Array.h:411
int size() const
Definition: Array.h:430
bool contains(const T &e) const
Definition: Array.h:732
int findIndex(const T &value) const
Definition: Array.h:1009
void append(const T &v, const T &v2)
Definition: SmallArray.h:86
int size() const
Definition: SmallArray.h:37
T & operator[](int i)
Definition: SmallArray.h:55
Array< T > m_rest
Definition: SmallArray.h:31
const FieldDescriptor value
Definition: descriptor.h:1522
void append(const T &value)
Definition: Array.h:583
void append(const T &v, const T &v2, const T &v3)
Definition: SmallArray.h:91
int m_size
Definition: SmallArray.h:25
void append(const T &v, const T &v2, const T &v3, const T &v4)
Definition: SmallArray.h:97
void push(const T &v)
Definition: SmallArray.h:73