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             m_parameters->add_vector(&m_array.array, &m_array.num_elements,
00037                     "array", "Memory for dynamic array.");
00038             SG_ADD(&m_array.last_element_idx, "last_element_idx",
00039                     "Element with largest index.", MS_NOT_AVAILABLE);
00040             SG_ADD(&m_array.resize_granularity, "resize_granularity",
00041                     "shrink/grow step size.", MS_NOT_AVAILABLE);
00042 
00043             dim1_size=1;
00044             dim2_size=1;
00045             dim3_size=1;
00046         }
00047 
00054         CDynamicObjectArray(int32_t dim1, int32_t dim2=1, int32_t dim3=1)
00055         : CSGObject(), m_array(dim1*dim2*dim3), name("Array")
00056         {
00057             m_parameters->add_vector(&m_array.array, &m_array.num_elements,
00058                     "array", "Memory for dynamic array.");
00059             SG_ADD(&m_array.last_element_idx, "last_element_idx",
00060                     "Element with largest index.", MS_NOT_AVAILABLE);
00061             SG_ADD(&m_array.resize_granularity, "resize_granularity",
00062                     "shrink/grow step size.", MS_NOT_AVAILABLE);
00063 
00064             dim1_size=dim1;
00065             dim2_size=dim2;
00066             dim3_size=dim3;
00067         }
00068 
00076         CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, bool p_free_array=true, bool p_copy_array=false)
00077         : CSGObject(), m_array(p_array, p_dim1_size, p_free_array, p_copy_array), name("Array")
00078         {
00079             m_parameters->add_vector(&m_array.array, &m_array.num_elements,
00080                     "array", "Memory for dynamic array.");
00081             SG_ADD(&m_array.last_element_idx, "last_element_idx",
00082                     "Element with largest index.", MS_NOT_AVAILABLE);
00083             SG_ADD(&m_array.resize_granularity, "resize_granularity",
00084                     "shrink/grow step size.", MS_NOT_AVAILABLE);
00085 
00086             dim1_size=p_dim1_size;
00087             dim2_size=1;
00088             dim3_size=1;
00089         }
00090 
00099         CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
00100                         bool p_free_array=true, bool p_copy_array=false)
00101         : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size, p_free_array, p_copy_array), name("Array")
00102         {
00103             m_parameters->add_vector(&m_array.array, &m_array.num_elements,
00104                     "array", "Memory for dynamic array.");
00105             SG_ADD(&m_array.last_element_idx, "last_element_idx",
00106                     "Element with largest index.", MS_NOT_AVAILABLE);
00107             SG_ADD(&m_array.resize_granularity, "resize_granularity",
00108                     "shrink/grow step size.", MS_NOT_AVAILABLE);
00109 
00110             dim1_size=p_dim1_size;
00111             dim2_size=p_dim2_size;
00112             dim3_size=1;
00113         }
00114 
00124         CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
00125                         int32_t p_dim3_size, bool p_free_array=true, bool p_copy_array=false)
00126         : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size, p_free_array, p_copy_array), name("Array")
00127         {
00128             m_parameters->add_vector(&m_array.array, &m_array.num_elements,
00129                     "array", "Memory for dynamic array.");
00130             SG_ADD(&m_array.last_element_idx, "last_element_idx",
00131                     "Element with largest index.", MS_NOT_AVAILABLE);
00132             SG_ADD(&m_array.resize_granularity, "resize_granularity",
00133                     "shrink/grow step size.", MS_NOT_AVAILABLE);
00134 
00135             dim1_size=p_dim1_size;
00136             dim2_size=p_dim2_size;
00137             dim3_size=p_dim3_size;
00138         }
00139 
00140         virtual ~CDynamicObjectArray() { unref_all(); }
00141 
00147         inline int32_t set_granularity(int32_t g)
00148         { return m_array.set_granularity(g); }
00149 
00154         inline int32_t get_array_size()
00155         {
00156             return m_array.get_array_size();
00157         }
00158 
00164         inline void get_array_size(int32_t& dim1, int32_t& dim2)
00165         {
00166             dim1=dim1_size;
00167             dim2=dim2_size;
00168         }
00169 
00176         inline void get_array_size(int32_t& dim1, int32_t& dim2, int32_t& dim3)
00177         {
00178             dim1=dim1_size;
00179             dim2=dim2_size;
00180             dim3=dim3_size;
00181         }
00182 
00187         inline int32_t get_dim1() { return dim1_size; }
00188 
00193         inline int32_t get_dim2() { return dim2_size; }
00194 
00199         inline int32_t get_dim3() { return dim3_size; }
00200 
00205         inline int32_t get_num_elements() const
00206         {
00207             return m_array.get_num_elements();
00208         }
00209 
00217         inline CSGObject* get_element(int32_t index) const
00218         {
00219             CSGObject* elem=m_array.get_element(index);
00220             SG_REF(elem);
00221             return elem;
00222         }
00223 
00231         inline CSGObject* element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
00232         {
00233             return get_element(idx1+dim1_size*(idx2+dim2_size*idx3));
00234         }
00235 
00240         inline CSGObject* get_last_element() const
00241         {
00242             CSGObject* e=m_array.get_last_element();
00243             SG_REF(e);
00244             return e;
00245         }
00246 
00254         inline CSGObject* get_element_safe(int32_t index) const
00255         {
00256             CSGObject* e=m_array.get_element_safe(index);
00257             SG_REF(e);
00258             return e;
00259         }
00260 
00269         inline bool set_element(CSGObject* e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
00270         {
00271             CSGObject* old=(CSGObject*) m_array.get_element(idx1+dim1_size*(idx2+dim2_size*idx3));
00272 
00273             bool success=m_array.set_element(e, idx1+dim1_size*(idx2+dim2_size*idx3));
00274             if (success)
00275             {
00276                 SG_REF(e);
00277                 SG_UNREF(old);
00278             }
00279 
00280             /* ref before unref to prevent deletion if new=old */
00281             return success;
00282         }
00283 
00290         inline bool insert_element(CSGObject* e, int32_t index)
00291         {
00292             bool success=m_array.insert_element(e, index);
00293             if (success)
00294                 SG_REF(e);
00295 
00296             return success;
00297         }
00298 
00304         inline bool append_element(CSGObject* e)
00305         {
00306             bool success=m_array.append_element(e);
00307             if (success)
00308                 SG_REF(e);
00309 
00310             return success;
00311         }
00312 
00318         inline void push_back(CSGObject* e)
00319         {
00320             SG_REF(e);
00321             m_array.push_back(e);
00322         }
00323 
00327         inline void pop_back()
00328         {
00329             CSGObject* e=m_array.back();
00330             SG_UNREF(e);
00331 
00332             m_array.pop_back();
00333         }
00334 
00340         inline CSGObject* back() const
00341         {
00342             CSGObject* e=m_array.back();
00343             SG_REF(e);
00344             return e;
00345         }
00346 
00353         inline int32_t find_element(CSGObject* elem) const
00354         {
00355             return m_array.find_element(elem);
00356         }
00357 
00364         inline bool delete_element(int32_t idx)
00365         {
00366             CSGObject* e=m_array.get_element(idx);
00367             SG_UNREF(e);
00368 
00369             return m_array.delete_element(idx);
00370         }
00371 
00379         inline bool resize_array(int32_t ndim1, int32_t ndim2=1, int32_t ndim3=1)
00380         {
00381             dim1_size=ndim1;
00382             dim2_size=ndim2;
00383             dim3_size=ndim3;
00384             return m_array.resize_array(ndim1*ndim2*ndim3);
00385         }
00386 
00388         inline void clear_array()
00389         {
00390             unref_all();
00391             m_array.clear_array();
00392         }
00393 
00395         inline void reset_array()
00396         {
00397             unref_all();
00398             m_array.reset();
00399         }
00400 
00406         inline CDynamicObjectArray& operator=(CDynamicObjectArray& orig)
00407         {
00408             /* SG_REF all new elements (implicitly) */
00409             for (index_t i=0; i<orig.get_num_elements(); ++i)
00410                 orig.get_element(i);
00411 
00412             /* unref after adding to avoid possible deletion */
00413             unref_all();
00414 
00415             /* copy pointer DynArray */
00416             m_array=orig.m_array;
00417             return *this;
00418         }
00419 
00421         inline CSGObject** get_array() const { return m_array.get_array(); }
00422 
00424         inline void shuffle() { m_array.shuffle(); }
00425 
00430         inline void set_array_name(const char* p_name)
00431         {
00432             name=p_name;
00433         }
00434 
00439         inline const char* get_array_name() const { return name; }
00440 
00442         virtual const char* get_name() const
00443         { return "DynamicObjectArray"; }
00444 
00445     private:
00447         inline void unref_all()
00448         {
00449             /* SG_UNREF all my elements */
00450             for (index_t i=0; i<m_array.get_num_elements(); ++i)
00451             {
00452                 CSGObject* elem=m_array.get_element(i);
00453                 SG_UNREF(elem);
00454             }
00455         }
00456 
00457     private:
00459         DynArray<CSGObject*> m_array;
00460 
00462         int32_t dim1_size;
00463 
00465         int32_t dim2_size;
00466 
00468         int32_t dim3_size;
00469 
00471         const char* name;
00472 
00473 };
00474 }
00475 #endif /* _DYNAMIC_OBJECT_ARRAY_H_  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation