diff options
author | John Langford <jl@hunch.net> | 2009-08-01 00:19:24 +0400 |
---|---|---|
committer | John Langford <jl@hunch.net> | 2009-08-01 00:19:24 +0400 |
commit | f30c74f7d36a3b3691d1e9a598163dcecb87b493 (patch) | |
tree | d531146bcef68de85930e6e65c31ad4c868d3bcd /v_array.h | |
parent | 8b4959c461235e733cef9ccb851c3834c3155c31 (diff) |
Initial release of version 3.10. I've incorporated some of the
Gordon's changes.
Diffstat (limited to 'v_array.h')
-rw-r--r-- | v_array.h | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/v_array.h b/v_array.h new file mode 100644 index 00000000..5e6962c2 --- /dev/null +++ b/v_array.h @@ -0,0 +1,83 @@ +/* +Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights +embodied in the content of this file are licensed under the BSD +(revised) open source license + */ + +#include <stdlib.h> + +/* +Copyright (c) 2004 John Langford. All rights reserved. The +copyrights embodied in the content of this file are licensed under the +BSD (revised) open source license. +*/ + +#ifndef STACK_H__ +#define STACK_H__ + +template<class T> class v_array{ + public: + T* begin; + T* end; + T* end_array; + + T last() { return *(end-1);} + T pop() { return *(--end);} + bool empty() { return begin == end;} + void decr() { end--;} + v_array() { begin= NULL; end = NULL; end_array=NULL;} + T& operator[](unsigned int i) { return begin[i]; } + unsigned int index(){return end-begin;} + void erase() { end = begin;} +}; + + +template<class T> inline void push(v_array<T>& v, const T &new_ele) +{ + if(v.end == v.end_array) + { + size_t old_length = v.end_array - v.begin; + size_t new_length = 2 * old_length + 3; + // size_t new_length = old_length + 1; + v.begin = (T *)realloc(v.begin,sizeof(T) * new_length); + v.end = v.begin + old_length; + v.end_array = v.begin + new_length; + } + *(v.end++) = new_ele; +} + + +template<class T> void push_many(v_array<T>& v, const T* begin, size_t num) +{ + if(v.end+num >= v.end_array) + { + size_t length = v.end - v.begin; + size_t new_length = max(2 * (size_t)(v.end_array - v.begin) + 3, + v.end - v.begin + num); + v.begin = (T *)realloc(v.begin,sizeof(T) * new_length); + v.end = v.begin + length; + v.end_array = v.begin + new_length; + } + memcpy(v.end, begin, num * sizeof(T)); + v.end += num; +} + +#include<iostream> +using namespace std; + +template<class T> void reserve(v_array<T>& v, size_t length) +{ + v.begin = (T *)realloc(v.begin, sizeof(T) * length); + v.end = v.begin; + v.end_array = v.begin + length; +} + +template<class T> v_array<T> pop(v_array<v_array<T> > &stack) +{ + if (stack.end != stack.begin) + return *(--stack.end); + else + return v_array<T>(); +} + +#endif // STACK_H__ |