SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-2012 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 {
30 {
31  public:
34  : CSGObject(), m_array(), name("Array")
35  {
36  dim1_size=1;
37  dim2_size=1;
38  dim3_size=1;
39 
40  init();
41  }
42 
49  CDynamicObjectArray(int32_t dim1, int32_t dim2=1, int32_t dim3=1)
50  : CSGObject(), m_array(dim1*dim2*dim3), name("Array")
51  {
52  dim1_size=dim1;
53  dim2_size=dim2;
54  dim3_size=dim3;
55 
56  init();
57  }
58 
66  CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, bool p_free_array=true, bool p_copy_array=false)
67  : CSGObject(), m_array(p_array, p_dim1_size, p_free_array, p_copy_array), name("Array")
68  {
69  dim1_size=p_dim1_size;
70  dim2_size=1;
71  dim3_size=1;
72 
73  init();
74  }
75 
84  CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
85  bool p_free_array=true, bool p_copy_array=false)
86  : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size, p_free_array, p_copy_array), name("Array")
87  {
88  dim1_size=p_dim1_size;
89  dim2_size=p_dim2_size;
90  dim3_size=1;
91 
92  init();
93  }
94 
104  CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
105  int32_t p_dim3_size, bool p_free_array=true, bool p_copy_array=false)
106  : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size, p_free_array, p_copy_array), name("Array")
107  {
108  dim1_size=p_dim1_size;
109  dim2_size=p_dim2_size;
110  dim3_size=p_dim3_size;
111 
112  init();
113  }
114 
115  virtual ~CDynamicObjectArray() { unref_all(); }
116 
122  inline int32_t set_granularity(int32_t g)
123  { return m_array.set_granularity(g); }
124 
129  inline int32_t get_array_size()
130  {
131  return m_array.get_array_size();
132  }
133 
139  inline void get_array_size(int32_t& dim1, int32_t& dim2)
140  {
141  dim1=dim1_size;
142  dim2=dim2_size;
143  }
144 
151  inline void get_array_size(int32_t& dim1, int32_t& dim2, int32_t& dim3)
152  {
153  dim1=dim1_size;
154  dim2=dim2_size;
155  dim3=dim3_size;
156  }
157 
162  inline int32_t get_dim1() { return dim1_size; }
163 
168  inline int32_t get_dim2() { return dim2_size; }
169 
174  inline int32_t get_dim3() { return dim3_size; }
175 
180  inline int32_t get_num_elements() const
181  {
182  return m_array.get_num_elements();
183  }
184 
192  inline CSGObject* get_element(int32_t index) const
193  {
194  CSGObject* elem=m_array.get_element(index);
195  SG_REF(elem);
196  return elem;
197  }
198 
206  inline CSGObject* element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
207  {
208  return get_element(idx1+dim1_size*(idx2+dim2_size*idx3));
209  }
210 
215  inline CSGObject* get_last_element() const
216  {
217  CSGObject* e=m_array.get_last_element();
218  SG_REF(e);
219  return e;
220  }
221 
229  inline CSGObject* get_element_safe(int32_t index) const
230  {
231  CSGObject* e=m_array.get_element_safe(index);
232  SG_REF(e);
233  return e;
234  }
235 
244  inline bool set_element(CSGObject* e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
245  {
246  CSGObject* old=(CSGObject*) m_array.get_element(idx1+dim1_size*(idx2+dim2_size*idx3));
247 
248  bool success=m_array.set_element(e, idx1+dim1_size*(idx2+dim2_size*idx3));
249  if (success)
250  {
251  SG_REF(e);
252  SG_UNREF(old);
253  }
254 
255  /* ref before unref to prevent deletion if new=old */
256  return success;
257  }
258 
265  inline bool insert_element(CSGObject* e, int32_t index)
266  {
267  bool success=m_array.insert_element(e, index);
268  if (success)
269  SG_REF(e);
270 
271  return success;
272  }
273 
279  inline bool append_element(CSGObject* e)
280  {
281  bool success=m_array.append_element(e);
282  if (success)
283  SG_REF(e);
284 
285  return success;
286  }
287 
293  inline void push_back(CSGObject* e)
294  {
295  SG_REF(e);
296  m_array.push_back(e);
297  }
298 
302  inline void pop_back()
303  {
304  CSGObject* e=m_array.back();
305  SG_UNREF(e);
306 
307  m_array.pop_back();
308  }
309 
315  inline CSGObject* back() const
316  {
317  CSGObject* e=m_array.back();
318  SG_REF(e);
319  return e;
320  }
321 
328  inline int32_t find_element(CSGObject* elem) const
329  {
330  return m_array.find_element(elem);
331  }
332 
339  inline bool delete_element(int32_t idx)
340  {
341  CSGObject* e=m_array.get_element(idx);
342  SG_UNREF(e);
343  m_array.set_element(NULL, idx);
344 
345  return m_array.delete_element(idx);
346  }
347 
349  inline void clear_array()
350  {
351  unref_all();
352  m_array.clear_array(NULL);
353  }
354 
356  inline void reset_array()
357  {
358  unref_all();
359  m_array.reset(NULL);
360  }
361 
368  {
369  /* SG_REF all new elements (implicitly) */
370  for (index_t i=0; i<orig.get_num_elements(); ++i)
371  orig.get_element(i);
372 
373  /* unref after adding to avoid possible deletion */
374  unref_all();
375 
376  /* copy pointer DynArray */
377  m_array=orig.m_array;
378  return *this;
379  }
380 
382  inline CSGObject** get_array() const { return m_array.get_array(); }
383 
385  inline void shuffle() { m_array.shuffle(); }
386 
388  inline void shuffle(CRandom * rand) { m_array.shuffle(rand); }
389 
394  inline void set_array_name(const char* p_name)
395  {
396  name=p_name;
397  }
398 
403  inline const char* get_array_name() const { return name; }
404 
406  virtual const char* get_name() const
407  { return "DynamicObjectArray"; }
408 
417  virtual void load_serializable_pre() throw (ShogunException)
418  {
420 
421  m_array.resize_array(m_array.get_num_elements(), true);
422  }
423 
432  virtual void save_serializable_pre() throw (ShogunException)
433  {
435 
436  m_array.resize_array(m_array.get_num_elements(), true);
437  }
438 
439  private:
440 
442  virtual void init()
443  {
444  m_parameters->add_vector(&m_array.array, &m_array.current_num_elements, "array",
445  "Memory for dynamic array.");
446  SG_ADD(&m_array.num_elements,
447  "num_elements",
448  "Number of Elements.", MS_NOT_AVAILABLE);
449  SG_ADD(&m_array.resize_granularity,
450  "resize_granularity",
451  "shrink/grow step size.", MS_NOT_AVAILABLE);
452  SG_ADD(&m_array.use_sg_mallocs,
453  "use_sg_malloc",
454  "whether SG_MALLOC or malloc should be used",
456  SG_ADD(&m_array.free_array,
457  "free_array",
458  "whether array must be freed",
460  }
461 
463  inline void unref_all()
464  {
465  /* SG_UNREF all my elements */
466  for (index_t i=0; i<m_array.get_num_elements(); ++i)
467  {
468  SG_UNREF(*m_array.get_element_ptr(i));
469  }
470  }
471 
472  private:
474  DynArray<CSGObject*> m_array;
475 
477  int32_t dim1_size;
478 
480  int32_t dim2_size;
481 
483  int32_t dim3_size;
484 
486  const char* name;
487 
488 };
489 }
490 #endif /* _DYNAMIC_OBJECT_ARRAY_H_ */

SHOGUN Machine Learning Toolbox - Documentation