SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DynamicObjectArray.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Written (W) 2011 Heiko Strathmann
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #ifndef _DYNAMIC_OBJECT_ARRAY_H_
13 #define _DYNAMIC_OBJECT_ARRAY_H_
14 
15 #include <shogun/base/SGObject.h>
16 #include <shogun/base/DynArray.h>
17 #include <shogun/base/Parameter.h>
18 
19 namespace shogun
20 {
32 template<class T>class CDynamicObjectArray :public CSGObject
33 {
34  public:
39  CDynamicObjectArray(int32_t p_resize_granularity=128)
40  : CSGObject()
41  {
42  CSGObject*** casted_array=(CSGObject***)&m_array.array;
43 
44  m_parameters->add_vector(casted_array, &m_array.num_elements, "array",
45  "Memory for dynamic array.");
46  m_parameters->add(&m_array.last_element_idx, "last_element_idx",
47  "Element with largest index.");
48  m_parameters->add(&m_array.resize_granularity, "resize_granularity",
49  "shrink/grow step size.");
50  }
51 
52  virtual ~CDynamicObjectArray() { unref_all(); }
53 
59  inline int32_t set_granularity(int32_t g)
60  { return m_array.set_granularity(g); }
61 
66  inline int32_t get_num_elements() const
67  {
68  return m_array.get_num_elements();
69  }
70 
78  inline T* get_element(int32_t index) const
79  {
80  T* element=m_array.get_element(index);
81  CSGObject* casted=cast_to_sgobject(element);
82  SG_REF(casted);
83  return element;
84  }
85 
93  inline T* get_element_safe(int32_t index) const
94  {
95  T* element=m_array.get_element_safe(index);
96  CSGObject* casted=(CSGObject*)element;
97  SG_REF(casted);
98  return element;
99  }
100 
107  inline bool set_element(T* element, int32_t index)
108  {
109  CSGObject* casted=cast_to_sgobject(element);
110  CSGObject* old=(CSGObject*)m_array.get_element(index);
111 
112  bool success=m_array.set_element(element, index);
113  if (success)
114  {
115  SG_REF(casted);
116  SG_UNREF(old);
117  }
118 
119  /* ref before unref to prevent deletion if new=old */
120  return success;
121  }
122 
129  inline bool insert_element(T* element, int32_t index)
130  {
131  CSGObject* casted=cast_to_sgobject(element);
132  bool success=m_array.insert_element(element, index);
133  if (success)
134  SG_REF(casted);
135 
136  return success;
137  }
138 
144  inline bool append_element(T* element)
145  {
146  CSGObject* casted=cast_to_sgobject(element);
147  bool success=m_array.append_element(element);
148  if (success)
149  SG_REF(casted);
150 
151  return success;
152  }
153 
159  inline void push_back(T* element)
160  {
161  CSGObject* casted=cast_to_sgobject(element);
162  SG_REF(casted);
163  m_array.push_back(element);
164  }
165 
169  inline void pop_back()
170  {
171  CSGObject* element=(CSGObject*)m_array.back();
172  SG_UNREF(element);
173 
174  m_array.pop_back();
175  }
176 
182  inline T* back() const
183  {
184  T* element=m_array.back();
185  CSGObject* casted=(CSGObject*)element;
186  SG_REF(casted);
187  return element;
188  }
189 
196  inline int32_t find_element(T* element) const
197  {
198  return m_array.find_element(element);
199  }
200 
207  inline bool delete_element(int32_t idx)
208  {
209  CSGObject* element=(CSGObject*)m_array.get_element(idx);
210  SG_UNREF(element);
211 
212  return m_array.delete_element(idx);
213  }
214 
216  inline void clear_array()
217  {
218  unref_all();
219  m_array.clear_array();
220  }
221 
228  {
229  /* SG_REF all new elements (implicitly) */
230  for (index_t i=0; i<orig.get_num_elements(); ++i)
231  orig.get_element(i);
232 
233  /* unref after adding to avoid possible deletion */
234  unref_all();
235 
236  /* copy pointer DynArray */
237  m_array=orig.m_array;
238  return *this;
239  }
240 
242  inline T** get_array() const { return m_array.get_array(); }
243 
245  inline void shuffle() { m_array.shuffle(); }
246 
248  inline virtual const char* get_name() const
249  { return "DynamicObjectArray"; }
250 
251  private:
253  inline void unref_all()
254  {
255  /* SG_UNREF all my elements */
256  for (index_t i=0; i<m_array.get_num_elements(); ++i)
257  {
258  CSGObject* element=(CSGObject*)m_array.get_element(i);
259  SG_UNREF(element);
260  }
261  }
262 
266  inline CSGObject* cast_to_sgobject(T* element) const
267  {
268  if (!element)
269  return NULL;
270 
271  CSGObject* casted=dynamic_cast<CSGObject*>(element);
272 
273  if (!casted)
274  {
275  SG_ERROR("Generic type of CDynamicObjectArray is not of type "
276  "CSGObject!\n");
277  }
278 
279  return casted;
280  }
281 
282  private:
283  DynArray<T*> m_array;
284 
285 };
286 }
287 #endif /* _DYNAMIC_OBJECT_ARRAY_H_ */

SHOGUN Machine Learning Toolbox - Documentation