SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Array.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, Andre Noll
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef _ARRAY_H_
12 #define _ARRAY_H_
13 
14 //#define ARRAY_STATISTICS
15 
16 //#define ARRAY_ASSERT(x) {if ((x)==0) {*((int*)0)=0;}}
17 //#define ARRAY_ASSERT(x) ASSERT(x)
18 #define ARRAY_ASSERT(x)
19 
20 #include <shogun/lib/common.h>
21 #include <shogun/base/SGObject.h>
22 
23 namespace shogun
24 {
25 #ifdef ARRAY_STATISTICS
26 struct array_statistics {
27  int32_t const_element;
28  int32_t element;
29  int32_t set_element;
30  int32_t get_element;
31  int32_t operator_overload;
32  int32_t const_operator_overload;
33  int32_t set_array;
34  int32_t get_array;
35  int32_t resize_array;
36  int32_t array_element;
37 };
38 
39 #define DECLARE_ARRAY_STATISTICS struct array_statistics as
40 #define INIT_ARRAY_STATISTICS memset(&as, 0, sizeof(as))
41 #define PRINT_ARRAY_STATISTICS \
42  SG_DEBUG("access statistics:\n" \
43  "const element %i\n" \
44  "element %i\n" \
45  "set_element %i\n" \
46  "get_element %i\n" \
47  "operator_overload[] %i\n" \
48  "const_operator_overload[] %i\n" \
49  "set_array %i\n" \
50  "get_array %i\n" \
51  "resize_array %i\n" \
52  "array_element %i\n", \
53  as.const_element, \
54  as.element, \
55  as.set_element, \
56  as.get_element, \
57  as.operator_overload, \
58  as.const_operator_overload, \
59  as.set_array, \
60  as.get_array, \
61  as.resize_array, \
62  as.array_element \
63 );
64 
65 #define INCREMENT_ARRAY_STATISTICS_VALUE(_val_) ((CArray<T>*)this)->as._val_++
66 
67 #else /* ARRAY_STATISTICS */
68 #define DECLARE_ARRAY_STATISTICS
69 #define INIT_ARRAY_STATISTICS
70 #define PRINT_ARRAY_STATISTICS
71 #define INCREMENT_ARRAY_STATISTICS_VALUE(_val_)
72 #endif /* ARRAY_STATISTICS */
73 
80 template <class T> class CArray : public CSGObject
81 {
82  public:
87  CArray(int32_t initial_size = 1)
88  : CSGObject(), free_array(true), name("Array")
89  {
91  array_size = initial_size;
92  array = (T*) calloc(array_size, sizeof(T));
94  }
95 
103  CArray(T* p_array, int32_t p_array_size, bool p_free_array=true,
104  bool p_copy_array=false)
105  : CSGObject(), array(NULL), free_array(false), name("Array")
106  {
108  set_array(p_array, p_array_size, p_free_array, p_copy_array);
109  }
110 
116  CArray(const T* p_array, int32_t p_array_size)
117  : CSGObject(), array(NULL), free_array(false), name("Array")
118  {
120  set_array(p_array, p_array_size);
121  }
122 
123  virtual ~CArray()
124  {
125  //SG_DEBUG( "destroying CArray array '%s' of size %i\n", name? name : "unnamed", array_size);
127  SG_FREE(array);
128  }
129 
134  inline virtual const char* get_name() const { return "Array"; }
135 
140  inline virtual const char* get_array_name() const { return name; }
141 
146  inline void set_array_name(const char* p_name)
147  {
148  name = p_name;
149  }
150 
155  inline int32_t get_array_size() const
156  {
157  return array_size;
158  }
159 
164  inline int32_t get_dim1()
165  {
166  return array_size;
167  }
168 
170  inline void zero()
171  {
172  for (int32_t i=0; i< array_size; i++)
173  array[i]=0;
174  }
175 
177  inline void set_const(T const_elem)
178  {
179  for (int32_t i=0; i< array_size; i++)
180  array[i]=const_elem ;
181  }
182 
188  inline const T& get_element(int32_t index) const
189  {
190  ARRAY_ASSERT(array && (index>=0) && (index<array_size));
192  return array[index];
193  }
194 
201  inline bool set_element(const T& p_element, int32_t index)
202  {
203  ARRAY_ASSERT(array && (index>=0) && (index<array_size));
205  array[index]=p_element;
206  return true;
207  }
208 
214  inline const T& element(int32_t idx1) const
215  {
216  INCREMENT_ARRAY_STATISTICS_VALUE(const_element);
217  return get_element(idx1);
218  }
219 
225  inline T& element(int32_t index)
226  {
228  ARRAY_ASSERT(index>=0);
229  ARRAY_ASSERT(index<array_size);
231  return array[index];
232  }
233 
240  inline T& element(T* p_array, int32_t index)
241  {
242  ARRAY_ASSERT(array && (index>=0) && (index<array_size));
243  ARRAY_ASSERT(array == p_array);
244  INCREMENT_ARRAY_STATISTICS_VALUE(array_element);
245  return p_array[index];
246  }
247 
253  bool resize_array(int32_t n)
254  {
257 
258  T* p= SG_REALLOC(T, array, n);
259  array=p;
260  if (n > array_size)
261  memset(&array[array_size], 0, (n-array_size)*sizeof(T));
262  array_size=n;
263  return true;
264  }
265 
272  inline T* get_array()
273  {
275  return array;
276  }
277 
285  inline void set_array(T* p_array, int32_t p_array_size, bool p_free_array=true,
286  bool copy_array=false)
287  {
289  SG_FREE(this->array);
290  if (copy_array)
291  {
292  this->array=SG_MALLOC(T, p_array_size);
293  memcpy(this->array, p_array, p_array_size*sizeof(T));
294  }
295  else
296  this->array=p_array;
297  this->array_size=p_array_size;
298  this->free_array=p_free_array;
299  }
300 
306  inline void set_array(const T* p_array, int32_t p_array_size)
307  {
309  SG_FREE(this->array);
310  this->array=SG_MALLOC(T, p_array_size);
311  memcpy(this->array, p_array, p_array_size*sizeof(T));
312  this->array_size=p_array_size;
313  this->free_array=true;
314  }
315 
317  inline void clear_array()
318  {
319  memset(array, 0, array_size*sizeof(T));
320  }
321 
322 
331  inline const T& operator[](int32_t index) const
332  {
333  INCREMENT_ARRAY_STATISTICS_VALUE(const_operator_overload);
334  return array[index];
335  }
336 
344  inline T& operator[](int32_t index)
345  {
346  INCREMENT_ARRAY_STATISTICS_VALUE(operator_overload);
347  return element(index);
348  }
349 
356  {
357  memcpy(array, orig.array, sizeof(T)*orig.array_size);
358  array_size=orig.array_size;
359 
360  return *this;
361  }
362 
364  void display_size() const
365  {
366  SG_PRINT( "Array '%s' of size: %d\n", name? name : "unnamed",
367  array_size);
368  }
369 
371  void display_array() const
372  {
373  display_size();
374  for (int32_t i=0; i<array_size; i++)
375  SG_PRINT("%1.1f,", (float32_t)array[i]);
376  SG_PRINT("\n");
377  }
378 
379  protected:
381  T* array;
383  int32_t array_size;
387  const char* name;
390 
391 };
392 }
393 #endif /* _ARRAY_H_ */

SHOGUN Machine Learning Toolbox - Documentation