SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  * Copyright (C) 2012 Engeniy Andreev (gsomix)
10  */
11 
12 #ifndef _DYNARRAY_H_
13 #define _DYNARRAY_H_
14 
15 #include <shogun/lib/common.h>
17 
18 namespace shogun
19 {
20 template <class T> class CDynamicArray;
21 
30 template <class T> class DynArray
31 {
32  template<class U> friend class CDynamicArray;
33  friend class CDynamicObjectArray;
34  friend class CCommUlongStringKernel;
35 
36  public:
42  DynArray(int32_t p_resize_granularity=128, bool tracable=true)
43  {
44  resize_granularity=p_resize_granularity;
45  free_array=true;
46  use_sg_mallocs=tracable;
47 
48  if (use_sg_mallocs)
49  array=SG_MALLOC(T, p_resize_granularity);
50  else
51  array=(T*) malloc(size_t(p_resize_granularity)*sizeof(T));
52 
53  num_elements=p_resize_granularity;
55  }
56 
65  DynArray(T* p_array, int32_t p_array_size, bool p_free_array, bool p_copy_array, bool tracable=true)
66  {
67  resize_granularity=p_array_size;
68  free_array=false;
69  use_sg_mallocs=tracable;
70 
71  array=NULL;
72  set_array(p_array, p_array_size, p_array_size, p_free_array, p_copy_array);
73  }
74 
81  DynArray(const T* p_array, int32_t p_array_size, bool tracable=true)
82  {
83  resize_granularity=p_array_size;
84  free_array=false;
85  use_sg_mallocs=tracable;
86 
87  array=NULL;
88  set_array(p_array, p_array_size, p_array_size);
89  }
90 
92  virtual ~DynArray()
93  {
94  if (array!=NULL && free_array)
95  {
96  if (use_sg_mallocs)
97  SG_FREE(array);
98  else
99  free(array);
100  }
101  }
102 
108  inline int32_t set_granularity(int32_t g)
109  {
110  g=CMath::max(g,1);
111  this->resize_granularity = g;
112  return g;
113  }
114 
119  inline int32_t get_array_size() const
120  {
121  return num_elements;
122  }
123 
128  inline int32_t get_num_elements() const
129  {
130  return current_num_elements;
131  }
132 
140  inline T get_element(int32_t index) const
141  {
142  return array[index];
143  }
144 
149  inline T get_last_element() const
150  {
151  return array[current_num_elements-1];
152  }
153 
161  inline T* get_element_ptr(int32_t index)
162  {
163  return &array[index];
164  }
165 
173  inline T get_element_safe(int32_t index) const
174  {
175  if (index>=get_num_elements())
176  {
177  SG_SERROR("array index out of bounds (%d >= %d)\n",
178  index, get_num_elements());
179  }
180  return array[index];
181  }
182 
189  inline bool set_element(T element, int32_t index)
190  {
191  if (index < 0)
192  {
193  return false;
194  }
195  else if (index <= current_num_elements-1)
196  {
197  array[index]=element;
198  return true;
199  }
200  else if (index < num_elements)
201  {
202  array[index]=element;
203  current_num_elements=index+1;
204  return true;
205  }
206  else
207  {
208  if (free_array && resize_array(index))
209  return set_element(element, index);
210  else
211  return false;
212  }
213  }
214 
221  inline bool insert_element(T element, int32_t index)
222  {
224  {
225  for (int32_t i=current_num_elements-2; i>index; i--)
226  {
227  array[i]=array[i-1];
228  }
229  array[index]=element;
230 
231  return true;
232  }
233 
234  return false;
235  }
236 
242  inline bool append_element(T element)
243  {
244  return set_element(element, current_num_elements);
245  }
246 
252  inline void push_back(T element)
253  {
254  if (get_num_elements() < 0)
255  set_element(element, 0);
256  else
257  set_element(element, get_num_elements());
258  }
259 
263  inline void pop_back()
264  {
265  if (get_num_elements() <= 0)
266  return;
267 
269  }
270 
276  inline T back() const
277  {
278  if (get_num_elements() <= 0)
279  return get_element(0);
280 
281  return get_element(get_num_elements()-1);
282  }
283 
290  int32_t find_element(T element) const
291  {
292  int32_t idx=-1;
293  int32_t num=get_num_elements();
294 
295  for (int32_t i=0; i<num; i++)
296  {
297  if (array[i] == element)
298  {
299  idx=i;
300  break;
301  }
302  }
303 
304  return idx;
305  }
306 
313  inline bool delete_element(int32_t idx)
314  {
315  if (idx>=0 && idx<=current_num_elements-1)
316  {
317  for (int32_t i=idx; i<current_num_elements-1; i++)
318  array[i]=array[i+1];
319 
320  current_num_elements--;
321 
322  if (num_elements - current_num_elements - 1
324  resize_array(current_num_elements);
325 
326  return true;
327  }
328 
329  return false;
330  }
331 
338  bool resize_array(int32_t n, bool exact_resize=false)
339  {
340  int32_t new_num_elements=n;
341 
342  if (!exact_resize)
343  {
344  new_num_elements=((n/resize_granularity)+1)*resize_granularity;
345  }
346 
347 
348  if (use_sg_mallocs)
349  array = SG_REALLOC(T, array, num_elements, new_num_elements);
350  else
351  array = (T*) realloc(array, new_num_elements*sizeof(T));
352 
353  //in case of shrinking we must adjust last element idx
354  if (n-1<current_num_elements-1)
356 
357  num_elements=new_num_elements;
358  return true;
359 
360  return array || new_num_elements==0;
361  }
362 
370  inline T* get_array() const
371  {
372  return array;
373  }
374 
383  inline void set_array(T* p_array, int32_t p_num_elements,
384  int32_t p_array_size, bool p_free_array, bool p_copy_array)
385  {
386  if (array!=NULL && free_array)
387  SG_FREE(array);
388 
389  if (p_copy_array)
390  {
391  if (use_sg_mallocs)
392  array=SG_MALLOC(T, p_array_size);
393  else
394  array=(T*) malloc(p_array_size*sizeof(T));
395  memcpy(array, p_array, p_array_size*sizeof(T));
396  }
397  else
398  array=p_array;
399 
400  num_elements=p_array_size;
401  current_num_elements=p_num_elements;
402  free_array=p_free_array;
403  }
404 
411  inline void set_array(const T* p_array, int32_t p_num_elements,
412  int32_t p_array_size)
413  {
414  if (array!=NULL && free_array)
415  SG_FREE(array);
416 
417  if (use_sg_mallocs)
418  array=SG_MALLOC(T, p_array_size);
419  else
420  array=(T*) malloc(p_array_size*sizeof(T));
421  memcpy(array, p_array, p_array_size*sizeof(T));
422 
423  num_elements=p_array_size;
424  current_num_elements=p_num_elements;
425  free_array=true;
426  }
427 
429  inline void clear_array(T value)
430  {
431  if (current_num_elements-1 >= 0)
432  {
433  for (int32_t i=0; i<current_num_elements; i++)
434  array[i]=value;
435  }
436  }
437 
439  void reset(T value)
440  {
441  clear_array(value);
443  }
444 
446  void shuffle()
447  {
448  for (index_t i=0; i<=current_num_elements-1; ++i)
450  }
451 
453  void shuffle(CRandom * rand)
454  {
455  for (index_t i=0; i<=current_num_elements-1; ++i)
457  }
458 
460  void set_const(const T& const_element)
461  {
462  for (int32_t i=0; i<num_elements; i++)
463  array[i]=const_element;
464  }
465 
475  inline T operator[](int32_t index) const
476  {
477  return array[index];
478  }
479 
487  {
489 
490  /* check if orig array is larger than current, create new array */
491  if (orig.num_elements>num_elements)
492  {
493  SG_FREE(array);
494 
495  if (use_sg_mallocs)
496  array=SG_MALLOC(T, orig.num_elements);
497  else
498  array=(T*) malloc(sizeof(T)*orig.num_elements);
499  }
500 
501  memcpy(array, orig.array, sizeof(T)*orig.num_elements);
504 
505  return *this;
506  }
507 
509  virtual const char* get_name() const { return "DynArray"; }
510 
511  protected:
514 
516  T* array;
517 
519  int32_t num_elements;
520 
523 
526 
529 };
530 }
531 #endif /* _DYNARRAY_H_ */
int32_t current_num_elements
Definition: DynArray.h:522
T get_element(int32_t index) const
Definition: DynArray.h:140
T operator[](int32_t index) const
Definition: DynArray.h:475
void shuffle(CRandom *rand)
Definition: DynArray.h:453
uint64_t random(uint64_t min_value, uint64_t max_value)
Definition: Random.h:100
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
The CommUlongString kernel may be used to compute the spectrum kernel from strings that have been map...
int32_t get_array_size() const
Definition: DynArray.h:119
void shuffle()
Definition: DynArray.h:446
void set_array(const T *p_array, int32_t p_num_elements, int32_t p_array_size)
Definition: DynArray.h:411
bool use_sg_mallocs
Definition: DynArray.h:525
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
static uint64_t random()
Definition: Math.h:590
int32_t find_element(T element) const
Definition: DynArray.h:290
virtual ~DynArray()
Definition: DynArray.h:92
void push_back(T element)
Definition: DynArray.h:252
T back() const
Definition: DynArray.h:276
Template Dynamic array class that creates an array that can be used like a list or an array...
Definition: DynArray.h:30
void clear_array(T value)
Definition: DynArray.h:429
void pop_back()
Definition: DynArray.h:263
DynArray(const T *p_array, int32_t p_array_size, bool tracable=true)
Definition: DynArray.h:81
virtual const char * get_name() const
Definition: DynArray.h:509
static T max(T a, T b)
return the maximum of two integers
Definition: Math.h:160
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
bool set_element(T element, int32_t index)
Definition: DynArray.h:189
T get_last_element() const
Definition: DynArray.h:149
bool resize_array(int32_t n, bool exact_resize=false)
Definition: DynArray.h:338
DynArray(T *p_array, int32_t p_array_size, bool p_free_array, bool p_copy_array, bool tracable=true)
Definition: DynArray.h:65
: Pseudo random number geneartor
Definition: Random.h:32
Template Dynamic array class that creates an array that can be used like a list or an array...
Definition: DynArray.h:20
void set_array(T *p_array, int32_t p_num_elements, int32_t p_array_size, bool p_free_array, bool p_copy_array)
Definition: DynArray.h:383
#define SG_SERROR(...)
Definition: SGIO.h:181
T * get_array() const
Definition: DynArray.h:370
DynArray< T > & operator=(DynArray< T > &orig)
Definition: DynArray.h:486
int32_t num_elements
Definition: DynArray.h:519
static void swap(T &a, T &b)
swap e.g. floats a and b
Definition: Math.h:292
void reset(T value)
Definition: DynArray.h:439
T * get_element_ptr(int32_t index)
Definition: DynArray.h:161
int32_t resize_granularity
Definition: DynArray.h:513
T get_element_safe(int32_t index) const
Definition: DynArray.h:173
void set_const(const T &const_element)
Definition: DynArray.h:460
DynArray(int32_t p_resize_granularity=128, bool tracable=true)
Definition: DynArray.h:42

SHOGUN Machine Learning Toolbox - Documentation