00001
00002
00003
00004
00005
00006
00007
00008
00015 #ifndef _FM_ARRAY_POINTER_H_
00016 #define _FM_ARRAY_POINTER_H_
00017
00018 #ifndef _FM_ARRAY_H_
00019 #include "FMath/FMArray.h"
00020 #endif // _FM_ARRAY_H_
00021
00022
00023 namespace fm
00024 {
00032 template <class T>
00033 class pvector : public fm::vector<const void*, true>
00034 {
00035 private:
00038 T*** first;
00039
00040 public:
00041 typedef typename fm::vector<const void*, true> Parent;
00042 typedef T* item;
00043 typedef const T* const_item;
00044 typedef item* iterator;
00045 typedef const_item* const_iterator;
00048 pvector() : Parent()
00049 {
00050 first = (T***) (size_t) &heapBuffer;
00051 }
00052
00055 pvector(size_t size) : Parent(size, NULL)
00056 {
00057 first = (T***) (size_t) &heapBuffer;
00058 }
00059
00063 pvector(size_t size, const T& defaultValue) : Parent(size, defaultValue)
00064 {
00065 first = (T***) &heapBuffer;
00066 }
00067
00070 pvector(const pvector<T>& copy) : Parent(copy)
00071 {
00072 first = (T***) (size_t) &heapBuffer;
00073 }
00074
00078 pvector(const T** values, size_t count) : Parent()
00079 {
00080 first = (T***) (size_t) &heapBuffer;
00081 resize(count);
00082 memcpy(&front(), values, count * sizeof(void*));
00083 }
00084
00086 ~pvector()
00087 {
00088 #ifdef _DEBUG
00089 Parent::clear();
00090 Parent::push_back((void*) (size_t) 0xFFFFFFFF);
00091 first = (T***) (size_t) 0xDEADDEAD;
00092 #endif // _DEBUG
00093 }
00094
00097 inline T*& front() { return (T*&) Parent::front(); }
00098 inline const T*& front() const { return (const T*&) Parent::front(); }
00102 inline T*& back() { return (T*&) Parent::back(); }
00103 inline const T*& back() const { return (const T*&) Parent::back(); }
00108 inline T*& at(size_t index) { return (T*&) Parent::at(index); }
00109 inline const T*& at(size_t index) const { return (const T*&) Parent::at(index); }
00110 template <class INTEGER> inline T*& operator[](INTEGER index) { return (T*&) Parent::at(index); }
00111 template <class INTEGER> inline const T*& operator[](INTEGER index) const { return (const T*&) Parent::at(index); }
00115 inline iterator begin() { return (!empty()) ? &front() : NULL; }
00116 inline const_iterator begin() const { return (!empty()) ? &front() : NULL; }
00120 inline iterator end() { return (!empty()) ? (&back()) + 1 : NULL; }
00121 inline const_iterator end() const { return (!empty()) ? (&back()) + 1 : NULL; }
00127 inline iterator find(const T* item) { Parent::iterator f = Parent::find(item); return begin() + (f - Parent::begin()); }
00128 inline const_iterator find(const T* item) const { Parent::const_iterator f = Parent::find(item); return begin() + (f - Parent::begin()); }
00134 inline iterator insert(iterator _iterator, T* object)
00135 {
00136 iterator originalStart = begin();
00137 Parent::iterator newIt = Parent::insert(Parent::begin() + (_iterator - originalStart), object);
00138 return begin() + (newIt - Parent::begin());
00139 }
00140
00144 inline void insert(iterator _iterator, size_t count)
00145 {
00146 Parent::iterator it = Parent::begin() + (_iterator - begin());
00147 Parent::insert(it, count, NULL);
00148 }
00149
00155 template <class _It>
00156 inline void insert(iterator _where, _It _startIterator, _It _endIterator)
00157 {
00158 if (_startIterator < _endIterator)
00159 {
00160 size_t relativeWhere = _where - begin();
00161 size_t count = _endIterator - _startIterator;
00162 Parent::insert(Parent::begin() + relativeWhere, count, (T*) 0);
00163 _where = begin() + relativeWhere;
00164
00165 memcpy(_where, _startIterator, sizeof(void*) * count);
00166 }
00167 }
00168
00171 inline iterator erase(iterator _it)
00172 {
00173 Parent::iterator it = Parent::begin() + (_it - begin());
00174 it = Parent::erase(it);
00175 return begin() + (it - Parent::begin());
00176 }
00177
00181 inline bool erase(const T* value)
00182 {
00183 Parent::iterator it = Parent::find(value);
00184 if (it != Parent::end()) { Parent::erase(it); return true; }
00185 return false;
00186 }
00187
00191 inline void erase(iterator first, iterator last)
00192 {
00193 Parent::erase(Parent::begin() + (first - begin()), Parent::begin() + (last - begin()));
00194 }
00195
00198 inline void erase(size_t index) { Parent::erase(Parent::begin() + index); }
00199
00204 inline bool release(const T* value)
00205 {
00206 Parent::iterator it = Parent::find(value);
00207 if (it != Parent::end()) { Parent::erase(it); delete value; return true; }
00208 return false;
00209 }
00210
00215 pvector<T>& operator= (const pvector<T>& other) { clear(); insert(end(), other.begin(), other.end()); return *this; }
00216
00220 inline void resize(size_t count) { Parent::resize(count, NULL); }
00221 };
00222 };
00223
00224 #endif // _FM_ARRAY_POINTER_H_