update localvector

This commit is contained in:
wyy 2014-06-05 18:30:08 +08:00
parent 189b2725a0
commit a8f83dd6f0

View File

@ -9,6 +9,10 @@
namespace Limonp namespace Limonp
{ {
using namespace std; using namespace std;
/*
* LocalVector<T> : T must be primitive type (char , int, size_t), if T is struct or class, LocalVector<T> may be dangerous..
* LocalVector<T> is simple and not well-tested.
*/
const size_t LOCAL_VECTOR_BUFFER_SIZE = 16; const size_t LOCAL_VECTOR_BUFFER_SIZE = 16;
template <class T> template <class T>
class LocalVector class LocalVector
@ -92,13 +96,11 @@ namespace Limonp
} }
void push_back(const T& t) void push_back(const T& t)
{ {
if(!full()) if(_size == _capacity)
{ {
_ptr[_size++] = t;
return ;
}
assert(_capacity); assert(_capacity);
reserve(_capacity * 2); reserve(_capacity * 2);
}
_ptr[_size ++ ] = t; _ptr[_size ++ ] = t;
} }
void reserve(size_t size) void reserve(size_t size)
@ -118,10 +120,6 @@ namespace Limonp
free(old); free(old);
} }
} }
bool full() const
{
return size() == capacity();
}
bool empty() const bool empty() const
{ {
return 0 == size(); return 0 == size();