SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
v_array.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights
3  embodied in the content of this file are licensed under the BSD
4  (revised) open source license.
5 
6  Copyright (c) 2011 Berlin Institute of Technology and Max-Planck-Society.
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  Shogun adjustments (w) 2011 Shashwat Lal Das
14 */
15 
16 #include <stdlib.h>
17 #include <shogun/lib/common.h>
19 
20 #ifndef VARRAY_H__
21 #define VARRAY_H__
22 
23 #include <shogun/lib/config.h>
24 
25 namespace shogun
26 {
41 template<class T> class v_array
42 {
43 public:
44 
50  {
51  begin = NULL;
52  end = NULL;
53  end_array = NULL;
54  }
55 
61  {
62  SG_FREE(begin);
63  }
64 
73  T& operator[](unsigned int i) { return begin[i]; }
74 
80  inline T last() { return *(end-1); }
81 
87  inline T pop() { return *(--end); }
88 
94  inline bool empty() { return begin == end; }
95 
100  inline void decr() { end--; }
101 
107  inline unsigned int index() { return end-begin; }
108 
113  inline void erase() { end = begin; }
114 
120  void push(const T &new_elem);
121 
128  void push_many(const T* new_elem, size_t num);
129 
136  void reserve(size_t length);
137 
144  void calloc_reserve(size_t length);
145 
152  v_array<T> pop(v_array< v_array<T> > &stack);
153 
154 public:
155 
157  T* begin;
158 
160  T* end;
161 
164 
165 };
166 
167 template<class T>
168 inline void v_array<T>::push(const T &new_elem)
169 {
170  if(end == end_array)
171  {
172  size_t old_length = end_array - begin;
173  size_t new_length = 2 * old_length + 3;
174  //size_t new_length = old_length + 1;
175  begin = SG_REALLOC(T, begin, old_length, new_length);
176  end = begin + old_length;
177  end_array = begin + new_length;
178  }
179  *(end++) = new_elem;
180 }
181 
182 template<class T>
183 inline void v_array<T>::push_many(const T* new_elem, size_t num)
184 {
185  if(end+num >= end_array)
186  {
187  size_t length = end - begin;
188  size_t new_length = CMath::max(2 * (size_t)(end_array - begin) + 3,
189  end - begin + num);
190  begin = SG_REALLOC(T, begin, length, new_length);
191  end = begin + length;
192  end_array = begin + new_length;
193  }
194  memcpy(end, new_elem, num * sizeof(T));
195  end += num;
196 }
197 
198 template<class T>
199 inline void v_array<T>::reserve(size_t length)
200 {
201  size_t old_length = end_array-begin;
202  begin = SG_REALLOC(T, begin, old_length, length);
203  if (old_length < length)
204  memset(begin + old_length, 0, (length - old_length)*sizeof(T));
205 
206  end = begin;
207  end_array = begin + length;
208 }
209 
210 template<class T>
211 inline void v_array<T>::calloc_reserve(size_t length)
212 {
213  begin = SG_CALLOC(T, length);
214  end = begin;
215  end_array = begin + length;
216 }
217 
218 template<class T>
220 {
221  if (stack.end != stack.begin)
222  return *(--stack.end);
223  else
224  return v_array<T>();
225 }
226 }
227 #endif // VARRAY_H__

SHOGUN Machine Learning Toolbox - Documentation