#7054 - MumblyJoe - Sat Jun 07, 2003 6:26 am
Hey, I have written a std::vector style class for arrays in my gba projects and it seems to work ok, but I would like it tested for robustness in some real world applications. If anyone has any projects that use arrays and add and remove items from them, could you give this a shot and tell me how it goes.
Basically it works just like an array, even uses operator[] and dynamically grows by 10 items when it gets full, but does not shrink as items are removed (I thought it would fragment memory too much on the GBA, would rather just be using it all up).
Any help would be great, and if you find it usefull feel free to steal it or modify it for your needs. It has really only been written because std::vector is to big to think about using and also it uses pure virtual functions, which of course break DKA under most circumstances.
PS: I know a linked list would be more dynamic but I feel the pointers involved outweigh the benifits on the GBA.
_________________
www.hungrydeveloper.com
Version 2.0 now up - guaranteed at least 100% more pleasing!
Basically it works just like an array, even uses operator[] and dynamically grows by 10 items when it gets full, but does not shrink as items are removed (I thought it would fragment memory too much on the GBA, would rather just be using it all up).
Any help would be great, and if you find it usefull feel free to steal it or modify it for your needs. It has really only been written because std::vector is to big to think about using and also it uses pure virtual functions, which of course break DKA under most circumstances.
PS: I know a linked list would be more dynamic but I feel the pointers involved outweigh the benifits on the GBA.
Code: |
template<class T>
class Array { public: Array(){array = new T[10]; size=10; top=0;} Array(const Array& rhs) { array = new T[10]; size=10; top=0; for(int i=0;i<rhs.top;i++) Add(rhs.array[i]); } ~Array(){delete [] array;} T& operator[](const int i){return array[i];} const Array& operator=(const Array& rhs) { delete [] array; array = new T[10]; size=10; top=0; for(int i=0;i<rhs.top;i++) Add(rhs.array[i]); return *this; } void Add(T wanted) { if(top==size) { T* temp = new T[size+10]; for(int i=0;i<size;i++) temp[i] = array[i]; delete [] array; array = temp; size+=10; } array[top++]=wanted; } void Remove(int i) { for(int j=i;j<(top-1);j++) array[j] = array[j+1]; top--; } const int Size(){return top;} private: T* array; int size; int top; }; |
_________________
www.hungrydeveloper.com
Version 2.0 now up - guaranteed at least 100% more pleasing!