Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
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 DynArray<T*> m_array;
00035
00036 public:
00041 CDynamicObjectArray(int32_t p_resize_granularity=128)
00042 : CSGObject()
00043 {
00044 CSGObject*** casted_array=(CSGObject***)&m_array.array;
00045
00046 m_parameters->add_vector(casted_array, &m_array.num_elements, "array",
00047 "Memory for dynamic array.");
00048 m_parameters->add(&m_array.last_element_idx, "last_element_idx",
00049 "Element with largest index.");
00050 m_parameters->add(&m_array.resize_granularity, "resize_granularity",
00051 "shrink/grow step size.");
00052 }
00053
00054 virtual ~CDynamicObjectArray() { unref_all(); }
00055
00061 inline int32_t set_granularity(int32_t g)
00062 { return m_array.set_granularity(g); }
00063
00068 inline int32_t get_num_elements(void) const
00069 {
00070 return m_array.get_num_elements();
00071 }
00072
00080 inline T* get_element(int32_t index) const
00081 {
00082 T* element=m_array.get_element(index);
00083 CSGObject* casted=cast_to_sgobject(element);
00084 SG_REF(casted);
00085 return element;
00086 }
00087
00095 inline T* get_element_safe(int32_t index) const
00096 {
00097 T* element=m_array.get_element_safe(index);
00098 CSGObject* casted=(CSGObject*)element;
00099 SG_REF(casted);
00100 return element;
00101 }
00102
00109 inline bool set_element(T* element, int32_t index)
00110 {
00111 CSGObject* casted=cast_to_sgobject(element);
00112 CSGObject* old=(CSGObject*)m_array.get_element(index);
00113
00114 bool success=m_array.set_element(element, index);
00115 if (success)
00116 {
00117 SG_REF(casted);
00118 SG_UNREF(old);
00119 }
00120
00121
00122 return success;
00123 }
00124
00131 inline bool insert_element(T* element, int32_t index)
00132 {
00133 CSGObject* casted=cast_to_sgobject(element);
00134 bool success=m_array.insert_element(element, index);
00135 if (success)
00136 SG_REF(casted);
00137
00138 return success;
00139 }
00140
00146 inline bool append_element(T* element)
00147 {
00148 CSGObject* casted=cast_to_sgobject(element);
00149 bool success=m_array.append_element(element);
00150 if (success)
00151 SG_REF(casted);
00152
00153 return success;
00154 }
00155
00161 inline void push_back(T* element)
00162 {
00163 CSGObject* casted=cast_to_sgobject(element);
00164 SG_REF(casted);
00165 m_array.push_back(element);
00166 }
00167
00171 inline void pop_back(void)
00172 {
00173 CSGObject* element=(CSGObject*)m_array.back();
00174 SG_UNREF(element);
00175
00176 m_array.pop_back();
00177 }
00178
00184 inline T* back(void) const
00185 {
00186 T* element=m_array.back();
00187 CSGObject* casted=(CSGObject*)element;
00188 SG_REF(casted);
00189 return element;
00190 }
00191
00198 inline int32_t find_element(T* element) const
00199 {
00200 return m_array.find_element(element);
00201 }
00202
00209 inline bool delete_element(int32_t idx)
00210 {
00211 CSGObject* element=(CSGObject*)m_array.get_element(idx);
00212 SG_UNREF(element);
00213
00214 return m_array.delete_element(idx);
00215 }
00216
00218 inline void clear_array()
00219 {
00220 unref_all();
00221 m_array.clear_array();
00222 }
00223
00229 inline CDynamicObjectArray<T>& operator=(CDynamicObjectArray<T>& orig)
00230 {
00231
00232 for (index_t i=0; i<orig.get_num_elements(); ++i)
00233 orig.get_element(i);
00234
00235
00236 unref_all();
00237
00238
00239 m_array=orig.m_array;
00240 return *this;
00241 }
00242
00244 inline T** get_array() const { return m_array.get_array(); }
00245
00247 inline void shuffle() { m_array.shuffle(); }
00248
00250 inline virtual const char* get_name() const
00251 { return "DynamicObjectArray"; }
00252
00253 private:
00255 inline void unref_all()
00256 {
00257
00258 for (index_t i=0; i<m_array.get_num_elements(); ++i)
00259 {
00260 CSGObject* element=(CSGObject*)m_array.get_element(i);
00261 SG_UNREF(element);
00262 }
00263 }
00264
00268 inline CSGObject* cast_to_sgobject(T* element) const
00269 {
00270 if (!element)
00271 return NULL;
00272
00273 CSGObject* casted=dynamic_cast<CSGObject*>(element);
00274
00275 if (!casted)
00276 {
00277 SG_ERROR("Generic type of CDynamicObjectArray is not of type "
00278 "CSGObject!\n");
00279 }
00280
00281 return casted;
00282 }
00283 };
00284 }
00285 #endif