26#ifndef HEADER_PtrVector_HPP
27#define HEADER_PtrVector_HPP
33#include "utils/aligned_array.hpp"
42template<
typename TYPE, VECTOR_TYPE type=HOLD>
47 AlignedArray<TYPE*> m_contents_vector;
57 if(type == HOLD) clearAndDeleteAll();
62 void push_back(TYPE* t)
64 m_contents_vector.push_back(t);
68 void swap(
int ID1,
int ID2)
71 assert((
unsigned int)ID1 < m_contents_vector.size());
73 assert((
unsigned int)ID2 < m_contents_vector.size());
76 TYPE* temp = m_contents_vector[ID2];
78 m_contents_vector[ID2] = m_contents_vector[ID1];
79 m_contents_vector[ID1] = temp;
83 TYPE* get(
const int ID)
86 assert((
unsigned int)ID < (
unsigned int)m_contents_vector.size());
88 return m_contents_vector[ID];
92 const TYPE* get(
const int ID)
const
95 assert((
unsigned int)ID < (
unsigned int)m_contents_vector.size());
97 return m_contents_vector[ID];
101 unsigned int size()
const
103 return (
int) m_contents_vector.size();
107 void erase(
const int ID)
110 assert((
unsigned int)ID < (
unsigned int)m_contents_vector.size());
112 delete ( TYPE *) m_contents_vector[ID];
114 const unsigned int amount = (
unsigned int)m_contents_vector.size();
115 for(
unsigned int i=ID; i<amount-1; i++)
117 m_contents_vector[i]=m_contents_vector[i+1];
119 m_contents_vector.pop_back();
121 m_contents_vector.erase(m_contents_vector.begin()+ID);
126 TYPE* remove(
const int ID)
129 assert((
unsigned int)ID < (
unsigned int)m_contents_vector.size());
131 TYPE* out = m_contents_vector[ID];
133 const unsigned int amount = (
unsigned int)m_contents_vector.size();
134 for(
unsigned int i=ID; i<amount-1; i++)
136 m_contents_vector[i]=m_contents_vector[i+1];
138 m_contents_vector.pop_back();
140 m_contents_vector.erase(m_contents_vector.begin()+ID);
146 bool contains(
const TYPE* instance )
const
148 const unsigned int amount = (
unsigned int)m_contents_vector.size();
149 for (
unsigned int n=0; n<amount; n++)
151 const TYPE * pointer = m_contents_vector[n];
152 if (pointer == instance)
return true;
159 void clearAndDeleteAll()
161 for (
unsigned int n=0; n<(
unsigned int)m_contents_vector.size(); n++)
163 TYPE * pointer = m_contents_vector[n];
165 m_contents_vector[n] = (TYPE*)(intptr_t)0xDEADBEEF;
169 assert( !contains(pointer) );
171 m_contents_vector.clear();
175 TYPE& operator[](
const unsigned int ID)
177 assert((
unsigned int)ID < (
unsigned int)m_contents_vector.size());
179 return *(m_contents_vector[ID]);
183 const TYPE& operator[](
const unsigned int ID)
const
185 assert((
unsigned int)ID < (
unsigned int)m_contents_vector.size());
187 return *(m_contents_vector[ID]);
191 void clearWithoutDeleting()
193 m_contents_vector.clear();
202 for(
unsigned int n=0; n<(
unsigned int)m_contents_vector.size(); n++)
205 TYPE * pointer = m_contents_vector[n];
209 const unsigned int amount =
210 (
unsigned int)m_contents_vector.size();
211 for(
unsigned int i=n; i<amount-1; i++)
213 m_contents_vector[i]=m_contents_vector[i+1];
215 m_contents_vector.pop_back();
217 m_contents_vector.erase(m_contents_vector.begin()+n);
225 typename AlignedArray<TYPE*>::iterator begin()
227 return m_contents_vector.begin();
230 typename AlignedArray<TYPE*>::iterator end()
232 return m_contents_vector.end();
235 typename AlignedArray<TYPE*>::const_iterator begin()
const
237 return m_contents_vector.begin();
240 typename AlignedArray<TYPE*>::const_iterator end()
const
242 return m_contents_vector.end();
252 for(
unsigned int n=0; n<(
unsigned int)m_contents_vector.size(); n++)
254 TYPE * pointer = m_contents_vector[n];
255 if((
void*)pointer == obj)
258 const unsigned int amount =
259 (
unsigned int)m_contents_vector.size();
260 for(
unsigned int i=n; i<amount-1; i++)
262 m_contents_vector[i]=m_contents_vector[i+1];
264 m_contents_vector.pop_back();
266 m_contents_vector.erase(m_contents_vector.begin()+n);
276 void insertionSort(
unsigned int start=0,
bool desc =
false)
282 for(
int j=(
int)start; j<(int)m_contents_vector.size()-1; j++)
284 if(*(m_contents_vector[j])<*(m_contents_vector[j+1]))
continue;
287 TYPE* t=m_contents_vector[j+1];
288 unsigned int i = j+1;
291 m_contents_vector[i] = m_contents_vector[i-1];
293 }
while (i>start && *t<*(m_contents_vector[i-1]));
294 m_contents_vector[i]=t;
299 for(
int j=(
int)start; j<(int)m_contents_vector.size()-1; j++)
301 if(*(m_contents_vector[j+1])<*(m_contents_vector[j]))
continue;
304 TYPE* t=m_contents_vector[j+1];
305 unsigned int i = j+1;
308 m_contents_vector[i] = m_contents_vector[i-1];
310 }
while (i>start && *(m_contents_vector[i-1]) <*t);
311 m_contents_vector[i]=t;
318 return m_contents_vector.empty();
324#define for_in( VAR, VECTOR ) for (unsigned int _foreach_i = 0; \
325 VAR = (_foreach_i < VECTOR.size() ? VECTOR.get(_foreach_i) : NULL),\
326 _foreach_i < VECTOR.size(); _foreach_i++)
327#define for_var_in( TYPE, VAR, VECTOR ) TYPE VAR; for (unsigned int _foreach_i = 0; \
328 VAR = (_foreach_i < VECTOR.size() ? VECTOR.get(_foreach_i) : NULL), \
329 _foreach_i < VECTOR.size(); _foreach_i++)
Definition: ptr_vector.hpp:44
bool erase(void *obj)
Removes and deletes the given object.
Definition: ptr_vector.hpp:250
void remove(TYPE *obj)
Removes without deleting.
Definition: ptr_vector.hpp:200