SHOGUN  v2.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  m_parameters->add_vector(&m_array.array, &m_array.num_elements,
37  "array", "Memory for dynamic array.");
38  SG_ADD(&m_array.last_element_idx, "last_element_idx",
39  "Element with largest index.", MS_NOT_AVAILABLE);
40  SG_ADD(&m_array.resize_granularity, "resize_granularity",
41  "shrink/grow step size.", MS_NOT_AVAILABLE);
42 
43  dim1_size=1;
44  dim2_size=1;
45  dim3_size=1;
46  }
47 
54  CDynamicObjectArray(int32_t dim1, int32_t dim2=1, int32_t dim3=1)
55  : CSGObject(), m_array(dim1*dim2*dim3), name("Array")
56  {
57  m_parameters->add_vector(&m_array.array, &m_array.num_elements,
58  "array", "Memory for dynamic array.");
59  SG_ADD(&m_array.last_element_idx, "last_element_idx",
60  "Element with largest index.", MS_NOT_AVAILABLE);
61  SG_ADD(&m_array.resize_granularity, "resize_granularity",
62  "shrink/grow step size.", MS_NOT_AVAILABLE);
63 
64  dim1_size=dim1;
65  dim2_size=dim2;
66  dim3_size=dim3;
67  }
68 
76  CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, bool p_free_array=true, bool p_copy_array=false)
77  : CSGObject(), m_array(p_array, p_dim1_size, p_free_array, p_copy_array), name("Array")
78  {
79  m_parameters->add_vector(&m_array.array, &m_array.num_elements,
80  "array", "Memory for dynamic array.");
81  SG_ADD(&m_array.last_element_idx, "last_element_idx",
82  "Element with largest index.", MS_NOT_AVAILABLE);
83  SG_ADD(&m_array.resize_granularity, "resize_granularity",
84  "shrink/grow step size.", MS_NOT_AVAILABLE);
85 
86  dim1_size=p_dim1_size;
87  dim2_size=1;
88  dim3_size=1;
89  }
90 
99  CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
100  bool p_free_array=true, bool p_copy_array=false)
101  : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size, p_free_array, p_copy_array), name("Array")
102  {
103  m_parameters->add_vector(&m_array.array, &m_array.num_elements,
104  "array", "Memory for dynamic array.");
105  SG_ADD(&m_array.last_element_idx, "last_element_idx",
106  "Element with largest index.", MS_NOT_AVAILABLE);
107  SG_ADD(&m_array.resize_granularity, "resize_granularity",
108  "shrink/grow step size.", MS_NOT_AVAILABLE);
109 
110  dim1_size=p_dim1_size;
111  dim2_size=p_dim2_size;
112  dim3_size=1;
113  }
114 
124  CDynamicObjectArray(CSGObject** p_array, int32_t p_dim1_size, int32_t p_dim2_size,
125  int32_t p_dim3_size, bool p_free_array=true, bool p_copy_array=false)
126  : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size, p_free_array, p_copy_array), name("Array")
127  {
128  m_parameters->add_vector(&m_array.array, &m_array.num_elements,
129  "array", "Memory for dynamic array.");
130  SG_ADD(&m_array.last_element_idx, "last_element_idx",
131  "Element with largest index.", MS_NOT_AVAILABLE);
132  SG_ADD(&m_array.resize_granularity, "resize_granularity",
133  "shrink/grow step size.", MS_NOT_AVAILABLE);
134 
135  dim1_size=p_dim1_size;
136  dim2_size=p_dim2_size;
137  dim3_size=p_dim3_size;
138  }
139 
140  virtual ~CDynamicObjectArray() { unref_all(); }
141 
147  inline int32_t set_granularity(int32_t g)
148  { return m_array.set_granularity(g); }
149 
154  inline int32_t get_array_size()
155  {
156  return m_array.get_array_size();
157  }
158 
164  inline void get_array_size(int32_t& dim1, int32_t& dim2)
165  {
166  dim1=dim1_size;
167  dim2=dim2_size;
168  }
169 
176  inline void get_array_size(int32_t& dim1, int32_t& dim2, int32_t& dim3)
177  {
178  dim1=dim1_size;
179  dim2=dim2_size;
180  dim3=dim3_size;
181  }
182 
187  inline int32_t get_dim1() { return dim1_size; }
188 
193  inline int32_t get_dim2() { return dim2_size; }
194 
199  inline int32_t get_dim3() { return dim3_size; }
200 
205  inline int32_t get_num_elements() const
206  {
207  return m_array.get_num_elements();
208  }
209 
217  inline CSGObject* get_element(int32_t index) const
218  {
219  CSGObject* elem=m_array.get_element(index);
220  SG_REF(elem);
221  return elem;
222  }
223 
231  inline CSGObject* element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
232  {
233  return get_element(idx1+dim1_size*(idx2+dim2_size*idx3));
234  }
235 
240  inline CSGObject* get_last_element() const
241  {
242  CSGObject* e=m_array.get_last_element();
243  SG_REF(e);
244  return e;
245  }
246 
254  inline CSGObject* get_element_safe(int32_t index) const
255  {
256  CSGObject* e=m_array.get_element_safe(index);
257  SG_REF(e);
258  return e;
259  }
260 
269  inline bool set_element(CSGObject* e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
270  {
271  CSGObject* old=(CSGObject*) m_array.get_element(idx1+dim1_size*(idx2+dim2_size*idx3));
272 
273  bool success=m_array.set_element(e, idx1+dim1_size*(idx2+dim2_size*idx3));
274  if (success)
275  {
276  SG_REF(e);
277  SG_UNREF(old);
278  }
279 
280  /* ref before unref to prevent deletion if new=old */
281  return success;
282  }
283 
290  inline bool insert_element(CSGObject* e, int32_t index)
291  {
292  bool success=m_array.insert_element(e, index);
293  if (success)
294  SG_REF(e);
295 
296  return success;
297  }
298 
304  inline bool append_element(CSGObject* e)
305  {
306  bool success=m_array.append_element(e);
307  if (success)
308  SG_REF(e);
309 
310  return success;
311  }
312 
318  inline void push_back(CSGObject* e)
319  {
320  SG_REF(e);
321  m_array.push_back(e);
322  }
323 
327  inline void pop_back()
328  {
329  CSGObject* e=m_array.back();
330  SG_UNREF(e);
331 
332  m_array.pop_back();
333  }
334 
340  inline CSGObject* back() const
341  {
342  CSGObject* e=m_array.back();
343  SG_REF(e);
344  return e;
345  }
346 
353  inline int32_t find_element(CSGObject* elem) const
354  {
355  return m_array.find_element(elem);
356  }
357 
364  inline bool delete_element(int32_t idx)
365  {
366  CSGObject* e=m_array.get_element(idx);
367  SG_UNREF(e);
368 
369  return m_array.delete_element(idx);
370  }
371 
379  inline bool resize_array(int32_t ndim1, int32_t ndim2=1, int32_t ndim3=1)
380  {
381  dim1_size=ndim1;
382  dim2_size=ndim2;
383  dim3_size=ndim3;
384  return m_array.resize_array(ndim1*ndim2*ndim3);
385  }
386 
388  inline void clear_array()
389  {
390  unref_all();
391  m_array.clear_array();
392  }
393 
395  inline void reset_array()
396  {
397  unref_all();
398  m_array.reset();
399  }
400 
407  {
408  /* SG_REF all new elements (implicitly) */
409  for (index_t i=0; i<orig.get_num_elements(); ++i)
410  orig.get_element(i);
411 
412  /* unref after adding to avoid possible deletion */
413  unref_all();
414 
415  /* copy pointer DynArray */
416  m_array=orig.m_array;
417  return *this;
418  }
419 
421  inline CSGObject** get_array() const { return m_array.get_array(); }
422 
424  inline void shuffle() { m_array.shuffle(); }
425 
430  inline void set_array_name(const char* p_name)
431  {
432  name=p_name;
433  }
434 
439  inline const char* get_array_name() const { return name; }
440 
442  inline virtual const char* get_name() const
443  { return "DynamicObjectArray"; }
444 
445  private:
447  inline void unref_all()
448  {
449  /* SG_UNREF all my elements */
450  for (index_t i=0; i<m_array.get_num_elements(); ++i)
451  {
452  CSGObject* elem=m_array.get_element(i);
453  SG_UNREF(elem);
454  }
455  }
456 
457  private:
459  DynArray<CSGObject*> m_array;
460 
462  int32_t dim1_size;
463 
465  int32_t dim2_size;
466 
468  int32_t dim3_size;
469 
471  const char* name;
472 
473 };
474 }
475 #endif /* _DYNAMIC_OBJECT_ARRAY_H_ */

SHOGUN Machine Learning Toolbox - Documentation