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 00008 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #ifndef _ARRAY2_H_ 00012 #define _ARRAY2_H_ 00013 00014 #include <shogun/lib/common.h> 00015 #include <shogun/base/SGObject.h> 00016 #include <shogun/lib/Array.h> 00017 00018 namespace shogun 00019 { 00020 template <class T> class CArray2; 00021 00028 template <class T> class CArray2: public CArray<T> 00029 { 00030 public: 00036 CArray2(int32_t dim1=1, int32_t dim2=1) 00037 : CArray<T>(dim1*dim2), dim1_size(dim1), dim2_size(dim2) 00038 { 00039 } 00040 00049 CArray2(T* p_array, int32_t dim1, int32_t dim2, bool p_free_array=true, bool p_copy_array=false) 00050 : CArray<T>(p_array, dim1*dim2, p_free_array, p_copy_array), 00051 dim1_size(dim1), dim2_size(dim2) 00052 { 00053 } 00054 00061 CArray2(const T* p_array, int32_t dim1, int32_t dim2) 00062 : CArray<T>(p_array, dim1*dim2), dim1_size(dim1), dim2_size(dim2) 00063 { 00064 } 00065 00066 virtual ~CArray2() {} 00067 00073 inline void get_array_size(int32_t & dim1, int32_t & dim2) 00074 { 00075 dim1=dim1_size; 00076 dim2=dim2_size; 00077 } 00078 00083 inline int32_t get_dim1() { return dim1_size; } 00084 00089 inline int32_t get_dim2() { return dim2_size; } 00090 00092 inline void zero() { CArray<T>::zero(); } 00093 00095 inline void set_const(T const_elem) 00096 { 00097 CArray<T>::set_const(const_elem) ; 00098 } 00099 00107 inline T* get_array() { return CArray<T>::array; } 00108 00113 inline void set_array_name(const char * p_name) 00114 { 00115 CArray<T>::set_array_name(p_name); 00116 } 00117 00126 inline void set_array(T* p_array, int32_t dim1, int32_t dim2, bool p_free_array=true, bool copy_array=false) 00127 { 00128 dim1_size=dim1; 00129 dim2_size=dim2; 00130 CArray<T>::set_array(p_array, dim1*dim2, p_free_array, copy_array); 00131 } 00132 00139 inline bool resize_array(int32_t dim1, int32_t dim2) 00140 { 00141 dim1_size=dim1; 00142 dim2_size=dim2; 00143 return CArray<T>::resize_array(dim1*dim2); 00144 } 00145 00152 inline const T& get_element(int32_t idx1, int32_t idx2) const 00153 { 00154 ARRAY_ASSERT(idx1>=0 && idx1<dim1_size); 00155 ARRAY_ASSERT(idx2>=0 && idx2<dim2_size); 00156 return CArray<T>::get_element(idx1+dim1_size*idx2); 00157 } 00158 00166 inline bool set_element(const T& p_element, int32_t idx1, int32_t idx2) 00167 { 00168 ARRAY_ASSERT(idx1>=0 && idx1<dim1_size); 00169 ARRAY_ASSERT(idx2>=0 && idx2<dim2_size); 00170 return CArray<T>::set_element(p_element, idx1+dim1_size*idx2); 00171 } 00172 00179 inline const T& element(int32_t idx1, int32_t idx2) const 00180 { 00181 return get_element(idx1,idx2); 00182 } 00183 00190 inline T& element(int32_t idx1, int32_t idx2) 00191 { 00192 ARRAY_ASSERT((idx1>=0 && idx1<dim1_size)); 00193 ARRAY_ASSERT((idx2>=0 && idx2<dim2_size)); 00194 return CArray<T>::element(idx1+dim1_size*idx2); 00195 } 00196 00204 inline T& element(T* p_array, int32_t idx1, int32_t idx2) 00205 { 00206 ARRAY_ASSERT(CArray<T>::array==p_array); 00207 ARRAY_ASSERT(idx1>=0 && idx1<dim1_size); 00208 ARRAY_ASSERT(idx2>=0 && idx2<dim2_size); 00209 return p_array[idx1+dim1_size*idx2]; 00210 } 00211 00220 inline T& element(T* p_array, int32_t idx1, int32_t idx2, int32_t p_dim1_size) 00221 { 00222 ARRAY_ASSERT(CArray<T>::array==p_array); 00223 ARRAY_ASSERT(p_dim1_size==dim1_size); 00224 ARRAY_ASSERT(idx1>=0 && idx1<p_dim1_size); 00225 ARRAY_ASSERT(idx2>=0 && idx2<dim2_size); 00226 return p_array[idx1+p_dim1_size*idx2]; 00227 } 00228 00234 CArray2<T>& operator=(CArray2<T>& orig) 00235 { 00236 CArray<T>::operator=(orig); 00237 dim1_size=orig.dim1_size; 00238 dim2_size=orig.dim2_size; 00239 return *this; 00240 } 00241 00243 void display_array() const 00244 { 00245 if (CArray<T>::get_name()) 00246 CArray<T>::SG_PRINT( "2d-Array '%s' of size: %dx%d\n", CArray<T>::get_name(), dim1_size,dim2_size); 00247 else 00248 CArray<T>::SG_PRINT( "2d-Array of size: %dx%d\n",dim1_size,dim2_size); 00249 for (int32_t i=0; i<dim1_size; i++) 00250 { 00251 CArray<T>::SG_PRINT( "element(%d,:) = [ ",i); 00252 for (int32_t j=0; j<dim2_size; j++) 00253 CArray<T>::SG_PRINT( "%1.1f,", (float32_t) element(i,j)); 00254 CArray<T>::SG_PRINT( " ]\n"); 00255 } 00256 } 00257 00259 void display_size() const 00260 { 00261 CArray<T>::SG_PRINT( "2d-Array of size: %dx%d\n",dim1_size,dim2_size); 00262 } 00263 00265 inline virtual const char* get_name() { return "Array2"; } 00266 00267 protected: 00269 int32_t dim1_size; 00271 int32_t dim2_size; 00272 }; 00273 } 00274 #endif