SHOGUN  3.2.1
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
DynamicObjectArray.h
浏览该文件的文档.
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  int32_t idx = idx1+dim1_size*(idx2+dim2_size*idx3);
247  CSGObject* old=NULL;
248 
249  if (idx<get_num_elements())
250  old = (CSGObject*) m_array.get_element(idx);
251 
252  bool success=m_array.set_element(e, idx);
253  if (success)
254  {
255  SG_REF(e);
256  SG_UNREF(old);
257  }
258 
259  /* ref before unref to prevent deletion if new=old */
260  return success;
261  }
262 
269  inline bool insert_element(CSGObject* e, int32_t index)
270  {
271  bool success=m_array.insert_element(e, index);
272  if (success)
273  SG_REF(e);
274 
275  return success;
276  }
277 
283  inline bool append_element(CSGObject* e)
284  {
285  bool success=m_array.append_element(e);
286  if (success)
287  SG_REF(e);
288 
289  return success;
290  }
291 
297  inline void push_back(CSGObject* e)
298  {
299  SG_REF(e);
300  m_array.push_back(e);
301  }
302 
306  inline void pop_back()
307  {
308  CSGObject* e=m_array.back();
309  SG_UNREF(e);
310 
311  m_array.pop_back();
312  }
313 
319  inline CSGObject* back() const
320  {
321  CSGObject* e=m_array.back();
322  SG_REF(e);
323  return e;
324  }
325 
332  inline int32_t find_element(CSGObject* elem) const
333  {
334  return m_array.find_element(elem);
335  }
336 
343  inline bool delete_element(int32_t idx)
344  {
345  CSGObject* e=m_array.get_element(idx);
346  SG_UNREF(e);
347  m_array.set_element(NULL, idx);
348 
349  return m_array.delete_element(idx);
350  }
351 
353  inline void clear_array()
354  {
355  unref_all();
356  m_array.clear_array(NULL);
357  }
358 
360  inline void reset_array()
361  {
362  unref_all();
363  m_array.reset(NULL);
364  }
365 
372  {
373  /* SG_REF all new elements (implicitly) */
374  for (index_t i=0; i<orig.get_num_elements(); ++i)
375  orig.get_element(i);
376 
377  /* unref after adding to avoid possible deletion */
378  unref_all();
379 
380  /* copy pointer DynArray */
381  m_array=orig.m_array;
382  return *this;
383  }
384 
386  inline CSGObject** get_array() const { return m_array.get_array(); }
387 
389  inline void shuffle() { m_array.shuffle(); }
390 
392  inline void shuffle(CRandom * rand) { m_array.shuffle(rand); }
393 
398  inline void set_array_name(const char* p_name)
399  {
400  name=p_name;
401  }
402 
407  inline const char* get_array_name() const { return name; }
408 
410  virtual const char* get_name() const
411  { return "DynamicObjectArray"; }
412 
421  virtual void load_serializable_pre() throw (ShogunException)
422  {
424 
425  m_array.resize_array(m_array.get_num_elements(), true);
426  }
427 
436  virtual void save_serializable_pre() throw (ShogunException)
437  {
439 
440  m_array.resize_array(m_array.get_num_elements(), true);
441  }
442 
443  private:
444 
446  virtual void init()
447  {
448  m_parameters->add_vector(&m_array.array, &m_array.current_num_elements, "array",
449  "Memory for dynamic array.");
450  SG_ADD(&m_array.num_elements,
451  "num_elements",
452  "Number of Elements.", MS_NOT_AVAILABLE);
453  SG_ADD(&m_array.resize_granularity,
454  "resize_granularity",
455  "shrink/grow step size.", MS_NOT_AVAILABLE);
456  SG_ADD(&m_array.use_sg_mallocs,
457  "use_sg_malloc",
458  "whether SG_MALLOC or malloc should be used",
460  SG_ADD(&m_array.free_array,
461  "free_array",
462  "whether array must be freed",
464  }
465 
467  inline void unref_all()
468  {
469  /* SG_UNREF all my elements */
470  for (index_t i=0; i<m_array.get_num_elements(); ++i)
471  {
472  SG_UNREF(*m_array.get_element_ptr(i));
473  }
474  }
475 
476  private:
478  DynArray<CSGObject*> m_array;
479 
481  int32_t dim1_size;
482 
484  int32_t dim2_size;
485 
487  int32_t dim3_size;
488 
490  const char* name;
491 
492 };
493 }
494 #endif /* _DYNAMIC_OBJECT_ARRAY_H_ */
const char * get_array_name() const
int32_t index_t
Definition: common.h:60
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...
#define SG_UNREF(x)
Definition: SGRefObject.h:35
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:1000
Parameter * m_parameters
Definition: SGObject.h:482
int32_t find_element(CSGObject *elem) const
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:102
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)
#define SG_REF(x)
Definition: SGRefObject.h:34
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:32
virtual void load_serializable_pre()
Definition: SGObject.cpp:990
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:324
CSGObject * get_element_safe(int32_t index) const
CSGObject * get_element(int32_t index) const
#define SG_ADD(...)
Definition: SGObject.h:71
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 机器学习工具包 - 项目文档