SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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-2016 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/lib/config.h>
16 
17 #include <shogun/base/SGObject.h>
18 #include <shogun/base/DynArray.h>
19 #include <shogun/base/Parameter.h>
20 
21 namespace shogun
22 {
32 {
33  public:
36  : CSGObject(), m_array(), name("Array")
37  {
38  dim1_size=1;
39  dim2_size=1;
40  dim3_size=1;
41 
42  init();
43  }
44 
51  CDynamicObjectArray(int32_t dim1, int32_t dim2=1, int32_t dim3=1)
52  : CSGObject(), m_array(dim1*dim2*dim3), name("Array")
53  {
54  dim1_size=dim1;
55  dim2_size=dim2;
56  dim3_size=dim3;
57 
58  init();
59  }
60 
68  CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, bool p_free_array=true, bool p_copy_array=false)
69  : CSGObject(), m_array(p_array, p_dim1_size, p_free_array, p_copy_array), name("Array")
70  {
71  dim1_size=p_dim1_size;
72  dim2_size=1;
73  dim3_size=1;
74 
75  init();
76  }
77 
86  CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
87  bool p_free_array=true, bool p_copy_array=false)
88  : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size, p_free_array, p_copy_array), name("Array")
89  {
90  dim1_size=p_dim1_size;
91  dim2_size=p_dim2_size;
92  dim3_size=1;
93 
94  init();
95  }
96 
106  CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
107  int32_t p_dim3_size, bool p_free_array=true, bool p_copy_array=false)
108  : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size, p_free_array, p_copy_array), name("Array")
109  {
110  dim1_size=p_dim1_size;
111  dim2_size=p_dim2_size;
112  dim3_size=p_dim3_size;
113 
114  init();
115  }
116 
117  virtual ~CDynamicObjectArray() { unref_all(); }
118 
124  inline int32_t set_granularity(int32_t g)
125  { return m_array.set_granularity(g); }
126 
131  inline int32_t get_array_size()
132  {
133  return m_array.get_array_size();
134  }
135 
141  inline void get_array_size(int32_t& dim1, int32_t& dim2)
142  {
143  dim1=dim1_size;
144  dim2=dim2_size;
145  }
146 
153  inline void get_array_size(int32_t& dim1, int32_t& dim2, int32_t& dim3)
154  {
155  dim1=dim1_size;
156  dim2=dim2_size;
157  dim3=dim3_size;
158  }
159 
164  inline int32_t get_dim1() { return dim1_size; }
165 
170  inline int32_t get_dim2() { return dim2_size; }
171 
176  inline int32_t get_dim3() { return dim3_size; }
177 
182  inline int32_t get_num_elements() const
183  {
184  return m_array.get_num_elements();
185  }
186 
194  inline CSGObject* get_element(int32_t index) const
195  {
196  CSGObject* elem=m_array.get_element(index);
197  SG_REF(elem);
198  return elem;
199  }
200 
208  inline CSGObject* element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
209  {
210  return get_element(idx1+dim1_size*(idx2+dim2_size*idx3));
211  }
212 
217  inline CSGObject* get_last_element() const
218  {
219  CSGObject* e=m_array.get_last_element();
220  SG_REF(e);
221  return e;
222  }
223 
231  inline CSGObject* get_element_safe(int32_t index) const
232  {
233  CSGObject* e=m_array.get_element_safe(index);
234  SG_REF(e);
235  return e;
236  }
237 
246  inline bool set_element(CSGObject* e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
247  {
248  int32_t idx = idx1+dim1_size*(idx2+dim2_size*idx3);
249  CSGObject* old=NULL;
250 
251  if (idx<get_num_elements())
252  old = (CSGObject*) m_array.get_element(idx);
253 
254  bool success=m_array.set_element(e, idx);
255  if (success)
256  {
257  SG_REF(e);
258  SG_UNREF(old);
259  }
260 
261  /* ref before unref to prevent deletion if new=old */
262  return success;
263  }
264 
271  inline bool insert_element(CSGObject* e, int32_t index)
272  {
273  bool success=m_array.insert_element(e, index);
274  if (success)
275  SG_REF(e);
276 
277  return success;
278  }
279 
285  inline bool append_element(CSGObject* e)
286  {
287  bool success=m_array.append_element(e);
288  if (success)
289  SG_REF(e);
290 
291  return success;
292  }
293 
299  inline void push_back(CSGObject* e)
300  {
301  SG_REF(e);
302  m_array.push_back(e);
303  }
304 
308  inline void pop_back()
309  {
310  CSGObject* e=m_array.back();
311  SG_UNREF(e);
312 
313  m_array.pop_back();
314  }
315 
321  inline CSGObject* back() const
322  {
323  CSGObject* e=m_array.back();
324  SG_REF(e);
325  return e;
326  }
327 
334  inline int32_t find_element(CSGObject* elem) const
335  {
336  return m_array.find_element(elem);
337  }
338 
345  inline bool delete_element(int32_t idx)
346  {
347  CSGObject* e=m_array.get_element(idx);
348  SG_UNREF(e);
349  m_array.set_element(NULL, idx);
350 
351  return m_array.delete_element(idx);
352  }
353 
355  inline void clear_array()
356  {
357  unref_all();
358  m_array.clear_array(NULL);
359  }
360 
362  inline void reset_array()
363  {
364  unref_all();
365  m_array.reset(NULL);
366  }
367 
374  {
375  /* SG_REF all new elements (implicitly) */
376  for (index_t i=0; i<orig.get_num_elements(); ++i)
377  orig.get_element(i);
378 
379  /* unref after adding to avoid possible deletion */
380  unref_all();
381 
382  /* copy pointer DynArray */
383  m_array=orig.m_array;
384  return *this;
385  }
386 
388  inline CSGObject** get_array() const { return m_array.get_array(); }
389 
391  inline void shuffle() { m_array.shuffle(); }
392 
394  inline void shuffle(CRandom * rand) { m_array.shuffle(rand); }
395 
400  inline void set_array_name(const char* p_name)
401  {
402  name=p_name;
403  }
404 
409  inline const char* get_array_name() const { return name; }
410 
412  virtual const char* get_name() const
413  { return "DynamicObjectArray"; }
414 
423  virtual void load_serializable_pre() throw (ShogunException)
424  {
426 
427  m_array.resize_array(m_array.get_num_elements(), true);
428  }
429 
438  virtual void save_serializable_pre() throw (ShogunException)
439  {
441 
442  m_array.resize_array(m_array.get_num_elements(), true);
443  }
444 
445  private:
446 
448  virtual void init()
449  {
450  m_parameters->add_vector(&m_array.array, &m_array.current_num_elements, "array",
451  "Memory for dynamic array.");
452  SG_ADD(&m_array.num_elements,
453  "num_elements",
454  "Number of Elements.", MS_NOT_AVAILABLE);
455  SG_ADD(&m_array.resize_granularity,
456  "resize_granularity",
457  "shrink/grow step size.", MS_NOT_AVAILABLE);
458  SG_ADD(&m_array.use_sg_mallocs,
459  "use_sg_malloc",
460  "whether SG_MALLOC or malloc should be used",
462  SG_ADD(&m_array.free_array,
463  "free_array",
464  "whether array must be freed",
466  }
467 
469  inline void unref_all()
470  {
471  /* SG_UNREF all my elements */
472  for (index_t i=0; i<m_array.get_num_elements(); ++i)
473  {
474  SG_UNREF(*m_array.get_element_ptr(i));
475  }
476  }
477 
478  private:
480  DynArray<CSGObject*> m_array;
481 
483  int32_t dim1_size;
484 
486  int32_t dim2_size;
487 
489  int32_t dim3_size;
490 
492  const char* name;
493 
494 };
495 }
496 #endif /* _DYNAMIC_OBJECT_ARRAY_H_ */
const char * get_array_name() const
int32_t index_t
Definition: common.h:62
CDynamicObjectArray & operator=(CDynamicObjectArray &orig)
CDynamicObjectArray(CSGObject **p_array, int32_t p_dim1_size, int32_t p_dim2_size, int32_t p_dim3_size, bool p_free_array=true, bool p_copy_array=false)
Class ShogunException defines an exception which is thrown whenever an error inside of shogun occurs...
bool insert_element(CSGObject *e, int32_t index)
CDynamicObjectArray(int32_t dim1, int32_t dim2=1, int32_t dim3=1)
virtual void save_serializable_pre()
Definition: SGObject.cpp:464
Parameter * m_parameters
Definition: SGObject.h:546
int32_t find_element(CSGObject *elem) const
#define SG_REF(x)
Definition: SGObject.h:54
bool set_element(CSGObject *e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
CSGObject ** get_array() const
virtual const char * get_name() const
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
CSGObject * element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
CDynamicObjectArray(CSGObject **p_array, int32_t p_dim1_size, bool p_free_array=true, bool p_copy_array=false)
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
void get_array_size(int32_t &dim1, int32_t &dim2)
: Pseudo random number geneartor
Definition: Random.h:34
virtual void load_serializable_pre()
Definition: SGObject.cpp:454
#define SG_UNREF(x)
Definition: SGObject.h:55
void get_array_size(int32_t &dim1, int32_t &dim2, int32_t &dim3)
void add_vector(bool **param, index_t *length, const char *name, const char *description="")
Definition: Parameter.cpp:334
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CSGObject * get_element_safe(int32_t index) const
CSGObject * get_element(int32_t index) const
#define SG_ADD(...)
Definition: SGObject.h:84
CSGObject * get_last_element() const
void set_array_name(const char *p_name)
int32_t set_granularity(int32_t g)
CDynamicObjectArray(CSGObject **p_array, int32_t p_dim1_size, int32_t p_dim2_size, bool p_free_array=true, bool p_copy_array=false)
bool append_element(CSGObject *e)

SHOGUN Machine Learning Toolbox - Documentation