SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DynArray.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  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef _DYNARRAY_H_
12 #define _DYNARRAY_H_
13 
14 #include <shogun/lib/common.h>
15 #include <shogun/lib/memory.h>
17 
18 namespace shogun
19 {
20 template <class T> class CDynamicArray;
21 template <class T> class CDynamicObjectArray;
22 
31 template <class T> class DynArray
32 {
33  template<class U> friend class CDynamicArray;
34  template<class U> friend class CDynamicObjectArray;
35  friend class CCommUlongStringKernel;
36 
37  public:
43  DynArray(int32_t p_resize_granularity=128, bool tracable=true)
44  {
45  this->resize_granularity=p_resize_granularity;
46  use_sg_mallocs=tracable;
47 
48  if (use_sg_mallocs)
49  array=SG_CALLOC(T, p_resize_granularity);
50  else
51  array=(T*) calloc(p_resize_granularity, sizeof(T));
52 
53  num_elements=p_resize_granularity;
55  }
56 
58  virtual ~DynArray()
59  {
60  if (use_sg_mallocs)
61  SG_FREE(array);
62  else
63  free(array);
64  }
65 
71  inline int32_t set_granularity(int32_t g)
72  {
73  g=CMath::max(g,128);
74  this->resize_granularity = g;
75  return g;
76  }
77 
82  inline int32_t get_array_size() const
83  {
84  return num_elements;
85  }
86 
91  inline int32_t get_num_elements() const
92  {
93  return last_element_idx+1;
94  }
95 
103  inline T get_element(int32_t index) const
104  {
105  return array[index];
106  }
107 
115  inline T* get_element_ptr(int32_t index)
116  {
117  return &array[index];
118  }
119 
127  inline T get_element_safe(int32_t index) const
128  {
129  if (index>=get_num_elements())
130  {
131  SG_SERROR("array index out of bounds (%d >= %d)\n",
132  index, get_num_elements());
133  }
134  return array[index];
135  }
136 
143  inline bool set_element(T element, int32_t index)
144  {
145  if (index < 0)
146  {
147  return false;
148  }
149  else if (index <= last_element_idx)
150  {
151  array[index]=element;
152  return true;
153  }
154  else if (index < num_elements)
155  {
156  array[index]=element;
157  last_element_idx=index;
158  return true;
159  }
160  else
161  {
162  if (resize_array(index))
163  return set_element(element, index);
164  else
165  return false;
166  }
167  }
168 
175  inline bool insert_element(T element, int32_t index)
176  {
178  {
179  for (int32_t i=last_element_idx-1; i>index; i--)
180  {
181  array[i]=array[i-1];
182  }
183  array[index]=element;
184 
185  return true;
186  }
187 
188  return false;
189  }
190 
196  inline bool append_element(T element)
197  {
198  return set_element(element, last_element_idx+1);
199  }
200 
206  inline void push_back(T element)
207  {
208  if (get_num_elements() < 0) set_element(element, 0);
209  else set_element(element, get_num_elements());
210  }
211 
215  inline void pop_back()
216  {
217  if (get_num_elements() <= 0) return;
219  }
220 
226  inline T back() const
227  {
228  if (get_num_elements() <= 0) return get_element(0);
229  return get_element(get_num_elements()-1);
230  }
231 
238  int32_t find_element(T element) const
239  {
240  int32_t idx=-1;
241  int32_t num=get_num_elements();
242 
243  for (int32_t i=0; i<num; i++)
244  {
245  if (array[i] == element)
246  {
247  idx=i;
248  break;
249  }
250  }
251 
252  return idx;
253  }
254 
261  inline bool delete_element(int32_t idx)
262  {
263  if (idx>=0 && idx<=last_element_idx)
264  {
265  for (int32_t i=idx; i<last_element_idx; i++)
266  array[i]=array[i+1];
267 
268  memset(&array[last_element_idx], 0, sizeof(T));
269  last_element_idx--;
270 
271  if (num_elements - last_element_idx
273  resize_array(last_element_idx+1);
274 
275  return true;
276  }
277 
278  return false;
279  }
280 
286  bool resize_array(int32_t n)
287  {
288  int32_t new_num_elements= ((n/resize_granularity)+1)
290 
291  T* p;
292 
293  if (use_sg_mallocs)
294  p = SG_REALLOC(T, array, new_num_elements);
295  else
296  p = (T*) realloc(array, new_num_elements*sizeof(T));
297  if (p)
298  {
299  array=p;
300  if (new_num_elements > num_elements)
301  {
302  memset(&array[num_elements], 0,
303  (new_num_elements-num_elements)*sizeof(T));
304  }
305  else if (n+1<new_num_elements)
306  {
307  memset(&array[n+1], 0,
308  (new_num_elements-n-1)*sizeof(T));
309  }
310 
311  //in case of shrinking we must adjust last element idx
312  if (n-1<last_element_idx)
313  last_element_idx=n-1;
314 
315  num_elements=new_num_elements;
316  return true;
317  }
318  else
319  return false;
320  }
321 
329  inline T* get_array() const
330  {
331  return array;
332  }
333 
340  inline void set_array(T* p_array, int32_t p_num_elements,
341  int32_t array_size)
342  {
343  SG_FREE(this->array);
344  this->array=p_array;
345  this->num_elements=array_size;
346  this->last_element_idx=p_num_elements-1;
347  }
348 
350  inline void clear_array()
351  {
352  if (last_element_idx >= 0)
353  memset(array, 0, (last_element_idx+1)*sizeof(T));
354  }
355 
357  void shuffle()
358  {
359  for (index_t i=0; i<=last_element_idx; ++i)
361  }
362 
372  inline T operator[](int32_t index) const
373  {
374  return array[index];
375  }
376 
384  {
386 
387  /* check if orig array is larger than current, create new array */
388  if (orig.num_elements>num_elements)
389  {
390  SG_FREE(array);
391 
392  if (use_sg_mallocs)
393  array=SG_MALLOC(T, orig.num_elements);
394  else
395  array=(T*) malloc(sizeof(T)*orig.num_elements);
396  }
397 
398  memcpy(array, orig.array, sizeof(T)*orig.num_elements);
401 
402  return *this;
403  }
404 
406  inline virtual const char* get_name() const { return "DynArray"; }
407 
408  protected:
411 
413  T* array;
414 
416  int32_t num_elements;
417 
420 
423 };
424 }
425 #endif /* _DYNARRAY_H_ */

SHOGUN Machine Learning Toolbox - Documentation