00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef _ARRAY_H_
00012 #define _ARRAY_H_
00013
00014
00015
00016
00017
00018 #define ARRAY_ASSERT(x)
00019
00020 #include "lib/common.h"
00021 #include "base/SGObject.h"
00022
00023 namespace shogun
00024 {
00025 #ifdef ARRAY_STATISTICS
00026 struct array_statistics {
00027 int32_t const_element;
00028 int32_t element;
00029 int32_t set_element;
00030 int32_t get_element;
00031 int32_t operator_overload;
00032 int32_t const_operator_overload;
00033 int32_t set_array;
00034 int32_t get_array;
00035 int32_t resize_array;
00036 int32_t array_element;
00037 };
00038
00039 #define DECLARE_ARRAY_STATISTICS struct array_statistics as
00040 #define INIT_ARRAY_STATISTICS memset(&as, 0, sizeof(as))
00041 #define PRINT_ARRAY_STATISTICS \
00042 SG_DEBUG("access statistics:\n" \
00043 "const element %i\n" \
00044 "element %i\n" \
00045 "set_element %i\n" \
00046 "get_element %i\n" \
00047 "operator_overload[] %i\n" \
00048 "const_operator_overload[] %i\n" \
00049 "set_array %i\n" \
00050 "get_array %i\n" \
00051 "resize_array %i\n" \
00052 "array_element %i\n", \
00053 as.const_element, \
00054 as.element, \
00055 as.set_element, \
00056 as.get_element, \
00057 as.operator_overload, \
00058 as.const_operator_overload, \
00059 as.set_array, \
00060 as.get_array, \
00061 as.resize_array, \
00062 as.array_element \
00063 );
00064
00065 #define INCREMENT_ARRAY_STATISTICS_VALUE(_val_) ((CArray<T>*)this)->as._val_++
00066
00067 #else
00068 #define DECLARE_ARRAY_STATISTICS
00069 #define INIT_ARRAY_STATISTICS
00070 #define PRINT_ARRAY_STATISTICS
00071 #define INCREMENT_ARRAY_STATISTICS_VALUE(_val_)
00072 #endif
00073
00080 template <class T> class CArray : public CSGObject
00081 {
00082 public:
00087 CArray(int32_t initial_size = 1)
00088 : CSGObject(), free_array(true), name("Array")
00089 {
00090 INIT_ARRAY_STATISTICS;
00091 array_size = initial_size;
00092 array = (T*) calloc(array_size, sizeof(T));
00093 ARRAY_ASSERT(array);
00094 }
00095
00103 CArray(T* p_array, int32_t p_array_size, bool p_free_array=true,
00104 bool p_copy_array=false)
00105 : CSGObject(), array(NULL), free_array(false), name("Array")
00106 {
00107 INIT_ARRAY_STATISTICS;
00108 set_array(p_array, p_array_size, p_free_array, p_copy_array);
00109 }
00110
00116 CArray(const T* p_array, int32_t p_array_size)
00117 : CSGObject(), array(NULL), free_array(false), name("Array")
00118 {
00119 INIT_ARRAY_STATISTICS;
00120 set_array(p_array, p_array_size);
00121 }
00122
00123 virtual ~CArray()
00124 {
00125
00126 PRINT_ARRAY_STATISTICS;
00127 if (free_array)
00128 free(array);
00129 }
00130
00135 inline virtual const char* get_name() const { return name; }
00136
00141 inline void set_name(const char* p_name)
00142 {
00143 name = p_name;
00144 }
00145
00150 inline int32_t get_array_size() const
00151 {
00152 return array_size;
00153 }
00154
00159 inline int32_t get_dim1()
00160 {
00161 return array_size;
00162 }
00163
00165 inline void zero()
00166 {
00167 for (int32_t i=0; i< array_size; i++)
00168 array[i]=0;
00169 }
00170
00172 inline void set_const(T const_elem)
00173 {
00174 for (int32_t i=0; i< array_size; i++)
00175 array[i]=const_elem ;
00176 }
00177
00183 inline const T& get_element(int32_t index) const
00184 {
00185 ARRAY_ASSERT(array && (index>=0) && (index<array_size));
00186 INCREMENT_ARRAY_STATISTICS_VALUE(get_element);
00187 return array[index];
00188 }
00189
00196 inline bool set_element(const T& p_element, int32_t index)
00197 {
00198 ARRAY_ASSERT(array && (index>=0) && (index<array_size));
00199 INCREMENT_ARRAY_STATISTICS_VALUE(set_element);
00200 array[index]=p_element;
00201 return true;
00202 }
00203
00209 inline const T& element(int32_t idx1) const
00210 {
00211 INCREMENT_ARRAY_STATISTICS_VALUE(const_element);
00212 return get_element(idx1);
00213 }
00214
00220 inline T& element(int32_t index)
00221 {
00222 ARRAY_ASSERT(array);
00223 ARRAY_ASSERT(index>=0);
00224 ARRAY_ASSERT(index<array_size);
00225 INCREMENT_ARRAY_STATISTICS_VALUE(element);
00226 return array[index];
00227 }
00228
00235 inline T& element(T* p_array, int32_t index)
00236 {
00237 ARRAY_ASSERT(array && (index>=0) && (index<array_size));
00238 ARRAY_ASSERT(array == p_array);
00239 INCREMENT_ARRAY_STATISTICS_VALUE(array_element);
00240 return p_array[index];
00241 }
00242
00248 bool resize_array(int32_t n)
00249 {
00250 INCREMENT_ARRAY_STATISTICS_VALUE(resize_array);
00251 ARRAY_ASSERT(free_array);
00252
00253 T* p= (T*) realloc(array, sizeof(T)*n);
00254 if (!p)
00255 return false;
00256 array=p;
00257 if (n > array_size)
00258 memset(&array[array_size], 0, (n-array_size)*sizeof(T));
00259 array_size=n;
00260 return true;
00261 }
00262
00269 inline T* get_array()
00270 {
00271 INCREMENT_ARRAY_STATISTICS_VALUE(get_array);
00272 return array;
00273 }
00274
00282 inline void set_array(T* p_array, int32_t p_array_size, bool p_free_array=true,
00283 bool copy_array=false)
00284 {
00285 INCREMENT_ARRAY_STATISTICS_VALUE(set_array);
00286 if (this->free_array)
00287 free(this->array);
00288 if (copy_array)
00289 {
00290 this->array=(T*)malloc(p_array_size*sizeof(T));
00291 memcpy(this->array, p_array, p_array_size*sizeof(T));
00292 }
00293 else
00294 this->array=p_array;
00295 this->array_size=p_array_size;
00296 this->free_array=p_free_array;
00297 }
00298
00304 inline void set_array(const T* p_array, int32_t p_array_size)
00305 {
00306 INCREMENT_ARRAY_STATISTICS_VALUE(set_array);
00307 free(this->array);
00308 this->array=(T*)malloc(p_array_size*sizeof(T));
00309 memcpy(this->array, p_array, p_array_size*sizeof(T));
00310 this->array_size=p_array_size;
00311 this->free_array=true;
00312 }
00313
00315 inline void clear_array()
00316 {
00317 memset(array, 0, array_size*sizeof(T));
00318 }
00319
00320
00329 inline const T& operator[](int32_t index) const
00330 {
00331 INCREMENT_ARRAY_STATISTICS_VALUE(const_operator_overload);
00332 return array[index];
00333 }
00334
00342 inline T& operator[](int32_t index)
00343 {
00344 INCREMENT_ARRAY_STATISTICS_VALUE(operator_overload);
00345 return element(index);
00346 }
00347
00353 CArray<T>& operator=(const CArray<T>& orig)
00354 {
00355 memcpy(array, orig.array, sizeof(T)*orig.array_size);
00356 array_size=orig.array_size;
00357
00358 return *this;
00359 }
00360
00362 void display_size() const
00363 {
00364 SG_PRINT( "Array '%s' of size: %d\n", name? name : "unnamed",
00365 array_size);
00366 }
00367
00369 void display_array() const
00370 {
00371 display_size();
00372 for (int32_t i=0; i<array_size; i++)
00373 SG_PRINT("%1.1f,", (float32_t)array[i]);
00374 SG_PRINT("\n");
00375 }
00376
00377 protected:
00379 T* array;
00381 int32_t array_size;
00383 bool free_array;
00385 const char* name;
00387 DECLARE_ARRAY_STATISTICS;
00388
00389 };
00390 }
00391 #endif