00001 #ifndef _RLIST_HXX_
00002 #define _RLIST_HXX_
00003
00004 #include <RBasicTypes.hxx>
00005 #include <RBase/RStatus.hxx>
00006
00007
00008
00009 namespace RayGina {
00010 namespace CORE {
00011 template<typename T> class RList {
00012 public:
00016 inline RList<T>() : _data(NULL),_size(0),_capacity(0) {
00017 }
00018
00022 inline RList<T>(const RUInt newCapacity) : _size(0) {
00023 _capacity = newCapacity;
00024 #ifdef MALLOCTEST
00025 _data = (T*)malloc(sizeof(T) * _capacity);
00026 #else
00027 _data = new T[_capacity];
00028 #endif
00029 }
00030
00034
00035
00036
00037
00038
00039
00043 inline ~RList<T>() {
00044 #ifdef MALLOCTEST
00045 if(NULL != _data) free(_data);
00046 #else
00047 if(NULL != _data) delete[] _data;
00048 #endif
00049 }
00050
00054 inline T getElement(const RUInt elementNumber) const {
00055 #ifdef RAYGINADEBUG
00056 if(elementNumber >= _size) {
00057 printf("RList::getElement elementNumber=%d is bigger than size=%d\n",elementNumber,_size);
00058 exit(0);
00059 }
00060 #endif
00061 return _data[elementNumber];
00062 }
00063
00067 inline const RUInt getSize() const {
00068 return _size;
00069 }
00070
00074 inline const RBool isEmpty() const {
00075 return (0 == _size);
00076 }
00077
00081 inline const RUInt getCapacity() const {
00082 return _capacity;
00083 }
00084
00088 inline const RStatus clear() {
00089 #ifdef MALLOCTEST
00090 if(NULL != _data) free(_data);
00091 #else
00092 if(NULL != _data) delete[] _data;
00093 #endif
00094 _data = NULL;
00095 _size = 0;
00096 _capacity = 0;
00097 return RStatus::SUCCESS;
00098 }
00099
00104 inline const RStatus setSize(const RUInt newSize) {
00105 setCapacity(newSize);
00106 _size = newSize;
00107 return RStatus::SUCCESS;
00108 }
00109
00113 inline const RStatus setCapacity(const RUInt newCapacity) {
00114
00115
00116
00117 T* oldData = _data;
00118
00119 #ifdef MALLOCTEST
00120 _data = (T*)realloc(_data,sizeof(T) * newCapacity);
00121 #else
00122
00123 _data = new T[newCapacity];
00124 #endif
00125
00126 if(NULL == _data) {
00127
00128
00129 _data = oldData;
00130
00131
00132 printf("OY OF MEMEORY\n");
00133 return RStatus(RStatus::OUT_OF_MEMORY,"Could no allocate memory for the new RList capacity");
00134 }
00135
00136
00137 _capacity = newCapacity;
00138
00139 #ifdef MALLOCTEST
00140 if(_size > _capacity) _size = _capacity;
00141 #else
00142
00143 if(NULL == oldData) return RStatus::SUCCESS;
00144
00145
00146 RUInt copySize = _size;
00147 if(copySize > _capacity) copySize = _capacity;
00148
00149 for(RUInt i = 0 ; i < copySize ; i++ ) {
00150 _data[i] = oldData[i];
00151 }
00152
00153
00154 delete[] oldData;
00155 #endif
00156 return RStatus::SUCCESS;
00157 }
00158
00162 inline const RUInt getFreeElements() const {
00163 return _capacity - _size;
00164 }
00165
00169 inline const RUInt addElement(T newElement) {
00170
00171 if(_size == _capacity) {
00172
00173 if(0 == _capacity) {
00174 setCapacity(10);
00175 } else {
00176 setCapacity(_capacity * 2);
00177 }
00178 }
00179 _data[_size] = newElement;
00180 _size++;
00181 return _size;
00182 }
00183
00187 inline T& operator[](const RUInt n) {
00188 #ifdef RAYGINADEBUG
00189 if(n >= getCapacity()) {
00190 printf("\n\nBIG ERROR, RList operator[n] is called with an n(%d) bigger then capacity %d!!\nThis will lead into a crash in Release mode!\n\n",n,getCapacity());
00191 const RStatus status = setCapacity(n+1);
00192 if(RStatus::SUCCESS != status) {
00193
00194 exit(0);
00195 }
00196 _size = n+1;
00197 }
00198 #endif
00199 return _data[n];
00200 }
00201
00205 inline T& operator[](const RUInt n) const {
00206 #ifdef RAYGINADEBUG
00207 if(n >= getCapacity()) {
00208 exit(0);
00209 }
00210 #endif
00211 return _data[n];
00212 }
00213
00221 inline const RStatus pack(const RUInt freeElements = 0) {
00222 return setCapacity(getSize() + freeElements);
00223 }
00224
00228 inline void iterate(T (*iterationFunction)(const RUInt elementNumber,T inputValue)) {
00229
00230 if(isEmpty()) return;
00231
00232 for(RUInt i = 0 ; i < getSize() ; i++ ) {
00233 _data[i] = iterationFunction(i,_data[i]);
00234 }
00235 }
00239 inline void readOnlyIterate(void (*iterationFunction)(const RUInt elementNumber,T inputValue)) {
00240
00241 if(isEmpty()) return;
00242
00243 for(RUInt i = 0 ; i < getSize() ; i++ ) {
00244 iterationFunction(i,_data[i]);
00245 }
00246 }
00247 private:
00249 T* _data;
00250
00252 RUInt _size;
00253
00255 RUInt _capacity;
00256
00258
00259 };
00260 };
00261 };
00262
00263 #endif
00264