SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DynamicObjectArray.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  * Written (W) 2011-2012 Heiko Strathmann
00009  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #ifndef _DYNAMIC_OBJECT_ARRAY_H_
00013 #define _DYNAMIC_OBJECT_ARRAY_H_
00014 
00015 #include <shogun/base/SGObject.h>
00016 #include <shogun/base/DynArray.h>
00017 #include <shogun/base/Parameter.h>
00018 
00019 namespace shogun
00020 {
00029 class CDynamicObjectArray : public CSGObject
00030 {
00031     public:
00033         CDynamicObjectArray()
00034         : CSGObject(), m_array(), name("Array")
00035         {
00036             dim1_size=1;
00037             dim2_size=1;
00038             dim3_size=1;
00039 
00040             init();
00041         }
00042 
00049         CDynamicObjectArray(int32_t dim1, int32_t dim2=1, int32_t dim3=1)
00050         : CSGObject(), m_array(dim1*dim2*dim3), name("Array")
00051         {
00052             dim1_size=dim1;
00053             dim2_size=dim2;
00054             dim3_size=dim3;
00055 
00056             init();
00057         }
00058 
00066         CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, bool p_free_array=true, bool p_copy_array=false)
00067         : CSGObject(), m_array(p_array, p_dim1_size, p_free_array, p_copy_array), name("Array")
00068         {
00069             dim1_size=p_dim1_size;
00070             dim2_size=1;
00071             dim3_size=1;
00072 
00073             init();
00074         }
00075 
00084         CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
00085                         bool p_free_array=true, bool p_copy_array=false)
00086         : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size, p_free_array, p_copy_array), name("Array")
00087         {
00088             dim1_size=p_dim1_size;
00089             dim2_size=p_dim2_size;
00090             dim3_size=1;
00091 
00092             init();
00093         }
00094 
00104         CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
00105                         int32_t p_dim3_size, bool p_free_array=true, bool p_copy_array=false)
00106         : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size, p_free_array, p_copy_array), name("Array")
00107         {
00108             dim1_size=p_dim1_size;
00109             dim2_size=p_dim2_size;
00110             dim3_size=p_dim3_size;
00111 
00112             init();
00113         }
00114 
00115         virtual ~CDynamicObjectArray() { unref_all(); }
00116 
00122         inline int32_t set_granularity(int32_t g)
00123         { return m_array.set_granularity(g); }
00124 
00129         inline int32_t get_array_size()
00130         {
00131             return m_array.get_array_size();
00132         }
00133 
00139         inline void get_array_size(int32_t& dim1, int32_t& dim2)
00140         {
00141             dim1=dim1_size;
00142             dim2=dim2_size;
00143         }
00144 
00151         inline void get_array_size(int32_t& dim1, int32_t& dim2, int32_t& dim3)
00152         {
00153             dim1=dim1_size;
00154             dim2=dim2_size;
00155             dim3=dim3_size;
00156         }
00157 
00162         inline int32_t get_dim1() { return dim1_size; }
00163 
00168         inline int32_t get_dim2() { return dim2_size; }
00169 
00174         inline int32_t get_dim3() { return dim3_size; }
00175 
00180         inline int32_t get_num_elements() const
00181         {
00182             return m_array.get_num_elements();
00183         }
00184 
00192         inline CSGObject* get_element(int32_t index) const
00193         {
00194             CSGObject* elem=m_array.get_element(index);
00195             SG_REF(elem);
00196             return elem;
00197         }
00198 
00206         inline CSGObject* element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
00207         {
00208             return get_element(idx1+dim1_size*(idx2+dim2_size*idx3));
00209         }
00210 
00215         inline CSGObject* get_last_element() const
00216         {
00217             CSGObject* e=m_array.get_last_element();
00218             SG_REF(e);
00219             return e;
00220         }
00221 
00229         inline CSGObject* get_element_safe(int32_t index) const
00230         {
00231             CSGObject* e=m_array.get_element_safe(index);
00232             SG_REF(e);
00233             return e;
00234         }
00235 
00244         inline bool set_element(CSGObject* e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
00245         {
00246             int32_t idx = idx1+dim1_size*(idx2+dim2_size*idx3);
00247             CSGObject* old=NULL;
00248 
00249             if (idx<get_num_elements())
00250                 old = (CSGObject*) m_array.get_element(idx);
00251 
00252             bool success=m_array.set_element(e, idx);
00253             if (success)
00254             {
00255                 SG_REF(e);
00256                 SG_UNREF(old);
00257             }
00258 
00259             /* ref before unref to prevent deletion if new=old */
00260             return success;
00261         }
00262 
00269         inline bool insert_element(CSGObject* e, int32_t index)
00270         {
00271             bool success=m_array.insert_element(e, index);
00272             if (success)
00273                 SG_REF(e);
00274 
00275             return success;
00276         }
00277 
00283         inline bool append_element(CSGObject* e)
00284         {
00285             bool success=m_array.append_element(e);
00286             if (success)
00287                 SG_REF(e);
00288 
00289             return success;
00290         }
00291 
00297         inline void push_back(CSGObject* e)
00298         {
00299             SG_REF(e);
00300             m_array.push_back(e);
00301         }
00302 
00306         inline void pop_back()
00307         {
00308             CSGObject* e=m_array.back();
00309             SG_UNREF(e);
00310 
00311             m_array.pop_back();
00312         }
00313 
00319         inline CSGObject* back() const
00320         {
00321             CSGObject* e=m_array.back();
00322             SG_REF(e);
00323             return e;
00324         }
00325 
00332         inline int32_t find_element(CSGObject* elem) const
00333         {
00334             return m_array.find_element(elem);
00335         }
00336 
00343         inline bool delete_element(int32_t idx)
00344         {
00345             CSGObject* e=m_array.get_element(idx);
00346             SG_UNREF(e);
00347             m_array.set_element(NULL, idx);
00348 
00349             return m_array.delete_element(idx);
00350         }
00351 
00353         inline void clear_array()
00354         {
00355             unref_all();
00356             m_array.clear_array(NULL);
00357         }
00358 
00360         inline void reset_array()
00361         {
00362             unref_all();
00363             m_array.reset(NULL);
00364         }
00365 
00371         inline CDynamicObjectArray& operator=(CDynamicObjectArray& orig)
00372         {
00373             /* SG_REF all new elements (implicitly) */
00374             for (index_t i=0; i<orig.get_num_elements(); ++i)
00375                 orig.get_element(i);
00376 
00377             /* unref after adding to avoid possible deletion */
00378             unref_all();
00379 
00380             /* copy pointer DynArray */
00381             m_array=orig.m_array;
00382             return *this;
00383         }
00384 
00386         inline CSGObject** get_array() const { return m_array.get_array(); }
00387 
00389         inline void shuffle() { m_array.shuffle(); }
00390 
00392         inline void shuffle(CRandom * rand) { m_array.shuffle(rand); }
00393 
00398         inline void set_array_name(const char* p_name)
00399         {
00400             name=p_name;
00401         }
00402 
00407         inline const char* get_array_name() const { return name; }
00408 
00410         virtual const char* get_name() const
00411         { return "DynamicObjectArray"; }
00412 
00421         virtual void load_serializable_pre() throw (ShogunException)
00422         {
00423             CSGObject::load_serializable_pre();
00424 
00425             m_array.resize_array(m_array.get_num_elements(), true);
00426         }
00427 
00436         virtual void save_serializable_pre() throw (ShogunException)
00437         {
00438             CSGObject::save_serializable_pre();
00439 
00440             m_array.resize_array(m_array.get_num_elements(), true);
00441         }
00442 
00443     private:
00444 
00446         virtual void init()
00447         {
00448             m_parameters->add_vector(&m_array.array, &m_array.current_num_elements, "array",
00449                                      "Memory for dynamic array.");
00450             SG_ADD(&m_array.num_elements,
00451                               "num_elements",
00452                               "Number of Elements.", MS_NOT_AVAILABLE);
00453             SG_ADD(&m_array.resize_granularity,
00454                               "resize_granularity",
00455                               "shrink/grow step size.", MS_NOT_AVAILABLE);
00456             SG_ADD(&m_array.use_sg_mallocs,
00457                               "use_sg_malloc",
00458                               "whether SG_MALLOC or malloc should be used",
00459                               MS_NOT_AVAILABLE);
00460             SG_ADD(&m_array.free_array,
00461                               "free_array",
00462                               "whether array must be freed",
00463                               MS_NOT_AVAILABLE);
00464         }
00465 
00467         inline void unref_all()
00468         {
00469             /* SG_UNREF all my elements */
00470             for (index_t i=0; i<m_array.get_num_elements(); ++i)
00471             {
00472                 SG_UNREF(*m_array.get_element_ptr(i));
00473             }
00474         }
00475 
00476     private:
00478         DynArray<CSGObject*> m_array;
00479 
00481         int32_t dim1_size;
00482 
00484         int32_t dim2_size;
00485 
00487         int32_t dim3_size;
00488 
00490         const char* name;
00491 
00492 };
00493 }
00494 #endif /* _DYNAMIC_OBJECT_ARRAY_H_  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation