SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DynamicArray.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
00008  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
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             dim1_size=1;
00036             dim2_size=1;
00037             dim3_size=1;
00038 
00039             init();
00040         }
00041 
00048         CDynamicArray(int32_t p_dim1_size, int32_t p_dim2_size=1, int32_t p_dim3_size=1)
00049         : CSGObject(), m_array(p_dim1_size*p_dim2_size*p_dim3_size), name("Array")
00050         {
00051             dim1_size=p_dim1_size;
00052             dim2_size=p_dim2_size;
00053             dim3_size=p_dim3_size;
00054 
00055             init();
00056         }
00057 
00065         CDynamicArray(T* p_array, int32_t p_dim1_size, bool p_free_array, bool p_copy_array)
00066         : CSGObject(), m_array(p_array, p_dim1_size, p_free_array, p_copy_array), name("Array")
00067         {
00068             dim1_size=p_dim1_size;
00069             dim2_size=1;
00070             dim3_size=1;
00071 
00072             init();
00073         }
00074 
00083         CDynamicArray(T* p_array, int32_t p_dim1_size, int32_t p_dim2_size,
00084                         bool p_free_array, bool p_copy_array)
00085         : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size, p_free_array, p_copy_array), name("Array")
00086         {
00087             dim1_size=p_dim1_size;
00088             dim2_size=p_dim2_size;
00089             dim3_size=1;
00090 
00091             init();
00092         }
00093 
00103         CDynamicArray(T* p_array, int32_t p_dim1_size, int32_t p_dim2_size,
00104                         int32_t p_dim3_size, bool p_free_array, bool p_copy_array)
00105         : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size, p_free_array, p_copy_array), name("Array")
00106         {
00107             dim1_size=p_dim1_size;
00108             dim2_size=p_dim2_size;
00109             dim3_size=p_dim3_size;
00110 
00111             init();
00112         }
00113 
00121         CDynamicArray(const T* p_array, int32_t p_dim1_size=1, int32_t p_dim2_size=1, int32_t p_dim3_size=1)
00122         : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size), name("Array")
00123         {
00124             dim1_size=p_dim1_size;
00125             dim2_size=p_dim2_size;
00126             dim3_size=p_dim3_size;
00127 
00128             init();
00129         }
00130 
00131         virtual ~CDynamicArray() {}
00132 
00138         inline int32_t set_granularity(int32_t g)
00139         {
00140             return m_array.set_granularity(g);
00141         }
00142 
00147         inline int32_t get_array_size()
00148         {
00149             return m_array.get_array_size();
00150         }
00151 
00157         inline void get_array_size(int32_t& dim1, int32_t& dim2)
00158         {
00159             dim1=dim1_size;
00160             dim2=dim2_size;
00161         }
00162 
00169         inline void get_array_size(int32_t& dim1, int32_t& dim2, int32_t& dim3)
00170         {
00171             dim1=dim1_size;
00172             dim2=dim2_size;
00173             dim3=dim3_size;
00174         }
00175 
00180         inline int32_t get_dim1() { return dim1_size; }
00181 
00186         inline int32_t get_dim2() { return dim2_size; }
00187 
00192         inline int32_t get_dim3() { return dim3_size; }
00193 
00198         inline int32_t get_num_elements() const
00199         {
00200             return m_array.get_num_elements();
00201         }
00202 
00210         inline const T& get_element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
00211         {
00212             return m_array.get_array()[idx1+dim1_size*(idx2+dim2_size*idx3)];
00213         }
00214 
00222         inline const T& element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
00223         {
00224             return get_element(idx1, idx2, idx3);
00225         }
00226 
00234         inline T& element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
00235         {
00236             return m_array.get_array()[idx1+dim1_size*(idx2+dim2_size*idx3)];
00237         }
00238 
00247         inline T& element(T* p_array, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
00248         {
00249             ASSERT(idx1>=0 && idx1<dim1_size)
00250             ASSERT(idx2>=0 && idx2<dim2_size)
00251             ASSERT(idx3>=0 && idx3<dim3_size)
00252             return p_array[idx1+dim1_size*(idx2+dim2_size*idx3)];
00253         }
00254 
00265         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)
00266         {
00267             ASSERT(p_dim1_size==dim1_size)
00268             ASSERT(p_dim2_size==dim2_size)
00269             ASSERT(idx1>=0 && idx1<p_dim1_size)
00270             ASSERT(idx2>=0 && idx2<p_dim2_size)
00271             ASSERT(idx3>=0 && idx3<dim3_size)
00272             return p_array[idx1+p_dim1_size*(idx2+p_dim2_size*idx3)];
00273         }
00274 
00279         inline T get_last_element() const
00280         {
00281             return m_array.get_last_element();
00282         }
00283 
00291         inline T get_element_safe(int32_t index) const
00292         {
00293             return m_array.get_element_safe(index);
00294         }
00295 
00304         inline bool set_element(T e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
00305         {
00306             return m_array.set_element(e, idx1+dim1_size*(idx2+dim2_size*idx3));
00307         }
00308 
00315         inline bool insert_element(T e, int32_t index)
00316         {
00317             return m_array.insert_element(e, index);
00318         }
00319 
00325         inline bool append_element(T e)
00326         {
00327             return m_array.append_element(e);
00328         }
00329 
00335         inline void push_back(T e)
00336         { m_array.push_back(e); }
00337 
00341         inline void pop_back()
00342         {
00343             m_array.pop_back();
00344         }
00345 
00351         inline T back()
00352         {
00353             return m_array.back();
00354         }
00355 
00362         inline int32_t find_element(T e)
00363         {
00364             return m_array.find_element(e);
00365         }
00366 
00373         inline bool delete_element(int32_t idx)
00374         {
00375             return m_array.delete_element(idx);
00376         }
00377 
00385         inline bool resize_array(int32_t ndim1, int32_t ndim2=1, int32_t ndim3=1)
00386         {
00387             dim1_size=ndim1;
00388             dim2_size=ndim2;
00389             dim3_size=ndim3;
00390             return m_array.resize_array(ndim1*ndim2*ndim3);
00391         }
00392 
00394         void set_const(const T& const_element)
00395         {
00396             m_array.set_const(const_element);
00397         }
00398 
00406         inline T* get_array() const
00407         {
00408             return m_array.get_array();
00409         }
00410 
00417         inline void set_array(T* p_array, int32_t p_num_elements,
00418                               int32_t array_size)
00419         {
00420             m_array.set_array(p_array, p_num_elements, array_size);
00421         }
00422 
00430         inline void set_array(T* p_array, int32_t dim1,
00431                         bool p_free_array, bool copy_array)
00432         {
00433             dim1_size=dim1;
00434             dim2_size=1;
00435             dim3_size=1;
00436             m_array.set_array(p_array, dim1, dim1, p_free_array, copy_array);
00437         }
00438 
00447         inline void set_array(T* p_array, int32_t dim1,
00448                         int32_t dim2, bool p_free_array, bool copy_array)
00449         {
00450             dim1_size=dim1;
00451             dim2_size=dim2;
00452             dim3_size=1;
00453 
00454             m_array.set_array(p_array, dim1*dim2, dim1*dim2, p_free_array, copy_array);
00455         }
00456 
00466         inline void set_array(T* p_array, int32_t dim1,
00467                         int32_t dim2, int32_t dim3, bool p_free_array, bool copy_array)
00468         {
00469             dim1_size=dim1;
00470             dim2_size=dim2;
00471             dim3_size=dim3;
00472             m_array.set_array(p_array, dim1*dim2*dim3, dim1*dim2*dim3, p_free_array, copy_array);
00473         }
00474 
00480         inline void set_array(const T* p_array, int32_t p_size)
00481         {
00482             m_array.set_array(p_array, p_size, p_size);
00483         }
00484 
00488         inline void clear_array(T value)
00489         {
00490             m_array.clear_array(value);
00491         }
00492 
00494         inline void reset_array()
00495         {
00496             m_array.reset((T) 0);
00497         }
00498 
00508         inline const T& operator[](int32_t index) const
00509         {
00510             return get_element(index);
00511         }
00512 
00520         inline T& operator[](int32_t index)
00521         {
00522             return element(index);
00523         }
00524 
00530         inline CDynamicArray<T>& operator=(CDynamicArray<T>& orig)
00531         {
00532             m_array=orig.m_array;
00533             dim1_size=orig.dim1_size;
00534             dim2_size=orig.dim2_size;
00535             dim3_size=orig.dim3_size;
00536 
00537             return *this;
00538         }
00539 
00541         inline void shuffle() { m_array.shuffle(); }
00542 
00544         inline void shuffle(CRandom * rand) { m_array.shuffle(rand); }
00545 
00550         inline void set_array_name(const char* p_name)
00551         {
00552             name=p_name;
00553         }
00554 
00559         inline const char* get_array_name() const { return name; }
00560 
00562         inline void display_array()
00563         {
00564             if (get_name())
00565                 SG_PRINT("DynamicArray '%s' of size: %dx%dx%d\n", get_name(), dim1_size, dim2_size, dim3_size)
00566             else
00567                 SG_PRINT("DynamicArray of size: %dx%dx%d\n",dim1_size, dim2_size, dim3_size)
00568 
00569             for (int32_t k=0; k<dim3_size; k++)
00570                 for (int32_t i=0; i<dim1_size; i++)
00571                 {
00572                     SG_PRINT("element(%d,:,%d) = [ ",i, k)
00573                     for (int32_t j=0; j<dim2_size; j++)
00574                         SG_PRINT("%1.1f,", (float32_t) element(i,j,k))
00575                     SG_PRINT(" ]\n")
00576                 }
00577         }
00578 
00580         inline void display_size()
00581         {
00582             SG_PRINT("DynamicArray of size: %dx%dx%d\n",dim1_size, dim2_size, dim3_size)
00583         }
00584 
00586         virtual const char* get_name() const
00587         {
00588             return "DynamicArray";
00589         }
00590 
00599         virtual void load_serializable_pre() throw (ShogunException)
00600         {
00601             CSGObject::load_serializable_pre();
00602 
00603             m_array.resize_array(m_array.get_num_elements(), true);
00604         }
00605 
00614         virtual void save_serializable_pre() throw (ShogunException)
00615         {
00616             CSGObject::save_serializable_pre();
00617             m_array.resize_array(m_array.get_num_elements(), true);
00618         }
00619 
00620 
00621     private:
00622 
00624         virtual void init()
00625         {
00626             set_generic<T>();
00627 
00628             m_parameters->add_vector(&m_array.array,
00629                     &m_array.current_num_elements, "array",
00630                     "Memory for dynamic array.");
00631             SG_ADD(&m_array.num_elements,
00632                               "num_elements",
00633                               "Number of Elements.", MS_NOT_AVAILABLE);
00634             SG_ADD(&m_array.resize_granularity,
00635                               "resize_granularity",
00636                               "shrink/grow step size.", MS_NOT_AVAILABLE);
00637             SG_ADD(&m_array.use_sg_mallocs,
00638                               "use_sg_malloc",
00639                               "whether SG_MALLOC or malloc should be used",
00640                               MS_NOT_AVAILABLE);
00641             SG_ADD(&m_array.free_array,
00642                               "free_array",
00643                               "whether array must be freed",
00644                               MS_NOT_AVAILABLE);
00645         }
00646 
00647     protected:
00648 
00650         DynArray<T> m_array;
00651 
00653         int32_t dim1_size;
00654 
00656         int32_t dim2_size;
00657 
00659         int32_t dim3_size;
00660 
00662         const char* name;
00663 };
00664 }
00665 #endif /* _DYNAMIC_ARRAY_H_  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation