Woopsi 1.0 GUI Framework for Nintendo DS Homebrew

- Main Page
- Classes
- Files

- File List

# woopsiarray.h

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