Array2.h

Go to the documentation of this file.
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 "lib/common.h"
00015 #include "base/SGObject.h"
00016 #include "lib/Array.h"
00017 
00018 template <class T> class CArray2;
00019 
00026 template <class T> class CArray2: public CArray<T>
00027 {
00028     public:
00034         CArray2(int32_t dim1, int32_t dim2)
00035         : CArray<T>(dim1*dim2), dim1_size(dim1), dim2_size(dim2)
00036         {
00037         }
00038 
00047         CArray2(T* p_array, int32_t dim1, int32_t dim2, bool p_free_array=true, bool p_copy_array=false)
00048         : CArray<T>(p_array, dim1*dim2, p_free_array, p_copy_array),
00049             dim1_size(dim1), dim2_size(dim2)
00050         {
00051         }
00052 
00059         CArray2(const T* p_array, int32_t dim1, int32_t dim2)
00060         : CArray<T>(p_array, dim1*dim2), dim1_size(dim1), dim2_size(dim2)
00061         {
00062         }
00063 
00064         virtual ~CArray2() {}
00065 
00071         inline void get_array_size(int32_t & dim1, int32_t & dim2)
00072         {
00073             dim1=dim1_size;
00074             dim2=dim2_size;
00075         }
00076 
00081         inline int32_t get_dim1() { return dim1_size; }
00082 
00087         inline int32_t get_dim2() { return dim2_size; }
00088 
00090         inline void zero() { CArray<T>::zero(); }
00091 
00099         inline T* get_array() { return CArray<T>::array; }
00100 
00105         inline void set_name(const char * p_name)
00106         {
00107             CArray<T>::set_name(p_name);
00108         }
00109 
00118         inline void set_array(T* p_array, int32_t dim1, int32_t dim2, bool p_free_array=true, bool copy_array=false)
00119         {
00120             dim1_size=dim1;
00121             dim2_size=dim2;
00122             CArray<T>::set_array(p_array, dim1*dim2, p_free_array, copy_array);
00123         }
00124 
00131         inline bool resize_array(int32_t dim1, int32_t dim2)
00132         {
00133             dim1_size=dim1;
00134             dim2_size=dim2;
00135             return CArray<T>::resize_array(dim1*dim2);
00136         }
00137 
00144         inline const T& get_element(int32_t idx1, int32_t idx2) const
00145         {
00146             ARRAY_ASSERT(idx1>=0 && idx1<dim1_size);
00147             ARRAY_ASSERT(idx2>=0 && idx2<dim2_size);
00148             return CArray<T>::get_element(idx1+dim1_size*idx2);
00149         }
00150 
00158         inline bool set_element(const T& p_element, int32_t idx1, int32_t idx2)
00159         {
00160             ARRAY_ASSERT(idx1>=0 && idx1<dim1_size);
00161             ARRAY_ASSERT(idx2>=0 && idx2<dim2_size);
00162             return CArray<T>::set_element(p_element, idx1+dim1_size*idx2);
00163         }
00164 
00171         inline const T& element(int32_t idx1, int32_t idx2) const
00172         {
00173             return get_element(idx1,idx2);
00174         }
00175 
00182         inline T& element(int32_t idx1, int32_t idx2)
00183         {
00184             ARRAY_ASSERT(idx1>=0 && idx1<dim1_size);
00185             ARRAY_ASSERT(idx2>=0 && idx2<dim2_size);
00186             return CArray<T>::element(idx1+dim1_size*idx2);
00187         }
00188 
00196         inline T& element(T* p_array, int32_t idx1, int32_t idx2)
00197         {
00198             ARRAY_ASSERT(CArray<T>::array==p_array);
00199             ARRAY_ASSERT(idx1>=0 && idx1<dim1_size);
00200             ARRAY_ASSERT(idx2>=0 && idx2<dim2_size);
00201             return p_array[idx1+dim1_size*idx2];
00202         }
00203 
00212         inline T& element(T* p_array, int32_t idx1, int32_t idx2, int32_t p_dim1_size) 
00213         {
00214             ARRAY_ASSERT(CArray<T>::array==p_array);
00215             ARRAY_ASSERT(p_dim1_size==dim1_size);
00216             ARRAY_ASSERT(idx1>=0 && idx1<p_dim1_size);
00217             ARRAY_ASSERT(idx2>=0 && idx2<dim2_size);
00218             return p_array[idx1+p_dim1_size*idx2];
00219         }
00220 
00226         CArray2<T>& operator=(CArray2<T>& orig)
00227         {
00228             CArray<T>::operator=(orig);
00229             dim1_size=orig.dim1_size;
00230             dim2_size=orig.dim2_size;
00231             return *this;
00232         }
00233 
00235         void display_array() const
00236         {
00237             if (CArray<T>::get_name())
00238                 CArray<T>::SG_PRINT( "2d-Array '%s' of size: %dx%d\n", CArray<T>::get_name(), dim1_size,dim2_size);
00239             else
00240                 CArray<T>::SG_PRINT( "2d-Array of size: %dx%d\n",dim1_size,dim2_size);
00241             for (int32_t i=0; i<dim1_size; i++)
00242             {
00243                 CArray<T>::SG_PRINT( "element(%d,:) = [ ",i);
00244                 for (int32_t j=0; j<dim2_size; j++)
00245                     CArray<T>::SG_PRINT( "%1.1f,", (float32_t) element(i,j));
00246                 CArray<T>::SG_PRINT( " ]\n");
00247             }
00248         }
00249 
00251         void display_size() const
00252         {
00253             CArray<T>::SG_PRINT( "2d-Array of size: %dx%d\n",dim1_size,dim2_size);
00254         }
00255 
00257         inline virtual const char* get_name() { return "Array2"; }
00258 
00259     protected:
00261         int32_t dim1_size;
00263         int32_t dim2_size;
00264 };
00265 #endif

SHOGUN Machine Learning Toolbox - Documentation