- Main Page
- Classes
- Files

- File List

# C:/Documents and Settings/Administrator/Desktop/woopsi-src-0.39/Woopsi/woopsi/woopsiarray.h

    00001 #ifndef _DYNAMIC_ARRAY_H_
    00002 #define _DYNAMIC_ARRAY_H_
    00003
    00004 #include <nds.h>
    00005
    00006 #define DYNAMIC_ARRAY_SIZE 100
    00007
    00021 template <class T>
    00022 class WoopsiArray {
    00023 public:
    00024
    00028         inline WoopsiArray();
    00029
    00033         inline ~WoopsiArray();
    00034
    00039         inline const s32 size() const;
    00040
    00045         void push_back(const T &value);
    00046
    00052         void insert(const s32 index, const T &value);
    00053
    00057         void pop_back();
    00058
    00062         void erase(const s32 index);
    00063
    00069         inline T& at(const s32 index) const;
    00070
    00075         inline bool empty() const;
    00076
    00080         void clear();
    00081
    00087         T& operator[](const s32 index) const;
    00088
    00089 private:
    00090         T* _data;
    00091         s32 _size;
    00092         s32 _reservedSize;
    00097         void resize();
    00098 };
    00099
    00100 template <class T>
    00101 WoopsiArray<T>::WoopsiArray() {
    00102         _size = 0;
    00103         _reservedSize = DYNAMIC_ARRAY_SIZE;
    00104         _data = new T[_reservedSize];
    00105 }
    00106
    00107 template <class T>
    00108 WoopsiArray<T>::~WoopsiArray() {
    00109         delete [] _data;
    00110 }
    00111
    00112 template <class T>
    00113 const s32 WoopsiArray<T>::size() const {
    00114         return _size;
    00115 }
    00116
    00117 template <class T>
    00118 void WoopsiArray<T>::push_back(const T &value) {
    00119
    00120         // Ensure the array is large enough to contain this data
    00121         resize();
    00122
    00123         // Add data to array
    00124         _data[_size] = value;
    00125
    00126         // Remember we've filled a slot
    00127         _size++;
    00128 }
    00129
    00130 template <class T>
    00131 void WoopsiArray<T>::pop_back() {
    00132         if (_size >= 1) {
    00133                 // We can just reduce the used size of the array, as the value
    00134                 // will get overwritten automatically
    00135                 _size--;
    00136         }
    00137 }
    00138
    00139 template <class T>
    00140 void WoopsiArray<T>::insert(const s32 index, const T &value) {
    00141
    00142         // Bounds check
    00143         if ((index >= _size) || (_size == 0)) {
    00144                 push_back(value);
    00145                 return;
    00146         }
    00147
    00148         // Ensure the array is large enough to contain this data
    00149         resize();
    00150
    00151         // Shift all of the data back one place to make a space for the new data
    00152         for (s32 i = _size; i > index; i--) {
    00153                 _data[i] = _data[i - 1];
    00154         }
    00155
    00156         // Add data to array
    00157         _data[index] = value;
    00158
    00159         // Remember we've filled a slot
    00160         _size++;
    00161 }
    00162
    00163 template <class T>
    00164 void WoopsiArray<T>::erase(const s32 index) {
    00165
    00166         // Bounds check
    00167         if (index >= _size) return;
    00168
    00169         // Shift all of the data back one place and overwrite the value
    00170         for (s32 i = index; i < _size - 1; i++) {
    00171                 _data[i] = _data[i + 1];
    00172         }
    00173
    00174         // Remember we've removed a slot
    00175         _size--;
    00176 }
    00177
    00178 template <class T>
    00179 void WoopsiArray<T>::resize() {
    00180         // Do we need to redim the array?
    00181         if (_reservedSize == _size) {
    00182
    00183                 // We have filled the array, so resize it
    00184
    00185                 // Create new array
    00186                 u32 newSize = _reservedSize + DYNAMIC_ARRAY_SIZE;
    00187                 T* newData = new T[newSize];
    00188
    00189                 // Copy old array to new
    00190                 for (s32 i = 0; i < _reservedSize; i++) {
    00191                         newData[i] = _data[i];
    00192                 }
    00193
    00194                 //memcpy(newData, _data, sizeof(T) * _reservedSize);
    00195
    00196                 // Delete the old array
    00197                 delete [] _data;
    00198
    00199                 // Update values
    00200                 _data = newData;
    00201                 _reservedSize = newSize;
    00202         }
    00203 }
    00204
    00205 template <class T>
    00206 T& WoopsiArray<T>::at(const s32 index) const {
    00207         return _data[index];
    00208 }
    00209
    00210 template <class T>
    00211 bool WoopsiArray<T>::empty() const {
    00212         return (_size == 0);
    00213 }
    00214
    00215 template <class T>
    00216 T& WoopsiArray<T>::operator[](const s32 index) const {
    00217         return _data[index];
    00218 }
    00219
    00220 template <class T>
    00221 void WoopsiArray<T>::clear() {
    00222         // All we need to do is reset the size value
    00223         _size = 0;
    00224 }
    00225
    00226 #endif

All Classes Functions Variables Enumerations Enumerator