SHOGUN
v3.2.0
|
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_REFOBJECT_ARRAY_H_ 00013 #define _DYNAMIC_REFOBJECT_ARRAY_H_ 00014 00015 #include <shogun/base/RefObject.h> 00016 #include <shogun/base/DynArray.h> 00017 #include <shogun/base/Parameter.h> 00018 00019 namespace shogun 00020 { 00031 class SGDynamicRefObjectArray : public SGRefObject 00032 { 00033 public: 00035 SGDynamicRefObjectArray() 00036 : CSGObject(), m_array(), name("Array") 00037 { 00038 dim1_size=1; 00039 dim2_size=1; 00040 dim3_size=1; 00041 } 00042 00049 SGDynamicRefObjectArray(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 00064 SGDynamicRefObjectArray(CRefObject** p_array, int32_t p_dim1_size, bool p_free_array=true, bool p_copy_array=false) 00065 : CSGObject(), m_array(p_array, p_dim1_size, p_free_array, p_copy_array), name("Array") 00066 { 00067 dim1_size=p_dim1_size; 00068 dim2_size=1; 00069 dim3_size=1; 00070 } 00071 00080 SGDynamicRefObjectArray(CRefObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size, 00081 bool p_free_array=true, bool p_copy_array=false) 00082 : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size, p_free_array, p_copy_array), name("Array") 00083 { 00084 dim1_size=p_dim1_size; 00085 dim2_size=p_dim2_size; 00086 dim3_size=1; 00087 } 00088 00098 SGDynamicRefObjectArray(CRefObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size, 00099 int32_t p_dim3_size, bool p_free_array=true, bool p_copy_array=false) 00100 : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size, p_free_array, p_copy_array), name("Array") 00101 { 00102 dim1_size=p_dim1_size; 00103 dim2_size=p_dim2_size; 00104 dim3_size=p_dim3_size; 00105 } 00106 00107 virtual ~SGDynamicRefObjectArray() { unref_all(); } 00108 00114 inline int32_t set_granularity(int32_t g) 00115 { return m_array.set_granularity(g); } 00116 00121 inline int32_t get_array_size() 00122 { 00123 return m_array.get_array_size(); 00124 } 00125 00131 inline void get_array_size(int32_t& dim1, int32_t& dim2) 00132 { 00133 dim1=dim1_size; 00134 dim2=dim2_size; 00135 } 00136 00143 inline void get_array_size(int32_t& dim1, int32_t& dim2, int32_t& dim3) 00144 { 00145 dim1=dim1_size; 00146 dim2=dim2_size; 00147 dim3=dim3_size; 00148 } 00149 00154 inline int32_t get_dim1() { return dim1_size; } 00155 00160 inline int32_t get_dim2() { return dim2_size; } 00161 00166 inline int32_t get_dim3() { return dim3_size; } 00167 00172 inline int32_t get_num_elements() const 00173 { 00174 return m_array.get_num_elements(); 00175 } 00176 00184 inline CRefObject* get_element(int32_t index) const 00185 { 00186 CRefObject* elem=m_array.get_element(index); 00187 SG_REF(elem); 00188 return elem; 00189 } 00190 00198 inline CRefObject* element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) 00199 { 00200 return get_element(idx1+dim1_size*(idx2+dim2_size*idx3)); 00201 } 00202 00207 inline CRefObject* get_last_element() const 00208 { 00209 CRefObject* e=m_array.get_last_element(); 00210 SG_REF(e); 00211 return e; 00212 } 00213 00221 inline CRefObject* get_element_safe(int32_t index) const 00222 { 00223 CRefObject* e=m_array.get_element_safe(index); 00224 SG_REF(e); 00225 return e; 00226 } 00227 00236 inline bool set_element(CRefObject* e, int32_t idx1, int32_t idx2=0, int32_t idx3=0) 00237 { 00238 int32_t idx = idx1+dim1_size*(idx2+dim2_size*idx3); 00239 CRefObject* old=NULL; 00240 00241 if (idx<get_num_elements()) 00242 old = (CRefObject*) m_array.get_element(idx); 00243 00244 bool success=m_array.set_element(e, idx); 00245 if (success) 00246 { 00247 SG_REF(e); 00248 SG_UNREF(old); 00249 } 00250 00251 /* ref before unref to prevent deletion if new=old */ 00252 return success; 00253 } 00254 00261 inline bool insert_element(CRefObject* e, int32_t index) 00262 { 00263 bool success=m_array.insert_element(e, index); 00264 if (success) 00265 SG_REF(e); 00266 00267 return success; 00268 } 00269 00275 inline bool append_element(CRefObject* e) 00276 { 00277 bool success=m_array.append_element(e); 00278 if (success) 00279 SG_REF(e); 00280 00281 return success; 00282 } 00283 00289 inline void push_back(CRefObject* e) 00290 { 00291 SG_REF(e); 00292 m_array.push_back(e); 00293 } 00294 00298 inline void pop_back() 00299 { 00300 CRefObject* e=m_array.back(); 00301 SG_UNREF(e); 00302 00303 m_array.pop_back(); 00304 } 00305 00311 inline CRefObject* back() const 00312 { 00313 CRefObject* e=m_array.back(); 00314 SG_REF(e); 00315 return e; 00316 } 00317 00324 inline int32_t find_element(CRefObject* elem) const 00325 { 00326 return m_array.find_element(elem); 00327 } 00328 00335 inline bool delete_element(int32_t idx) 00336 { 00337 CRefObject* e=m_array.get_element(idx); 00338 SG_UNREF(e); 00339 m_array.set_element(NULL, idx); 00340 00341 return m_array.delete_element(idx); 00342 } 00343 00345 inline void clear_array() 00346 { 00347 unref_all(); 00348 m_array.clear_array(NULL); 00349 } 00350 00352 inline void reset_array() 00353 { 00354 unref_all(); 00355 m_array.reset(NULL); 00356 } 00357 00363 inline SGDynamicRefObjectArray& operator=(SGDynamicRefObjectArray& orig) 00364 { 00365 /* SG_REF all new elements (implicitly) */ 00366 for (index_t i=0; i<orig.get_num_elements(); ++i) 00367 orig.get_element(i); 00368 00369 /* unref after adding to avoid possible deletion */ 00370 unref_all(); 00371 00372 /* copy pointer DynArray */ 00373 m_array=orig.m_array; 00374 return *this; 00375 } 00376 00378 inline CRefObject** get_array() const { return m_array.get_array(); } 00379 00381 inline void shuffle() { m_array.shuffle(); } 00382 00384 inline void shuffle(CRandom * rand) { m_array.shuffle(rand); } 00385 00390 inline void set_array_name(const char* p_name) 00391 { 00392 name=p_name; 00393 } 00394 00399 inline const char* get_array_name() const { return name; } 00400 00402 virtual const char* get_name() const 00403 { return "DynamicRefObjectArray"; } 00404 00405 private: 00407 inline void unref_all() 00408 { 00409 /* SG_UNREF all my elements */ 00410 for (index_t i=0; i<m_array.get_num_elements(); ++i) 00411 { 00412 SG_UNREF(*m_array.get_element_ptr(i)); 00413 } 00414 } 00415 00416 private: 00418 DynArray<CRefObject*> m_array; 00419 00421 int32_t dim1_size; 00422 00424 int32_t dim2_size; 00425 00427 int32_t dim3_size; 00428 00430 const char* name; 00431 00432 }; 00433 } 00434 #endif /* _DYNAMIC_REFOBJECT_ARRAY_H_ */