00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef _ARRAY_H_
00012 #define _ARRAY_H_
00013
00014
00015
00016
00017 #define ARRAY_ASSERT(x)
00018
00019 #include "lib/common.h"
00020 #include "base/SGObject.h"
00021
00022 #ifdef ARRAY_STATISTICS
00023 struct array_statistics {
00024 int32_t const_element;
00025 int32_t element;
00026 int32_t set_element;
00027 int32_t get_element;
00028 int32_t operator_overload;
00029 int32_t const_operator_overload;
00030 int32_t set_array;
00031 int32_t get_array;
00032 int32_t resize_array;
00033 int32_t array_element;
00034 };
00035
00036 #define DECLARE_ARRAY_STATISTICS struct array_statistics as
00037 #define INIT_ARRAY_STATISTICS memset(&as, 0, sizeof(as))
00038 #define PRINT_ARRAY_STATISTICS \
00039 SG_DEBUG("access statistics:\n" \
00040 "const element %i\n" \
00041 "element %i\n" \
00042 "set_element %i\n" \
00043 "get_element %i\n" \
00044 "operator_overload[] %i\n" \
00045 "const_operator_overload[] %i\n" \
00046 "set_array %i\n" \
00047 "get_array %i\n" \
00048 "resize_array %i\n" \
00049 "array_element %i\n", \
00050 as.const_element, \
00051 as.element, \
00052 as.set_element, \
00053 as.get_element, \
00054 as.operator_overload, \
00055 as.const_operator_overload, \
00056 as.set_array, \
00057 as.get_array, \
00058 as.resize_array, \
00059 as.array_element \
00060 );
00061
00062 #define INCREMENT_ARRAY_STATISTICS_VALUE(_val_) ((CArray<T>*)this)->as._val_++
00063
00064 #else
00065 #define DECLARE_ARRAY_STATISTICS
00066 #define INIT_ARRAY_STATISTICS
00067 #define PRINT_ARRAY_STATISTICS
00068 #define INCREMENT_ARRAY_STATISTICS_VALUE(_val_)
00069 #endif
00070
00077 template <class T> class CArray : public CSGObject
00078 {
00079 public:
00084 CArray(int32_t initial_size = 1)
00085 : CSGObject(), free_array(true), name(NULL)
00086 {
00087 INIT_ARRAY_STATISTICS;
00088 array_size = initial_size;
00089 array = (T*) calloc(array_size, sizeof(T));
00090 ARRAY_ASSERT(array);
00091 }
00092
00100 CArray(T* p_array, int32_t p_array_size, bool p_free_array=true,
00101 bool p_copy_array=false)
00102 : CSGObject(), array(NULL), free_array(false), name(NULL)
00103 {
00104 INIT_ARRAY_STATISTICS;
00105 set_array(p_array, p_array_size, p_free_array, p_copy_array);
00106 }
00107
00113 CArray(const T* p_array, int32_t p_array_size)
00114 : CSGObject(), array(NULL), free_array(false), name(NULL)
00115 {
00116 INIT_ARRAY_STATISTICS;
00117 set_array(p_array, p_array_size);
00118 }
00119
00120 virtual ~CArray()
00121 {
00122 SG_DEBUG( "destroying CArray array '%s' of size %i\n",
00123 name? name : "unnamed", array_size);
00124 PRINT_ARRAY_STATISTICS;
00125 if (free_array)
00126 free(array);
00127 }
00128
00133 inline const char* get_name() const { return name; }
00134
00139 inline void set_name(const char * p_name)
00140 {
00141 name = p_name;
00142 }
00143
00148 inline int32_t get_array_size() const
00149 {
00150 return array_size;
00151 }
00152
00157 inline int32_t get_dim1()
00158 {
00159 return array_size;
00160 }
00161
00163 inline void zero()
00164 {
00165 for (int32_t i=0; i< array_size; i++)
00166 array[i]=0;
00167 }
00168
00174 inline const T& get_element(int32_t index) const
00175 {
00176 ARRAY_ASSERT(array && (index>=0) && (index<array_size));
00177 INCREMENT_ARRAY_STATISTICS_VALUE(get_element);
00178 return array[index];
00179 }
00180
00187 inline bool set_element(const T& p_element, int32_t index)
00188 {
00189 ARRAY_ASSERT(array && (index>=0) && (index<array_size));
00190 INCREMENT_ARRAY_STATISTICS_VALUE(set_element);
00191 array[index]=p_element;
00192 return true;
00193 }
00194
00200 inline const T& element(int32_t idx1) const
00201 {
00202 INCREMENT_ARRAY_STATISTICS_VALUE(const_element);
00203 return get_element(idx1);
00204 }
00205
00211 inline T& element(int32_t index)
00212 {
00213 ARRAY_ASSERT(array);
00214 ARRAY_ASSERT(index>=0);
00215 ARRAY_ASSERT(index<array_size);
00216 INCREMENT_ARRAY_STATISTICS_VALUE(element);
00217 return array[index];
00218 }
00219
00226 inline T& element(T* p_array, int32_t index)
00227 {
00228 ARRAY_ASSERT(array && (index>=0) && (index<array_size));
00229 ARRAY_ASSERT(array == p_array);
00230 INCREMENT_ARRAY_STATISTICS_VALUE(array_element);
00231 return p_array[index];
00232 }
00233
00239 bool resize_array(int32_t n)
00240 {
00241 INCREMENT_ARRAY_STATISTICS_VALUE(resize_array);
00242 ARRAY_ASSERT(free_array);
00243
00244 T* p= (T*) realloc(array, sizeof(T)*n);
00245 if (!p)
00246 return false;
00247 array=p;
00248 if (n > array_size)
00249 memset(&array[array_size], 0, (n-array_size)*sizeof(T));
00250 array_size=n;
00251 return true;
00252 }
00253
00260 inline T* get_array()
00261 {
00262 INCREMENT_ARRAY_STATISTICS_VALUE(get_array);
00263 return array;
00264 }
00265
00273 inline void set_array(T* p_array, int32_t p_array_size, bool p_free_array=true,
00274 bool copy_array=false)
00275 {
00276 INCREMENT_ARRAY_STATISTICS_VALUE(set_array);
00277 if (this->free_array)
00278 free(this->array);
00279 if (copy_array)
00280 {
00281 this->array=(T*)malloc(p_array_size*sizeof(T));
00282 memcpy(this->array, p_array, p_array_size*sizeof(T));
00283 }
00284 else
00285 this->array=p_array;
00286 this->array_size=p_array_size;
00287 this->free_array=p_free_array;
00288 }
00289
00295 inline void set_array(const T* p_array, int32_t p_array_size)
00296 {
00297 INCREMENT_ARRAY_STATISTICS_VALUE(set_array);
00298 free(this->array);
00299 this->array=(T*)malloc(p_array_size*sizeof(T));
00300 memcpy(this->array, p_array, p_array_size*sizeof(T));
00301 this->array_size=p_array_size;
00302 this->free_array=true;
00303 }
00304
00306 inline void clear_array()
00307 {
00308 memset(array, 0, array_size*sizeof(T));
00309 }
00310
00311
00320 inline const T& operator[](int32_t index) const
00321 {
00322 INCREMENT_ARRAY_STATISTICS_VALUE(const_operator_overload);
00323 return array[index];
00324 }
00325
00333 inline T& operator[](int32_t index)
00334 {
00335 INCREMENT_ARRAY_STATISTICS_VALUE(operator_overload);
00336 return element(index);
00337 }
00338
00344 CArray<T>& operator=(const CArray<T>& orig)
00345 {
00346 memcpy(array, orig.array, sizeof(T)*orig.array_size);
00347 array_size=orig.array_size;
00348
00349 return *this;
00350 }
00351
00353 void display_size() const
00354 {
00355 SG_PRINT( "Array '%s' of size: %d\n", name? name : "unnamed",
00356 array_size);
00357 }
00358
00360 void display_array() const
00361 {
00362 display_size();
00363 for (int32_t i=0; i<array_size; i++)
00364 SG_PRINT("%1.1f,", (float32_t)array[i]);
00365 SG_PRINT("\n");
00366 }
00367
00369 inline virtual const char* get_name() { return "Array"; }
00370
00371 protected:
00373 T* array;
00375 int32_t array_size;
00377 bool free_array;
00379 const char *name;
00381 DECLARE_ARRAY_STATISTICS;
00382
00383 };
00384 #endif