SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
62  T& operator[](unsigned int i) { return begin[i]; }
63 
69  inline T last() { return *(end-1); }
70 
76  inline T pop() { return *(--end); }
77 
83  inline bool empty() { return begin == end; }
84 
89  inline void decr() { end--; }
90 
96  inline unsigned int index() { return end-begin; }
97 
102  inline void erase() { end = begin; }
103 
109  void push(const T &new_elem);
110 
117  void push_many(const T* new_elem, size_t num);
118 
125  void reserve(size_t length);
126 
133  void calloc_reserve(size_t length);
134 
141  v_array<T> pop(v_array< v_array<T> > &stack);
142 
143 public:
144 
146  T* begin;
147 
149  T* end;
150 
153 
154 };
155 
156 template<class T>
157 inline void v_array<T>::push(const T &new_elem)
158 {
159  if(end == end_array)
160  {
161  size_t old_length = end_array - begin;
162  size_t new_length = 2 * old_length + 3;
163  //size_t new_length = old_length + 1;
164  begin = SG_REALLOC(T, begin, new_length);
165  end = begin + old_length;
166  end_array = begin + new_length;
167  }
168  *(end++) = new_elem;
169 }
170 
171 template<class T>
172 inline void v_array<T>::push_many(const T* new_elem, size_t num)
173 {
174  if(end+num >= end_array)
175  {
176  size_t length = end - begin;
177  size_t new_length = CMath::max(2 * (size_t)(end_array - begin) + 3,
178  end - begin + num);
179  begin = SG_REALLOC(T, begin, new_length);
180  end = begin + length;
181  end_array = begin + new_length;
182  }
183  memcpy(end, new_elem, num * sizeof(T));
184  end += num;
185 }
186 
187 template<class T>
188 inline void v_array<T>::reserve(size_t length)
189 {
190  size_t old_length = end_array-begin;
191  begin = SG_REALLOC(T, begin, length);
192  if (old_length < length)
193  bzero(begin + old_length, (length - old_length)*sizeof(T));
194 
195  end = begin;
196  end_array = begin + length;
197 }
198 
199 template<class T>
200 inline void v_array<T>::calloc_reserve(size_t length)
201 {
202  begin = SG_CALLOC(T, length);
203  end = begin;
204  end_array = begin + length;
205 }
206 
207 template<class T>
209 {
210  if (stack.end != stack.begin)
211  return *(--stack.end);
212  else
213  return v_array<T>();
214 }
215 }
216 #endif // VARRAY_H__

SHOGUN Machine Learning Toolbox - Documentation