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 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 {
00032 template<class T>class CDynamicObjectArray :public CSGObject
00033 {
00034     public:
00039         CDynamicObjectArray(int32_t p_resize_granularity=128)
00040         : CSGObject()
00041         {
00042             CSGObject*** casted_array=(CSGObject***)&m_array.array;
00043 
00044             m_parameters->add_vector(casted_array, &m_array.num_elements, "array",
00045                     "Memory for dynamic array.");
00046             m_parameters->add(&m_array.last_element_idx, "last_element_idx",
00047                     "Element with largest index.");
00048             m_parameters->add(&m_array.resize_granularity, "resize_granularity",
00049                     "shrink/grow step size.");
00050         }
00051 
00052         virtual ~CDynamicObjectArray() { unref_all(); }
00053 
00059         inline int32_t set_granularity(int32_t g)
00060         { return m_array.set_granularity(g); }
00061 
00066         inline int32_t get_num_elements() const
00067         {
00068             return m_array.get_num_elements();
00069         }
00070 
00078         inline T* get_element(int32_t index) const
00079         {
00080             T* element=m_array.get_element(index);
00081             CSGObject* casted=cast_to_sgobject(element);
00082             SG_REF(casted);
00083             return element;
00084         }
00085 
00093         inline T* get_element_safe(int32_t index) const
00094         {
00095             T* element=m_array.get_element_safe(index);
00096             CSGObject* casted=(CSGObject*)element;
00097             SG_REF(casted);
00098             return element;
00099         }
00100 
00107         inline bool set_element(T* element, int32_t index)
00108         {
00109             CSGObject* casted=cast_to_sgobject(element);
00110             CSGObject* old=(CSGObject*)m_array.get_element(index);
00111 
00112             bool success=m_array.set_element(element, index);
00113             if (success)
00114             {
00115                 SG_REF(casted);
00116                 SG_UNREF(old);
00117             }
00118 
00119             /* ref before unref to prevent deletion if new=old */
00120             return success;
00121         }
00122 
00129         inline bool insert_element(T* element, int32_t index)
00130         {
00131             CSGObject* casted=cast_to_sgobject(element);
00132             bool success=m_array.insert_element(element, index);
00133             if (success)
00134                 SG_REF(casted);
00135 
00136             return success;
00137         }
00138 
00144         inline bool append_element(T* element)
00145         {
00146             CSGObject* casted=cast_to_sgobject(element);
00147             bool success=m_array.append_element(element);
00148             if (success)
00149                 SG_REF(casted);
00150 
00151             return success;
00152         }
00153 
00159         inline void push_back(T* element)
00160         {
00161             CSGObject* casted=cast_to_sgobject(element);
00162             SG_REF(casted);
00163             m_array.push_back(element);
00164         }
00165 
00169         inline void pop_back()
00170         {
00171             CSGObject* element=(CSGObject*)m_array.back();
00172             SG_UNREF(element);
00173 
00174             m_array.pop_back();
00175         }
00176 
00182         inline T* back() const
00183         {
00184             T* element=m_array.back();
00185             CSGObject* casted=(CSGObject*)element;
00186             SG_REF(casted);
00187             return element;
00188         }
00189 
00196         inline int32_t find_element(T* element) const
00197         {
00198             return m_array.find_element(element);
00199         }
00200 
00207         inline bool delete_element(int32_t idx)
00208         {
00209             CSGObject* element=(CSGObject*)m_array.get_element(idx);
00210             SG_UNREF(element);
00211 
00212             return m_array.delete_element(idx);
00213         }
00214 
00216         inline void clear_array()
00217         {
00218             unref_all();
00219             m_array.clear_array();
00220         }
00221 
00227         inline CDynamicObjectArray<T>& operator=(CDynamicObjectArray<T>& orig)
00228         {
00229             /* SG_REF all new elements (implicitly) */
00230             for (index_t i=0; i<orig.get_num_elements(); ++i)
00231                 orig.get_element(i);
00232 
00233             /* unref after adding to avoid possible deletion */
00234             unref_all();
00235 
00236             /* copy pointer DynArray */
00237             m_array=orig.m_array;
00238             return *this;
00239         }
00240 
00242         inline T** get_array() const { return m_array.get_array(); }
00243 
00245         inline void shuffle() { m_array.shuffle(); }
00246 
00248         inline virtual const char* get_name() const
00249         { return "DynamicObjectArray"; }
00250 
00251     private:
00253         inline void unref_all()
00254         {
00255             /* SG_UNREF all my elements */
00256             for (index_t i=0; i<m_array.get_num_elements(); ++i)
00257             {
00258                 CSGObject* element=(CSGObject*)m_array.get_element(i);
00259                 SG_UNREF(element);
00260             }
00261         }
00262 
00266         inline CSGObject* cast_to_sgobject(T* element) const
00267         {
00268             if (!element)
00269                 return NULL;
00270 
00271             CSGObject* casted=dynamic_cast<CSGObject*>(element);
00272 
00273             if (!casted)
00274             {
00275                 SG_ERROR("Generic type of CDynamicObjectArray is not of type "
00276                         "CSGObject!\n");
00277             }
00278 
00279             return casted;
00280         }
00281 
00282     private:
00283         DynArray<T*> m_array;
00284 
00285 };
00286 }
00287 #endif /* _DYNAMIC_OBJECT_ARRAY_H_  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation