SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Array2.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg, Gunnar Raetsch
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef _ARRAY2_H_
12 #define _ARRAY2_H_
13 
14 #include <shogun/lib/common.h>
15 #include <shogun/base/SGObject.h>
16 #include <shogun/lib/Array.h>
17 
18 namespace shogun
19 {
20 template <class T> class CArray2;
21 
28 template <class T> class CArray2: public CArray<T>
29 {
30  public:
36  CArray2(int32_t dim1=1, int32_t dim2=1)
37  : CArray<T>(dim1*dim2), dim1_size(dim1), dim2_size(dim2)
38  {
39  }
40 
49  CArray2(T* p_array, int32_t dim1, int32_t dim2, bool p_free_array=true, bool p_copy_array=false)
50  : CArray<T>(p_array, dim1*dim2, p_free_array, p_copy_array),
51  dim1_size(dim1), dim2_size(dim2)
52  {
53  }
54 
61  CArray2(const T* p_array, int32_t dim1, int32_t dim2)
62  : CArray<T>(p_array, dim1*dim2), dim1_size(dim1), dim2_size(dim2)
63  {
64  }
65 
66  virtual ~CArray2() {}
67 
73  inline void get_array_size(int32_t & dim1, int32_t & dim2)
74  {
75  dim1=dim1_size;
76  dim2=dim2_size;
77  }
78 
83  inline int32_t get_dim1() { return dim1_size; }
84 
89  inline int32_t get_dim2() { return dim2_size; }
90 
92  inline void zero() { CArray<T>::zero(); }
93 
95  inline void set_const(T const_elem)
96  {
97  CArray<T>::set_const(const_elem) ;
98  }
99 
107  inline T* get_array() { return CArray<T>::array; }
108 
113  inline void set_array_name(const char * p_name)
114  {
116  }
117 
126  inline void set_array(T* p_array, int32_t dim1, int32_t dim2, bool p_free_array=true, bool copy_array=false)
127  {
128  dim1_size=dim1;
129  dim2_size=dim2;
130  CArray<T>::set_array(p_array, dim1*dim2, p_free_array, copy_array);
131  }
132 
139  inline bool resize_array(int32_t dim1, int32_t dim2)
140  {
141  dim1_size=dim1;
142  dim2_size=dim2;
143  return CArray<T>::resize_array(dim1*dim2);
144  }
145 
152  inline const T& get_element(int32_t idx1, int32_t idx2) const
153  {
154  ARRAY_ASSERT(idx1>=0 && idx1<dim1_size);
155  ARRAY_ASSERT(idx2>=0 && idx2<dim2_size);
156  return CArray<T>::get_element(idx1+dim1_size*idx2);
157  }
158 
166  inline bool set_element(const T& p_element, int32_t idx1, int32_t idx2)
167  {
168  ARRAY_ASSERT(idx1>=0 && idx1<dim1_size);
169  ARRAY_ASSERT(idx2>=0 && idx2<dim2_size);
170  return CArray<T>::set_element(p_element, idx1+dim1_size*idx2);
171  }
172 
179  inline const T& element(int32_t idx1, int32_t idx2) const
180  {
181  return get_element(idx1,idx2);
182  }
183 
190  inline T& element(int32_t idx1, int32_t idx2)
191  {
192  ARRAY_ASSERT((idx1>=0 && idx1<dim1_size));
193  ARRAY_ASSERT((idx2>=0 && idx2<dim2_size));
194  return CArray<T>::element(idx1+dim1_size*idx2);
195  }
196 
204  inline T& element(T* p_array, int32_t idx1, int32_t idx2)
205  {
206  ARRAY_ASSERT(CArray<T>::array==p_array);
207  ARRAY_ASSERT(idx1>=0 && idx1<dim1_size);
208  ARRAY_ASSERT(idx2>=0 && idx2<dim2_size);
209  return p_array[idx1+dim1_size*idx2];
210  }
211 
220  inline T& element(T* p_array, int32_t idx1, int32_t idx2, int32_t p_dim1_size)
221  {
222  ARRAY_ASSERT(CArray<T>::array==p_array);
223  ARRAY_ASSERT(p_dim1_size==dim1_size);
224  ARRAY_ASSERT(idx1>=0 && idx1<p_dim1_size);
225  ARRAY_ASSERT(idx2>=0 && idx2<dim2_size);
226  return p_array[idx1+p_dim1_size*idx2];
227  }
228 
235  {
236  CArray<T>::operator=(orig);
237  dim1_size=orig.dim1_size;
238  dim2_size=orig.dim2_size;
239  return *this;
240  }
241 
243  void display_array() const
244  {
245  if (CArray<T>::get_name())
246  CArray<T>::SG_PRINT( "2d-Array '%s' of size: %dx%d\n", CArray<T>::get_name(), dim1_size,dim2_size);
247  else
248  CArray<T>::SG_PRINT( "2d-Array of size: %dx%d\n",dim1_size,dim2_size);
249  for (int32_t i=0; i<dim1_size; i++)
250  {
251  CArray<T>::SG_PRINT( "element(%d,:) = [ ",i);
252  for (int32_t j=0; j<dim2_size; j++)
253  CArray<T>::SG_PRINT( "%1.1f,", (float32_t) element(i,j));
254  CArray<T>::SG_PRINT( " ]\n");
255  }
256  }
257 
259  void display_size() const
260  {
261  CArray<T>::SG_PRINT( "2d-Array of size: %dx%d\n",dim1_size,dim2_size);
262  }
263 
265  inline virtual const char* get_name() { return "Array2"; }
266 
267  protected:
269  int32_t dim1_size;
271  int32_t dim2_size;
272 };
273 }
274 #endif

SHOGUN Machine Learning Toolbox - Documentation