SHOGUN
v1.1.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 1999-2009 Soeren Sonnenburg, Gunnar Raetsch, Andre Noll 00008 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #ifndef _ARRAY_H_ 00012 #define _ARRAY_H_ 00013 00014 //#define ARRAY_STATISTICS 00015 00016 //#define ARRAY_ASSERT(x) {if ((x)==0) {*((int*)0)=0;}} 00017 //#define ARRAY_ASSERT(x) ASSERT(x) 00018 #define ARRAY_ASSERT(x) 00019 00020 #include <shogun/lib/common.h> 00021 #include <shogun/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 /* ARRAY_STATISTICS */ 00068 #define DECLARE_ARRAY_STATISTICS 00069 #define INIT_ARRAY_STATISTICS 00070 #define PRINT_ARRAY_STATISTICS 00071 #define INCREMENT_ARRAY_STATISTICS_VALUE(_val_) 00072 #endif /* ARRAY_STATISTICS */ 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 //SG_DEBUG( "destroying CArray array '%s' of size %i\n", name? name : "unnamed", array_size); 00126 PRINT_ARRAY_STATISTICS; 00127 SG_FREE(array); 00128 } 00129 00134 inline virtual const char* get_name() const { return "Array"; } 00135 00140 inline virtual const char* get_array_name() const { return name; } 00141 00146 inline void set_array_name(const char* p_name) 00147 { 00148 name = p_name; 00149 } 00150 00155 inline int32_t get_array_size() const 00156 { 00157 return array_size; 00158 } 00159 00164 inline int32_t get_dim1() 00165 { 00166 return array_size; 00167 } 00168 00170 inline void zero() 00171 { 00172 for (int32_t i=0; i< array_size; i++) 00173 array[i]=0; 00174 } 00175 00177 inline void set_const(T const_elem) 00178 { 00179 for (int32_t i=0; i< array_size; i++) 00180 array[i]=const_elem ; 00181 } 00182 00188 inline const T& get_element(int32_t index) const 00189 { 00190 ARRAY_ASSERT(array && (index>=0) && (index<array_size)); 00191 INCREMENT_ARRAY_STATISTICS_VALUE(get_element); 00192 return array[index]; 00193 } 00194 00201 inline bool set_element(const T& p_element, int32_t index) 00202 { 00203 ARRAY_ASSERT(array && (index>=0) && (index<array_size)); 00204 INCREMENT_ARRAY_STATISTICS_VALUE(set_element); 00205 array[index]=p_element; 00206 return true; 00207 } 00208 00214 inline const T& element(int32_t idx1) const 00215 { 00216 INCREMENT_ARRAY_STATISTICS_VALUE(const_element); 00217 return get_element(idx1); 00218 } 00219 00225 inline T& element(int32_t index) 00226 { 00227 ARRAY_ASSERT(array); 00228 ARRAY_ASSERT(index>=0); 00229 ARRAY_ASSERT(index<array_size); 00230 INCREMENT_ARRAY_STATISTICS_VALUE(element); 00231 return array[index]; 00232 } 00233 00240 inline T& element(T* p_array, int32_t index) 00241 { 00242 ARRAY_ASSERT(array && (index>=0) && (index<array_size)); 00243 ARRAY_ASSERT(array == p_array); 00244 INCREMENT_ARRAY_STATISTICS_VALUE(array_element); 00245 return p_array[index]; 00246 } 00247 00253 bool resize_array(int32_t n) 00254 { 00255 INCREMENT_ARRAY_STATISTICS_VALUE(resize_array); 00256 ARRAY_ASSERT(free_array); 00257 00258 T* p= SG_REALLOC(T, array, n); 00259 array=p; 00260 if (n > array_size) 00261 memset(&array[array_size], 0, (n-array_size)*sizeof(T)); 00262 array_size=n; 00263 return true; 00264 } 00265 00272 inline T* get_array() 00273 { 00274 INCREMENT_ARRAY_STATISTICS_VALUE(get_array); 00275 return array; 00276 } 00277 00285 inline void set_array(T* p_array, int32_t p_array_size, bool p_free_array=true, 00286 bool copy_array=false) 00287 { 00288 INCREMENT_ARRAY_STATISTICS_VALUE(set_array); 00289 SG_FREE(this->array); 00290 if (copy_array) 00291 { 00292 this->array=SG_MALLOC(T, p_array_size); 00293 memcpy(this->array, p_array, p_array_size*sizeof(T)); 00294 } 00295 else 00296 this->array=p_array; 00297 this->array_size=p_array_size; 00298 this->free_array=p_free_array; 00299 } 00300 00306 inline void set_array(const T* p_array, int32_t p_array_size) 00307 { 00308 INCREMENT_ARRAY_STATISTICS_VALUE(set_array); 00309 SG_FREE(this->array); 00310 this->array=SG_MALLOC(T, p_array_size); 00311 memcpy(this->array, p_array, p_array_size*sizeof(T)); 00312 this->array_size=p_array_size; 00313 this->free_array=true; 00314 } 00315 00317 inline void clear_array() 00318 { 00319 memset(array, 0, array_size*sizeof(T)); 00320 } 00321 00322 00331 inline const T& operator[](int32_t index) const 00332 { 00333 INCREMENT_ARRAY_STATISTICS_VALUE(const_operator_overload); 00334 return array[index]; 00335 } 00336 00344 inline T& operator[](int32_t index) 00345 { 00346 INCREMENT_ARRAY_STATISTICS_VALUE(operator_overload); 00347 return element(index); 00348 } 00349 00355 CArray<T>& operator=(const CArray<T>& orig) 00356 { 00357 memcpy(array, orig.array, sizeof(T)*orig.array_size); 00358 array_size=orig.array_size; 00359 00360 return *this; 00361 } 00362 00364 void display_size() const 00365 { 00366 SG_PRINT( "Array '%s' of size: %d\n", name? name : "unnamed", 00367 array_size); 00368 } 00369 00371 void display_array() const 00372 { 00373 display_size(); 00374 for (int32_t i=0; i<array_size; i++) 00375 SG_PRINT("%1.1f,", (float32_t)array[i]); 00376 SG_PRINT("\n"); 00377 } 00378 00379 protected: 00381 T* array; 00383 int32_t array_size; 00385 bool free_array; 00387 const char* name; 00389 DECLARE_ARRAY_STATISTICS; 00390 00391 }; 00392 } 00393 #endif /* _ARRAY_H_ */