2
Copyright (c) by respective owners including Yahoo!, Microsoft, and
3
individual contributors. All rights reserved. Released under a BSD
4
license as described in the file LICENSE.
17
#define __INLINE inline
20
const size_t erase_point = ~ ((1 << 10) -1);
22
template<class T> class v_array{
29
T last() { return *(end-1);}
30
T pop() { return *(--end);}
31
bool empty() { return begin == end;}
33
v_array() { begin= NULL; end = NULL; end_array=NULL; erase_count = 0;}
34
T& operator[](size_t i) { return begin[i]; }
35
size_t size(){return end-begin;}
36
void resize(size_t length, bool zero_everything=false)
38
if ((size_t)(end_array-begin) != length)
40
size_t old_len = end-begin;
41
begin = (T *)realloc(begin, sizeof(T) * length);
42
if ((begin == NULL) && ((sizeof(T)*length) > 0)) {
43
std::cerr << "realloc of " << length << " failed in resize(). out of memory?" << std::endl;
44
throw std::exception();
46
if (zero_everything && (old_len < length))
47
memset(begin+old_len, 0, (length-old_len)*sizeof(T));
49
end_array = begin + length;
54
{ if (++erase_count & erase_point)
65
begin = end = end_array = NULL;
67
void push_back(const T &new_ele)
70
resize(2 * (end_array-begin) + 3);
81
inline size_t max(size_t a, size_t b)
82
{ if ( a < b) return b; else return a;
84
inline size_t min(size_t a, size_t b)
85
{ if ( a < b) return a; else return b;
88
template<class T> void copy_array(v_array<T>& dst, v_array<T> src)
91
push_many(dst, src.begin, src.size());
94
template<class T> void copy_array(v_array<T>& dst, v_array<T> src, T(*copy_item)(T))
97
for (T*item = src.begin; item != src.end; item++)
98
dst.push_back(copy_item(*item));
101
template<class T> void push_many(v_array<T>& v, const T* begin, size_t num)
103
if(v.end+num >= v.end_array)
104
v.resize(max(2 * (size_t)(v.end_array - v.begin) + 3,
105
v.end - v.begin + num));
106
memcpy(v.end, begin, num * sizeof(T));
110
template<class T> void calloc_reserve(v_array<T>& v, size_t length)
112
v.begin = (T *)calloc(length, sizeof(T));
114
v.end_array = v.begin + length;
117
template<class T> v_array<T> pop(v_array<v_array<T> > &stack)
119
if (stack.end != stack.begin)
120
return *(--stack.end);