SHOGUN  v2.0.0
 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/memory.h>
19 
20 #ifndef VARRAY_H__
21 #define VARRAY_H__
22 
23 namespace shogun
24 {
39 template<class T> class v_array
40 {
41 public:
42 
48  {
49  begin = NULL;
50  end = NULL;
51  end_array = NULL;
52  }
53 
59  {
60  SG_FREE(begin);
61  }
62 
71  T& operator[](unsigned int i) { return begin[i]; }
72 
78  inline T last() { return *(end-1); }
79 
85  inline T pop() { return *(--end); }
86 
92  inline bool empty() { return begin == end; }
93 
98  inline void decr() { end--; }
99 
105  inline unsigned int index() { return end-begin; }
106 
111  inline void erase() { end = begin; }
112 
118  void push(const T &new_elem);
119 
126  void push_many(const T* new_elem, size_t num);
127 
134  void reserve(size_t length);
135 
142  void calloc_reserve(size_t length);
143 
150  v_array<T> pop(v_array< v_array<T> > &stack);
151 
152 public:
153 
155  T* begin;
156 
158  T* end;
159 
162 
163 };
164 
165 template<class T>
166 inline void v_array<T>::push(const T &new_elem)
167 {
168  if(end == end_array)
169  {
170  size_t old_length = end_array - begin;
171  size_t new_length = 2 * old_length + 3;
172  //size_t new_length = old_length + 1;
173  begin = SG_REALLOC(T, begin, new_length);
174  end = begin + old_length;
175  end_array = begin + new_length;
176  }
177  *(end++) = new_elem;
178 }
179 
180 template<class T>
181 inline void v_array<T>::push_many(const T* new_elem, size_t num)
182 {
183  if(end+num >= end_array)
184  {
185  size_t length = end - begin;
186  size_t new_length = CMath::max(2 * (size_t)(end_array - begin) + 3,
187  end - begin + num);
188  begin = SG_REALLOC(T, begin, new_length);
189  end = begin + length;
190  end_array = begin + new_length;
191  }
192  memcpy(end, new_elem, num * sizeof(T));
193  end += num;
194 }
195 
196 template<class T>
197 inline void v_array<T>::reserve(size_t length)
198 {
199  size_t old_length = end_array-begin;
200  begin = SG_REALLOC(T, begin, length);
201  if (old_length < length)
202  memset(begin + old_length, 0, (length - old_length)*sizeof(T));
203 
204  end = begin;
205  end_array = begin + length;
206 }
207 
208 template<class T>
209 inline void v_array<T>::calloc_reserve(size_t length)
210 {
211  begin = SG_CALLOC(T, length);
212  end = begin;
213  end_array = begin + length;
214 }
215 
216 template<class T>
218 {
219  if (stack.end != stack.begin)
220  return *(--stack.end);
221  else
222  return v_array<T>();
223 }
224 }
225 #endif // VARRAY_H__

SHOGUN Machine Learning Toolbox - Documentation