00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef _DYNAMIC_ARRAY_H_
00012 #define _DYNAMIC_ARRAY_H_
00013
00014 #include <shogun/base/SGObject.h>
00015 #include <shogun/base/DynArray.h>
00016 #include <shogun/base/Parameter.h>
00017
00018 namespace shogun
00019 {
00028 template <class T> class CDynamicArray :public CSGObject
00029 {
00030 public:
00032 CDynamicArray()
00033 : CSGObject(), m_array(), name("Array")
00034 {
00035 set_generic<T>();
00036
00037 m_parameters->add_vector(&m_array.array, &m_array.num_elements, "array",
00038 "Memory for dynamic array.");
00039
00040 SG_ADD(&m_array.last_element_idx,
00041 "last_element_idx",
00042 "Element with largest index.", MS_NOT_AVAILABLE);
00043 SG_ADD(&m_array.resize_granularity,
00044 "resize_granularity",
00045 "shrink/grow step size.", MS_NOT_AVAILABLE);
00046
00047 dim1_size=1;
00048 dim2_size=1;
00049 dim3_size=1;
00050 }
00051
00058 CDynamicArray(int32_t p_dim1_size, int32_t p_dim2_size=1, int32_t p_dim3_size=1)
00059 : CSGObject(), m_array(p_dim1_size*p_dim2_size*p_dim3_size), name("Array")
00060 {
00061 set_generic<T>();
00062
00063 m_parameters->add_vector(&m_array.array,
00064 &m_array.num_elements, "array",
00065 "Memory for dynamic array.");
00066 m_parameters->add(&m_array.last_element_idx,
00067 "last_element_idx",
00068 "Element with largest index.");
00069 m_parameters->add(&m_array.resize_granularity,
00070 "resize_granularity",
00071 "shrink/grow step size.");
00072
00073 dim1_size=p_dim1_size;
00074 dim2_size=p_dim2_size;
00075 dim3_size=p_dim3_size;
00076 }
00077
00085 CDynamicArray(T* p_array, int32_t p_dim1_size, bool p_free_array, bool p_copy_array)
00086 : CSGObject(), m_array(p_array, p_dim1_size, p_free_array, p_copy_array), name("Array")
00087 {
00088 set_generic<T>();
00089
00090 m_parameters->add_vector(&m_array.array, &m_array.num_elements, "array",
00091 "Memory for dynamic array.");
00092
00093 SG_ADD(&m_array.last_element_idx,
00094 "last_element_idx",
00095 "Element with largest index.", MS_NOT_AVAILABLE);
00096 SG_ADD(&m_array.resize_granularity,
00097 "resize_granularity",
00098 "shrink/grow step size.", MS_NOT_AVAILABLE);
00099 dim1_size=p_dim1_size;
00100 dim2_size=1;
00101 dim3_size=1;
00102 }
00103
00112 CDynamicArray(T* p_array, int32_t p_dim1_size, int32_t p_dim2_size,
00113 bool p_free_array, bool p_copy_array)
00114 : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size, p_free_array, p_copy_array), name("Array")
00115 {
00116 set_generic<T>();
00117
00118 m_parameters->add_vector(&m_array.array, &m_array.num_elements, "array",
00119 "Memory for dynamic array.");
00120
00121 SG_ADD(&m_array.last_element_idx,
00122 "last_element_idx",
00123 "Element with largest index.", MS_NOT_AVAILABLE);
00124 SG_ADD(&m_array.resize_granularity,
00125 "resize_granularity",
00126 "shrink/grow step size.", MS_NOT_AVAILABLE);
00127
00128 dim1_size=p_dim1_size;
00129 dim2_size=p_dim2_size;
00130 dim3_size=1;
00131 }
00132
00142 CDynamicArray(T* p_array, int32_t p_dim1_size, int32_t p_dim2_size,
00143 int32_t p_dim3_size, bool p_free_array, bool p_copy_array)
00144 : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size, p_free_array, p_copy_array), name("Array")
00145 {
00146 set_generic<T>();
00147
00148 m_parameters->add_vector(&m_array.array, &m_array.num_elements, "array",
00149 "Memory for dynamic array.");
00150
00151 SG_ADD(&m_array.last_element_idx,
00152 "last_element_idx",
00153 "Element with largest index.", MS_NOT_AVAILABLE);
00154 SG_ADD(&m_array.resize_granularity,
00155 "resize_granularity",
00156 "shrink/grow step size.", MS_NOT_AVAILABLE);
00157
00158 dim1_size=p_dim1_size;
00159 dim2_size=p_dim2_size;
00160 dim3_size=p_dim3_size;
00161 }
00162
00170 CDynamicArray(const T* p_array, int32_t p_dim1_size=1, int32_t p_dim2_size=1, int32_t p_dim3_size=1)
00171 : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size), name("Array")
00172 {
00173 set_generic<T>();
00174
00175 m_parameters->add_vector(&m_array.array, &m_array.num_elements, "array",
00176 "Memory for dynamic array.");
00177
00178 SG_ADD(&m_array.last_element_idx,
00179 "last_element_idx",
00180 "Element with largest index.", MS_NOT_AVAILABLE);
00181 SG_ADD(&m_array.resize_granularity,
00182 "resize_granularity",
00183 "shrink/grow step size.", MS_NOT_AVAILABLE);
00184
00185 dim1_size=p_dim1_size;
00186 dim2_size=p_dim2_size;
00187 dim3_size=p_dim3_size;
00188 }
00189
00190 virtual ~CDynamicArray() {}
00191
00197 inline int32_t set_granularity(int32_t g)
00198 {
00199 return m_array.set_granularity(g);
00200 }
00201
00206 inline int32_t get_array_size()
00207 {
00208 return m_array.get_array_size();
00209 }
00210
00216 inline void get_array_size(int32_t& dim1, int32_t& dim2)
00217 {
00218 dim1=dim1_size;
00219 dim2=dim2_size;
00220 }
00221
00228 inline void get_array_size(int32_t& dim1, int32_t& dim2, int32_t& dim3)
00229 {
00230 dim1=dim1_size;
00231 dim2=dim2_size;
00232 dim3=dim3_size;
00233 }
00234
00239 inline int32_t get_dim1() { return dim1_size; }
00240
00245 inline int32_t get_dim2() { return dim2_size; }
00246
00251 inline int32_t get_dim3() { return dim3_size; }
00252
00257 inline int32_t get_num_elements() const
00258 {
00259 return m_array.get_num_elements();
00260 }
00261
00269 inline const T& get_element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
00270 {
00271 return m_array.get_array()[idx1+dim1_size*(idx2+dim2_size*idx3)];
00272 }
00273
00281 inline const T& element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
00282 {
00283 return get_element(idx1, idx2, idx3);
00284 }
00285
00293 inline T& element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
00294 {
00295 return m_array.get_array()[idx1+dim1_size*(idx2+dim2_size*idx3)];
00296 }
00297
00306 inline T& element(T* p_array, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
00307 {
00308 ASSERT(idx1>=0 && idx1<dim1_size);
00309 ASSERT(idx2>=0 && idx2<dim2_size);
00310 ASSERT(idx3>=0 && idx3<dim3_size);
00311 return p_array[idx1+dim1_size*(idx2+dim2_size*idx3)];
00312 }
00313
00324 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)
00325 {
00326 ASSERT(p_dim1_size==dim1_size);
00327 ASSERT(p_dim2_size==dim2_size);
00328 ASSERT(idx1>=0 && idx1<p_dim1_size);
00329 ASSERT(idx2>=0 && idx2<p_dim2_size);
00330 ASSERT(idx3>=0 && idx3<dim3_size);
00331 return p_array[idx1+p_dim1_size*(idx2+p_dim2_size*idx3)];
00332 }
00333
00338 inline T get_last_element() const
00339 {
00340 return m_array.get_last_element();
00341 }
00342
00350 inline T get_element_safe(int32_t index) const
00351 {
00352 return m_array.get_element_safe(index);
00353 }
00354
00363 inline bool set_element(T e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
00364 {
00365 return m_array.set_element(e, idx1+dim1_size*(idx2+dim2_size*idx3));
00366 }
00367
00374 inline bool insert_element(T e, int32_t index)
00375 {
00376 return m_array.insert_element(e, index);
00377 }
00378
00384 inline bool append_element(T e)
00385 {
00386 return m_array.append_element(e);
00387 }
00388
00394 inline void push_back(T e)
00395 { m_array.push_back(e); }
00396
00400 inline void pop_back()
00401 {
00402 m_array.pop_back();
00403 }
00404
00410 inline T back()
00411 {
00412 return m_array.back();
00413 }
00414
00421 inline int32_t find_element(T e)
00422 {
00423 return m_array.find_element(e);
00424 }
00425
00432 inline bool delete_element(int32_t idx)
00433 {
00434 return m_array.delete_element(idx);
00435 }
00436
00444 inline bool resize_array(int32_t ndim1, int32_t ndim2=1, int32_t ndim3=1)
00445 {
00446 dim1_size=ndim1;
00447 dim2_size=ndim2;
00448 dim3_size=ndim3;
00449 return m_array.resize_array(ndim1*ndim2*ndim3);
00450 }
00451
00453 void set_const(const T& const_element)
00454 {
00455 m_array.set_const(const_element);
00456 }
00457
00465 inline T* get_array() const
00466 {
00467 return m_array.get_array();
00468 }
00469
00476 inline void set_array(T* p_array, int32_t p_num_elements,
00477 int32_t array_size)
00478 {
00479 m_array.set_array(p_array, p_num_elements, array_size);
00480 }
00481
00489 inline void set_array(T* p_array, int32_t dim1,
00490 bool p_free_array, bool copy_array)
00491 {
00492 dim1_size=dim1;
00493 dim2_size=1;
00494 dim3_size=1;
00495 m_array.set_array(p_array, dim1, dim1, p_free_array, copy_array);
00496 }
00497
00506 inline void set_array(T* p_array, int32_t dim1,
00507 int32_t dim2, bool p_free_array, bool copy_array)
00508 {
00509 dim1_size=dim1;
00510 dim2_size=dim2;
00511 dim3_size=1;
00512
00513 m_array.set_array(p_array, dim1*dim2, dim1*dim2, p_free_array, copy_array);
00514 }
00515
00525 inline void set_array(T* p_array, int32_t dim1,
00526 int32_t dim2, int32_t dim3, bool p_free_array, bool copy_array)
00527 {
00528 dim1_size=dim1;
00529 dim2_size=dim2;
00530 dim3_size=dim3;
00531 m_array.set_array(p_array, dim1*dim2*dim3, dim1*dim2*dim3, p_free_array, copy_array);
00532 }
00533
00539 inline void set_array(const T* p_array, int32_t p_size)
00540 {
00541 m_array.set_array(p_array, p_size, p_size);
00542 }
00543
00545 inline void clear_array()
00546 {
00547 m_array.clear_array();
00548 }
00549
00559 inline const T& operator[](int32_t index) const
00560 {
00561 return get_element(index);
00562 }
00563
00571 inline T& operator[](int32_t index)
00572 {
00573 return element(index);
00574 }
00575
00581 inline CDynamicArray<T>& operator=(CDynamicArray<T>& orig)
00582 {
00583 m_array=orig.m_array;
00584 dim1_size=orig.dim1_size;
00585 dim2_size=orig.dim2_size;
00586 dim3_size=orig.dim3_size;
00587
00588 return *this;
00589 }
00590
00592 inline void shuffle() { m_array.shuffle(); }
00593
00598 inline void set_array_name(const char* p_name)
00599 {
00600 name=p_name;
00601 }
00602
00607 inline const char* get_array_name() const { return name; }
00608
00610 inline void display_array()
00611 {
00612 if (get_name())
00613 SG_PRINT( "DynamicArray '%s' of size: %dx%dx%d\n", get_name(), dim1_size, dim2_size, dim3_size);
00614 else
00615 SG_PRINT( "DynamicArray of size: %dx%dx%d\n",dim1_size, dim2_size, dim3_size);
00616
00617 for (int32_t k=0; k<dim3_size; k++)
00618 for (int32_t i=0; i<dim1_size; i++)
00619 {
00620 SG_PRINT( "element(%d,:,%d) = [ ",i, k);
00621 for (int32_t j=0; j<dim2_size; j++)
00622 SG_PRINT( "%1.1f,", (float32_t) element(i,j,k));
00623 SG_PRINT( " ]\n");
00624 }
00625 }
00626
00628 inline void display_size()
00629 {
00630 SG_PRINT( "DynamicArray of size: %dx%dx%d\n",dim1_size, dim2_size, dim3_size);
00631 }
00632
00634 virtual const char* get_name() const
00635 {
00636 return "DynamicArray";
00637 }
00638
00639 protected:
00640
00642 DynArray<T> m_array;
00643
00645 int32_t dim1_size;
00646
00648 int32_t dim2_size;
00649
00651 int32_t dim3_size;
00652
00654 const char* name;
00655 };
00656 }
00657 #endif