SHOGUN  v3.2.0
SGDynamicRefObjectArray.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_REFOBJECT_ARRAY_H_
13 #define _DYNAMIC_REFOBJECT_ARRAY_H_
14 
15 #include <shogun/base/RefObject.h>
16 #include <shogun/base/DynArray.h>
17 #include <shogun/base/Parameter.h>
18 
19 namespace shogun
20 {
32 {
33  public:
36  : CSGObject(), m_array(), name("Array")
37  {
38  dim1_size=1;
39  dim2_size=1;
40  dim3_size=1;
41  }
42 
49  SGDynamicRefObjectArray(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 
64  SGDynamicRefObjectArray(CRefObject** p_array, int32_t p_dim1_size, bool p_free_array=true, bool p_copy_array=false)
65  : CSGObject(), m_array(p_array, p_dim1_size, p_free_array, p_copy_array), name("Array")
66  {
67  dim1_size=p_dim1_size;
68  dim2_size=1;
69  dim3_size=1;
70  }
71 
80  SGDynamicRefObjectArray(CRefObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
81  bool p_free_array=true, bool p_copy_array=false)
82  : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size, p_free_array, p_copy_array), name("Array")
83  {
84  dim1_size=p_dim1_size;
85  dim2_size=p_dim2_size;
86  dim3_size=1;
87  }
88 
98  SGDynamicRefObjectArray(CRefObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
99  int32_t p_dim3_size, bool p_free_array=true, bool p_copy_array=false)
100  : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size, p_free_array, p_copy_array), name("Array")
101  {
102  dim1_size=p_dim1_size;
103  dim2_size=p_dim2_size;
104  dim3_size=p_dim3_size;
105  }
106 
107  virtual ~SGDynamicRefObjectArray() { unref_all(); }
108 
114  inline int32_t set_granularity(int32_t g)
115  { return m_array.set_granularity(g); }
116 
121  inline int32_t get_array_size()
122  {
123  return m_array.get_array_size();
124  }
125 
131  inline void get_array_size(int32_t& dim1, int32_t& dim2)
132  {
133  dim1=dim1_size;
134  dim2=dim2_size;
135  }
136 
143  inline void get_array_size(int32_t& dim1, int32_t& dim2, int32_t& dim3)
144  {
145  dim1=dim1_size;
146  dim2=dim2_size;
147  dim3=dim3_size;
148  }
149 
154  inline int32_t get_dim1() { return dim1_size; }
155 
160  inline int32_t get_dim2() { return dim2_size; }
161 
166  inline int32_t get_dim3() { return dim3_size; }
167 
172  inline int32_t get_num_elements() const
173  {
174  return m_array.get_num_elements();
175  }
176 
184  inline CRefObject* get_element(int32_t index) const
185  {
186  CRefObject* elem=m_array.get_element(index);
187  SG_REF(elem);
188  return elem;
189  }
190 
198  inline CRefObject* element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
199  {
200  return get_element(idx1+dim1_size*(idx2+dim2_size*idx3));
201  }
202 
207  inline CRefObject* get_last_element() const
208  {
209  CRefObject* e=m_array.get_last_element();
210  SG_REF(e);
211  return e;
212  }
213 
221  inline CRefObject* get_element_safe(int32_t index) const
222  {
223  CRefObject* e=m_array.get_element_safe(index);
224  SG_REF(e);
225  return e;
226  }
227 
236  inline bool set_element(CRefObject* e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
237  {
238  int32_t idx = idx1+dim1_size*(idx2+dim2_size*idx3);
239  CRefObject* old=NULL;
240 
241  if (idx<get_num_elements())
242  old = (CRefObject*) m_array.get_element(idx);
243 
244  bool success=m_array.set_element(e, idx);
245  if (success)
246  {
247  SG_REF(e);
248  SG_UNREF(old);
249  }
250 
251  /* ref before unref to prevent deletion if new=old */
252  return success;
253  }
254 
261  inline bool insert_element(CRefObject* e, int32_t index)
262  {
263  bool success=m_array.insert_element(e, index);
264  if (success)
265  SG_REF(e);
266 
267  return success;
268  }
269 
275  inline bool append_element(CRefObject* e)
276  {
277  bool success=m_array.append_element(e);
278  if (success)
279  SG_REF(e);
280 
281  return success;
282  }
283 
289  inline void push_back(CRefObject* e)
290  {
291  SG_REF(e);
292  m_array.push_back(e);
293  }
294 
298  inline void pop_back()
299  {
300  CRefObject* e=m_array.back();
301  SG_UNREF(e);
302 
303  m_array.pop_back();
304  }
305 
311  inline CRefObject* back() const
312  {
313  CRefObject* e=m_array.back();
314  SG_REF(e);
315  return e;
316  }
317 
324  inline int32_t find_element(CRefObject* elem) const
325  {
326  return m_array.find_element(elem);
327  }
328 
335  inline bool delete_element(int32_t idx)
336  {
337  CRefObject* e=m_array.get_element(idx);
338  SG_UNREF(e);
339  m_array.set_element(NULL, idx);
340 
341  return m_array.delete_element(idx);
342  }
343 
345  inline void clear_array()
346  {
347  unref_all();
348  m_array.clear_array(NULL);
349  }
350 
352  inline void reset_array()
353  {
354  unref_all();
355  m_array.reset(NULL);
356  }
357 
364  {
365  /* SG_REF all new elements (implicitly) */
366  for (index_t i=0; i<orig.get_num_elements(); ++i)
367  orig.get_element(i);
368 
369  /* unref after adding to avoid possible deletion */
370  unref_all();
371 
372  /* copy pointer DynArray */
373  m_array=orig.m_array;
374  return *this;
375  }
376 
378  inline CRefObject** get_array() const { return m_array.get_array(); }
379 
381  inline void shuffle() { m_array.shuffle(); }
382 
384  inline void shuffle(CRandom * rand) { m_array.shuffle(rand); }
385 
390  inline void set_array_name(const char* p_name)
391  {
392  name=p_name;
393  }
394 
399  inline const char* get_array_name() const { return name; }
400 
402  virtual const char* get_name() const
403  { return "DynamicRefObjectArray"; }
404 
405  private:
407  inline void unref_all()
408  {
409  /* SG_UNREF all my elements */
410  for (index_t i=0; i<m_array.get_num_elements(); ++i)
411  {
412  SG_UNREF(*m_array.get_element_ptr(i));
413  }
414  }
415 
416  private:
418  DynArray<CRefObject*> m_array;
419 
421  int32_t dim1_size;
422 
424  int32_t dim2_size;
425 
427  int32_t dim3_size;
428 
430  const char* name;
431 
432 };
433 }
434 #endif /* _DYNAMIC_REFOBJECT_ARRAY_H_ */
T get_element(int32_t index) const
Definition: DynArray.h:140
Dynamic array class for CRefObject pointers that creates an array that can be used like a list or an ...
Class SGRefObject is a reference count based memory management class.
Definition: SGRefObject.h:46
bool insert_element(T element, int32_t index)
Definition: DynArray.h:221
bool append_element(T element)
Definition: DynArray.h:242
int32_t index_t
Definition: common.h:60
bool set_element(CRefObject *e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
SGDynamicRefObjectArray(int32_t dim1, int32_t dim2=1, int32_t dim3=1)
int32_t get_array_size() const
Definition: DynArray.h:119
#define SG_UNREF(x)
Definition: SGRefObject.h:35
void shuffle()
Definition: DynArray.h:446
int32_t get_num_elements() const
Definition: DynArray.h:128
bool delete_element(int32_t idx)
Definition: DynArray.h:313
int32_t set_granularity(int32_t g)
Definition: DynArray.h:108
CRefObject * element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
SGDynamicRefObjectArray(CRefObject **p_array, int32_t p_dim1_size, int32_t p_dim2_size, bool p_free_array=true, bool p_copy_array=false)
virtual const char * get_name() const
int32_t find_element(T element) const
Definition: DynArray.h:290
void get_array_size(int32_t &dim1, int32_t &dim2)
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:102
void push_back(T element)
Definition: DynArray.h:252
SGDynamicRefObjectArray & operator=(SGDynamicRefObjectArray &orig)
void set_array_name(const char *p_name)
T back() const
Definition: DynArray.h:276
void clear_array(T value)
Definition: DynArray.h:429
void pop_back()
Definition: DynArray.h:263
#define SG_REF(x)
Definition: SGRefObject.h:34
bool set_element(T element, int32_t index)
Definition: DynArray.h:189
T get_last_element() const
Definition: DynArray.h:149
int32_t find_element(CRefObject *elem) const
: Pseudo random number geneartor
Definition: Random.h:32
CRefObject * get_element(int32_t index) const
bool insert_element(CRefObject *e, int32_t index)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:16
T * get_array() const
Definition: DynArray.h:370
SGDynamicRefObjectArray(CRefObject **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)
void get_array_size(int32_t &dim1, int32_t &dim2, int32_t &dim3)
void reset(T value)
Definition: DynArray.h:439
T * get_element_ptr(int32_t index)
Definition: DynArray.h:161
CRefObject * get_element_safe(int32_t index) const
T get_element_safe(int32_t index) const
Definition: DynArray.h:173
SGDynamicRefObjectArray(CRefObject **p_array, int32_t p_dim1_size, bool p_free_array=true, bool p_copy_array=false)

SHOGUN Machine Learning Toolbox - Documentation