TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::SmallArray< T, N > Class Template Reference

#include <SmallArray.h>

Public Member Functions

 SmallArray ()
 
int size () const
 
void resize (int n, bool shrinkIfNecessary=true)
 
void clear (bool shrinkIfNecessary=true)
 
void clearAndSetMemoryManager (MemoryManager::Ref &m)
 
T & operator[] (int i)
 
const T & operator[] (int i) const
 
void push (const T &v)
 
void append (const T &v)
 
void append (const T &v, const T &v2)
 
void append (const T &v, const T &v2, const T &v3)
 
void append (const T &v, const T &v2, const T &v3, const T &v4)
 
int findIndex (const T &v)
 
void fastRemove (int i, bool shrinkIfNecessary=false)
 
pop ()
 
void popDiscard ()
 
T & next ()
 
bool contains (const T &value) const
 
template<int MIN_ELEMENTS>
SmallArray< T, N > & operator= (const Array< T, MIN_ELEMENTS > &src)
 
const T & last () const
 
T & last ()
 

Private Attributes

int m_size
 
m_embedded [N]
 
Array< T > m_rest
 

Detailed Description

template<class T, int N>
class G3D::SmallArray< T, N >

Embeds N elements to reduce allocation time and increase memory coherence when working with arrays of arrays. Offers a limited subset of the functionality of G3D::Array.

Constructor & Destructor Documentation

template<class T, int N>
G3D::SmallArray< T, N >::SmallArray ( )
inline
35 : m_size(0) {}
int m_size
Definition: SmallArray.h:25

Member Function Documentation

template<class T, int N>
void G3D::SmallArray< T, N >::append ( const T &  v)
inline
82  {
83  push(v);
84  }
void push(const T &v)
Definition: SmallArray.h:73

+ Here is the caller graph for this function:

template<class T, int N>
void G3D::SmallArray< T, N >::append ( const T &  v,
const T &  v2 
)
inline
86  {
87  push(v);
88  push(v2);
89  }
void push(const T &v)
Definition: SmallArray.h:73
template<class T, int N>
void G3D::SmallArray< T, N >::append ( const T &  v,
const T &  v2,
const T &  v3 
)
inline
91  {
92  push(v);
93  push(v2);
94  push(v3);
95  }
void push(const T &v)
Definition: SmallArray.h:73
template<class T, int N>
void G3D::SmallArray< T, N >::append ( const T &  v,
const T &  v2,
const T &  v3,
const T &  v4 
)
inline
97  {
98  push(v);
99  push(v2);
100  push(v3);
101  push(v4);
102  }
void push(const T &v)
Definition: SmallArray.h:73
template<class T, int N>
void G3D::SmallArray< T, N >::clear ( bool  shrinkIfNecessary = true)
inline
46  {
47  resize(0, shrinkIfNecessary);
48  }
void resize(int n, bool shrinkIfNecessary=true)
Definition: SmallArray.h:41

+ Here is the caller graph for this function:

template<class T, int N>
void G3D::SmallArray< T, N >::clearAndSetMemoryManager ( MemoryManager::Ref m)
inline
50  {
51  clear();
53  }
void clear(bool shrinkIfNecessary=true)
Definition: SmallArray.h:46
void clearAndSetMemoryManager(const MemoryManager::Ref &m)
Definition: Array.h:411
Array< T > m_rest
Definition: SmallArray.h:31

+ Here is the caller graph for this function:

template<class T, int N>
bool G3D::SmallArray< T, N >::contains ( const T &  value) const
inline
162  {
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  }
T m_embedded[N]
Definition: SmallArray.h:28
T min(const T &x, const T &y)
Definition: g3dmath.h:305
bool contains(const T &e) const
Definition: Array.h:732
Array< T > m_rest
Definition: SmallArray.h:31
const FieldDescriptor value
Definition: descriptor.h:1522
int m_size
Definition: SmallArray.h:25

+ Here is the caller graph for this function:

template<class T, int N>
void G3D::SmallArray< T, N >::fastRemove ( int  i,
bool  shrinkIfNecessary = false 
)
inline
115  {
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  }
T pop(bool shrinkUnderlyingArrayIfNecessary=true)
Definition: Array.h:837
T m_embedded[N]
Definition: SmallArray.h:28
void fastRemove(int index, bool shrinkIfNecessary=false)
Definition: Array.h:446
#define debugAssert(exp)
Definition: debugAssert.h:160
Array< T > m_rest
Definition: SmallArray.h:31
int m_size
Definition: SmallArray.h:25

+ Here is the caller graph for this function:

template<class T, int N>
int G3D::SmallArray< T, N >::findIndex ( const T &  v)
inline

Find the index of v or -1 if not found

105  {
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  }
T m_embedded[N]
Definition: SmallArray.h:28
int findIndex(const T &value) const
Definition: Array.h:1009
Array< T > m_rest
Definition: SmallArray.h:31
template<class T, int N>
const T& G3D::SmallArray< T, N >::last ( ) const
inline
180  {
181  return (*this)[size() - 1];
182  }
int size() const
Definition: SmallArray.h:37
template<class T, int N>
T& G3D::SmallArray< T, N >::last ( )
inline
184  {
185  return (*this)[size() - 1];
186  }
int size() const
Definition: SmallArray.h:37
template<class T, int N>
T& G3D::SmallArray< T, N >::next ( )
inline
153  {
154  ++m_size;
155  if (m_size <= N) {
156  return m_embedded[m_size - 1];
157  } else {
158  return m_rest.next();
159  }
160  }
T m_embedded[N]
Definition: SmallArray.h:28
T & next()
Definition: Array.h:762
Array< T > m_rest
Definition: SmallArray.h:31
int m_size
Definition: SmallArray.h:25

+ Here is the caller graph for this function:

template<class T, int N>
template<int MIN_ELEMENTS>
SmallArray<T, N>& G3D::SmallArray< T, N >::operator= ( const Array< T, MIN_ELEMENTS > &  src)
inline
172  {
173  resize(src.size());
174  for (int i = 0; i < src.size(); ++i) {
175  (*this)[i] = src[i];
176  }
177  return *this;
178  }
void resize(int n, bool shrinkIfNecessary=true)
Definition: SmallArray.h:41
template<class T, int N>
T& G3D::SmallArray< T, N >::operator[] ( int  i)
inline
55  {
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  }
T m_embedded[N]
Definition: SmallArray.h:28
#define debugAssert(exp)
Definition: debugAssert.h:160
Array< T > m_rest
Definition: SmallArray.h:31
template<class T, int N>
const T& G3D::SmallArray< T, N >::operator[] ( int  i) const
inline
64  {
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  }
T m_embedded[N]
Definition: SmallArray.h:28
#define debugAssert(exp)
Definition: debugAssert.h:160
Array< T > m_rest
Definition: SmallArray.h:31
template<class T, int N>
T G3D::SmallArray< T, N >::pop ( )
inline
132  {
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  }
T pop(bool shrinkUnderlyingArrayIfNecessary=true)
Definition: Array.h:837
T m_embedded[N]
Definition: SmallArray.h:28
#define debugAssert(exp)
Definition: debugAssert.h:160
Array< T > m_rest
Definition: SmallArray.h:31
int m_size
Definition: SmallArray.h:25

+ Here is the caller graph for this function:

template<class T, int N>
void G3D::SmallArray< T, N >::popDiscard ( )
inline
145  {
146  debugAssert(m_size > 0);
147  if (m_size > N) {
148  m_rest.popDiscard();
149  }
150  --m_size;
151  }
void popDiscard(bool shrinkUnderlyingArrayIfNecessary=false)
Definition: Array.h:846
#define debugAssert(exp)
Definition: debugAssert.h:160
Array< T > m_rest
Definition: SmallArray.h:31
int m_size
Definition: SmallArray.h:25
template<class T, int N>
void G3D::SmallArray< T, N >::push ( const T &  v)
inline
73  {
74  ++m_size;
75  if (m_size <= N) {
76  m_embedded[m_size - 1] = v;
77  } else {
78  m_rest.append(v);
79  }
80  }
T m_embedded[N]
Definition: SmallArray.h:28
Array< T > m_rest
Definition: SmallArray.h:31
void append(const T &value)
Definition: Array.h:583
int m_size
Definition: SmallArray.h:25

+ Here is the caller graph for this function:

template<class T, int N>
void G3D::SmallArray< T, N >::resize ( int  n,
bool  shrinkIfNecessary = true 
)
inline
41  {
42  m_rest.resize(std::max(0, n - N), shrinkIfNecessary);
43  m_size = n;
44  }
void resize(size_t n, bool shrinkIfNecessary=true)
Definition: Array.h:490
T max(const T &x, const T &y)
Definition: g3dmath.h:320
Array< T > m_rest
Definition: SmallArray.h:31
int m_size
Definition: SmallArray.h:25

+ Here is the caller graph for this function:

template<class T, int N>
int G3D::SmallArray< T, N >::size ( ) const
inline
37  {
38  return m_size;
39  }
int m_size
Definition: SmallArray.h:25

+ Here is the caller graph for this function:

Member Data Documentation

template<class T, int N>
T G3D::SmallArray< T, N >::m_embedded[N]
private

First N elements

template<class T, int N>
Array<T> G3D::SmallArray< T, N >::m_rest
private

Remaining elements

template<class T, int N>
int G3D::SmallArray< T, N >::m_size
private

The documentation for this class was generated from the following file: