SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DynamicArray.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
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef _DYNAMIC_ARRAY_H_
12 #define _DYNAMIC_ARRAY_H_
13 
14 #include <shogun/base/SGObject.h>
15 #include <shogun/base/DynArray.h>
16 #include <shogun/base/Parameter.h>
17 
18 namespace shogun
19 {
28 template <class T> class CDynamicArray :public CSGObject
29 {
30  public:
33  : CSGObject(), m_array(), name("Array")
34  {
35  set_generic<T>();
36 
37  m_parameters->add_vector(&m_array.array, &m_array.num_elements, "array",
38  "Memory for dynamic array.");
39 
40  SG_ADD(&m_array.last_element_idx,
41  "last_element_idx",
42  "Element with largest index.", MS_NOT_AVAILABLE);
43  SG_ADD(&m_array.resize_granularity,
44  "resize_granularity",
45  "shrink/grow step size.", MS_NOT_AVAILABLE);
46 
47  dim1_size=1;
48  dim2_size=1;
49  dim3_size=1;
50  }
51 
58  CDynamicArray(int32_t p_dim1_size, int32_t p_dim2_size=1, int32_t p_dim3_size=1)
59  : CSGObject(), m_array(p_dim1_size*p_dim2_size*p_dim3_size), name("Array")
60  {
61  set_generic<T>();
62 
64  &m_array.num_elements, "array",
65  "Memory for dynamic array.");
66  m_parameters->add(&m_array.last_element_idx,
67  "last_element_idx",
68  "Element with largest index.");
69  m_parameters->add(&m_array.resize_granularity,
70  "resize_granularity",
71  "shrink/grow step size.");
72 
73  dim1_size=p_dim1_size;
74  dim2_size=p_dim2_size;
75  dim3_size=p_dim3_size;
76  }
77 
85  CDynamicArray(T* p_array, int32_t p_dim1_size, bool p_free_array, bool p_copy_array)
86  : CSGObject(), m_array(p_array, p_dim1_size, p_free_array, p_copy_array), name("Array")
87  {
88  set_generic<T>();
89 
90  m_parameters->add_vector(&m_array.array, &m_array.num_elements, "array",
91  "Memory for dynamic array.");
92 
93  SG_ADD(&m_array.last_element_idx,
94  "last_element_idx",
95  "Element with largest index.", MS_NOT_AVAILABLE);
96  SG_ADD(&m_array.resize_granularity,
97  "resize_granularity",
98  "shrink/grow step size.", MS_NOT_AVAILABLE);
99  dim1_size=p_dim1_size;
100  dim2_size=1;
101  dim3_size=1;
102  }
103 
112  CDynamicArray(T* p_array, int32_t p_dim1_size, int32_t p_dim2_size,
113  bool p_free_array, bool p_copy_array)
114  : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size, p_free_array, p_copy_array), name("Array")
115  {
116  set_generic<T>();
117 
118  m_parameters->add_vector(&m_array.array, &m_array.num_elements, "array",
119  "Memory for dynamic array.");
120 
121  SG_ADD(&m_array.last_element_idx,
122  "last_element_idx",
123  "Element with largest index.", MS_NOT_AVAILABLE);
124  SG_ADD(&m_array.resize_granularity,
125  "resize_granularity",
126  "shrink/grow step size.", MS_NOT_AVAILABLE);
127 
128  dim1_size=p_dim1_size;
129  dim2_size=p_dim2_size;
130  dim3_size=1;
131  }
132 
142  CDynamicArray(T* p_array, int32_t p_dim1_size, int32_t p_dim2_size,
143  int32_t p_dim3_size, bool p_free_array, bool p_copy_array)
144  : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size, p_free_array, p_copy_array), name("Array")
145  {
146  set_generic<T>();
147 
148  m_parameters->add_vector(&m_array.array, &m_array.num_elements, "array",
149  "Memory for dynamic array.");
150 
151  SG_ADD(&m_array.last_element_idx,
152  "last_element_idx",
153  "Element with largest index.", MS_NOT_AVAILABLE);
154  SG_ADD(&m_array.resize_granularity,
155  "resize_granularity",
156  "shrink/grow step size.", MS_NOT_AVAILABLE);
157 
158  dim1_size=p_dim1_size;
159  dim2_size=p_dim2_size;
160  dim3_size=p_dim3_size;
161  }
162 
170  CDynamicArray(const T* p_array, int32_t p_dim1_size=1, int32_t p_dim2_size=1, int32_t p_dim3_size=1)
171  : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size), name("Array")
172  {
173  set_generic<T>();
174 
175  m_parameters->add_vector(&m_array.array, &m_array.num_elements, "array",
176  "Memory for dynamic array.");
177 
178  SG_ADD(&m_array.last_element_idx,
179  "last_element_idx",
180  "Element with largest index.", MS_NOT_AVAILABLE);
181  SG_ADD(&m_array.resize_granularity,
182  "resize_granularity",
183  "shrink/grow step size.", MS_NOT_AVAILABLE);
184 
185  dim1_size=p_dim1_size;
186  dim2_size=p_dim2_size;
187  dim3_size=p_dim3_size;
188  }
189 
190  virtual ~CDynamicArray() {}
191 
197  inline int32_t set_granularity(int32_t g)
198  {
199  return m_array.set_granularity(g);
200  }
201 
206  inline int32_t get_array_size()
207  {
208  return m_array.get_array_size();
209  }
210 
216  inline void get_array_size(int32_t& dim1, int32_t& dim2)
217  {
218  dim1=dim1_size;
219  dim2=dim2_size;
220  }
221 
228  inline void get_array_size(int32_t& dim1, int32_t& dim2, int32_t& dim3)
229  {
230  dim1=dim1_size;
231  dim2=dim2_size;
232  dim3=dim3_size;
233  }
234 
239  inline int32_t get_dim1() { return dim1_size; }
240 
245  inline int32_t get_dim2() { return dim2_size; }
246 
251  inline int32_t get_dim3() { return dim3_size; }
252 
257  inline int32_t get_num_elements() const
258  {
259  return m_array.get_num_elements();
260  }
261 
269  inline const T& get_element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
270  {
271  return m_array.get_array()[idx1+dim1_size*(idx2+dim2_size*idx3)];
272  }
273 
281  inline const T& element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
282  {
283  return get_element(idx1, idx2, idx3);
284  }
285 
293  inline T& element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
294  {
295  return m_array.get_array()[idx1+dim1_size*(idx2+dim2_size*idx3)];
296  }
297 
306  inline T& element(T* p_array, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
307  {
308  ASSERT(idx1>=0 && idx1<dim1_size);
309  ASSERT(idx2>=0 && idx2<dim2_size);
310  ASSERT(idx3>=0 && idx3<dim3_size);
311  return p_array[idx1+dim1_size*(idx2+dim2_size*idx3)];
312  }
313 
324  inline T& element(T* p_array, int32_t idx1, int32_t idx2, int32_t idx3, int32_t p_dim1_size, int32_t p_dim2_size)
325  {
326  ASSERT(p_dim1_size==dim1_size);
327  ASSERT(p_dim2_size==dim2_size);
328  ASSERT(idx1>=0 && idx1<p_dim1_size);
329  ASSERT(idx2>=0 && idx2<p_dim2_size);
330  ASSERT(idx3>=0 && idx3<dim3_size);
331  return p_array[idx1+p_dim1_size*(idx2+p_dim2_size*idx3)];
332  }
333 
338  inline T get_last_element() const
339  {
340  return m_array.get_last_element();
341  }
342 
350  inline T get_element_safe(int32_t index) const
351  {
352  return m_array.get_element_safe(index);
353  }
354 
363  inline bool set_element(T e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
364  {
365  return m_array.set_element(e, idx1+dim1_size*(idx2+dim2_size*idx3));
366  }
367 
374  inline bool insert_element(T e, int32_t index)
375  {
376  return m_array.insert_element(e, index);
377  }
378 
384  inline bool append_element(T e)
385  {
386  return m_array.append_element(e);
387  }
388 
394  inline void push_back(T e)
395  { m_array.push_back(e); }
396 
400  inline void pop_back()
401  {
402  m_array.pop_back();
403  }
404 
410  inline T back()
411  {
412  return m_array.back();
413  }
414 
421  inline int32_t find_element(T e)
422  {
423  return m_array.find_element(e);
424  }
425 
432  inline bool delete_element(int32_t idx)
433  {
434  return m_array.delete_element(idx);
435  }
436 
444  inline bool resize_array(int32_t ndim1, int32_t ndim2=1, int32_t ndim3=1)
445  {
446  dim1_size=ndim1;
447  dim2_size=ndim2;
448  dim3_size=ndim3;
449  return m_array.resize_array(ndim1*ndim2*ndim3);
450  }
451 
453  void set_const(const T& const_element)
454  {
455  m_array.set_const(const_element);
456  }
457 
465  inline T* get_array() const
466  {
467  return m_array.get_array();
468  }
469 
476  inline void set_array(T* p_array, int32_t p_num_elements,
477  int32_t array_size)
478  {
479  m_array.set_array(p_array, p_num_elements, array_size);
480  }
481 
489  inline void set_array(T* p_array, int32_t dim1,
490  bool p_free_array, bool copy_array)
491  {
492  dim1_size=dim1;
493  dim2_size=1;
494  dim3_size=1;
495  m_array.set_array(p_array, dim1, dim1, p_free_array, copy_array);
496  }
497 
506  inline void set_array(T* p_array, int32_t dim1,
507  int32_t dim2, bool p_free_array, bool copy_array)
508  {
509  dim1_size=dim1;
510  dim2_size=dim2;
511  dim3_size=1;
512 
513  m_array.set_array(p_array, dim1*dim2, dim1*dim2, p_free_array, copy_array);
514  }
515 
525  inline void set_array(T* p_array, int32_t dim1,
526  int32_t dim2, int32_t dim3, bool p_free_array, bool copy_array)
527  {
528  dim1_size=dim1;
529  dim2_size=dim2;
530  dim3_size=dim3;
531  m_array.set_array(p_array, dim1*dim2*dim3, dim1*dim2*dim3, p_free_array, copy_array);
532  }
533 
539  inline void set_array(const T* p_array, int32_t p_size)
540  {
541  m_array.set_array(p_array, p_size, p_size);
542  }
543 
545  inline void clear_array()
546  {
547  m_array.clear_array();
548  }
549 
559  inline const T& operator[](int32_t index) const
560  {
561  return get_element(index);
562  }
563 
571  inline T& operator[](int32_t index)
572  {
573  return element(index);
574  }
575 
582  {
583  m_array=orig.m_array;
584  dim1_size=orig.dim1_size;
585  dim2_size=orig.dim2_size;
586  dim3_size=orig.dim3_size;
587 
588  return *this;
589  }
590 
592  inline void shuffle() { m_array.shuffle(); }
593 
598  inline void set_array_name(const char* p_name)
599  {
600  name=p_name;
601  }
602 
607  inline const char* get_array_name() const { return name; }
608 
610  inline void display_array()
611  {
612  if (get_name())
613  SG_PRINT( "DynamicArray '%s' of size: %dx%dx%d\n", get_name(), dim1_size, dim2_size, dim3_size);
614  else
615  SG_PRINT( "DynamicArray of size: %dx%dx%d\n",dim1_size, dim2_size, dim3_size);
616 
617  for (int32_t k=0; k<dim3_size; k++)
618  for (int32_t i=0; i<dim1_size; i++)
619  {
620  SG_PRINT( "element(%d,:,%d) = [ ",i, k);
621  for (int32_t j=0; j<dim2_size; j++)
622  SG_PRINT( "%1.1f,", (float32_t) element(i,j,k));
623  SG_PRINT( " ]\n");
624  }
625  }
626 
628  inline void display_size()
629  {
630  SG_PRINT( "DynamicArray of size: %dx%dx%d\n",dim1_size, dim2_size, dim3_size);
631  }
632 
634  inline virtual const char* get_name() const
635  {
636  return "DynamicArray";
637  }
638 
639  protected:
640 
643 
645  int32_t dim1_size;
646 
648  int32_t dim2_size;
649 
651  int32_t dim3_size;
652 
654  const char* name;
655 };
656 }
657 #endif /* _DYNAMIC_ARRAY_H_ */

SHOGUN Machine Learning Toolbox - Documentation