Cycles: Expose array's capacity via getter function

This way it's possible to query capacity of an array, which then
could be used for some smart re-allocation and reserve policies.
This commit is contained in:
Sergey Sharybin
2016-02-22 16:40:32 +01:00
parent 63d017be90
commit 7fd71338f9

View File

@@ -93,23 +93,22 @@ class array
{ {
public: public:
array() array()
{ : data_(NULL),
data = NULL; datasize_(0),
datasize = 0; capacity_(0)
capacity = 0; {}
}
array(size_t newsize) array(size_t newsize)
{ {
if(newsize == 0) { if(newsize == 0) {
data = NULL; data_ = NULL;
datasize = 0; datasize_ = 0;
capacity = 0; capacity_ = 0;
} }
else { else {
data = mem_allocate(newsize); data_ = mem_allocate(newsize);
datasize = newsize; datasize_ = newsize;
capacity = datasize; capacity_ = datasize_;
} }
} }
@@ -121,15 +120,15 @@ public:
array& operator=(const array& from) array& operator=(const array& from)
{ {
if(from.datasize == 0) { if(from.datasize == 0) {
data = NULL; data_ = NULL;
datasize = 0; datasize_ = 0;
capacity = 0; capacity_ = 0;
} }
else { else {
data = mem_allocate(from.datasize); data_ = mem_allocate(from.datasize);
memcpy(data, from.data, from.datasize*sizeof(T)); memcpy(data_, from.data, from.datasize*sizeof(T));
datasize = from.datasize; datasize_ = from.datasize_;
capacity = datasize; capacity_ = datasize_;
} }
return *this; return *this;
@@ -137,13 +136,13 @@ public:
array& operator=(const vector<T>& from) array& operator=(const vector<T>& from)
{ {
datasize = from.size(); datasize_ = from.size();
capacity = datasize; capacity_ = datasize_;
data = NULL; data_ = NULL;
if(datasize > 0) { if(datasize_ > 0) {
data = mem_allocate(datasize); data_ = mem_allocate(datasize_);
memcpy(data, &from[0], datasize*sizeof(T)); memcpy(data_, &from[0], datasize_*sizeof(T));
} }
return *this; return *this;
@@ -151,7 +150,7 @@ public:
~array() ~array()
{ {
mem_free(data, capacity); mem_free(data_, capacity_);
} }
T* resize(size_t newsize) T* resize(size_t newsize)
@@ -159,59 +158,63 @@ public:
if(newsize == 0) { if(newsize == 0) {
clear(); clear();
} }
else if(newsize != datasize) { else if(newsize != datasize_) {
if(newsize > capacity) { if(newsize > capacity_) {
T *newdata = mem_allocate(newsize); T *newdata = mem_allocate(newsize);
if(newdata == NULL) { if(newdata == NULL) {
/* Allocation failed, likely out of memory. */ /* Allocation failed, likely out of memory. */
clear(); clear();
return NULL; return NULL;
} }
else if(data != NULL) { else if(data_ != NULL) {
memcpy(newdata, data, ((datasize < newsize)? datasize: newsize)*sizeof(T)); memcpy(newdata, data_, ((datasize_ < newsize)? datasize_: newsize)*sizeof(T));
mem_free(data, capacity); mem_free(data_, capacity_);
} }
data = newdata; data_ = newdata;
capacity = newsize; capacity_ = newsize;
} }
datasize = newsize; datasize_ = newsize;
} }
return data; return data_;
} }
void clear() void clear()
{ {
if(data != NULL) { if(data_ != NULL) {
mem_free(data, capacity); mem_free(data_, capacity_);
data = NULL; data_ = NULL;
} }
datasize = 0; datasize_ = 0;
capacity = 0; capacity_ = 0;
} }
size_t size() const size_t size() const
{ {
return datasize; return datasize_;
} }
T& operator[](size_t i) const T& operator[](size_t i) const
{ {
assert(i < datasize); assert(i < datasize_);
return data[i]; return data_[i];
} }
void reserve(size_t newcapacity) { void reserve(size_t newcapacity) {
if(newcapacity > capacity) { if(newcapacity > capacity_) {
T *newdata = mem_allocate(newcapacity); T *newdata = mem_allocate(newcapacity);
if(data) { if(data_ != NULL) {
memcpy(newdata, data, ((datasize < newcapacity)? datasize: newcapacity)*sizeof(T)); memcpy(newdata, data_, ((datasize_ < newcapacity)? datasize_: newcapacity)*sizeof(T));
mem_free(data, capacity); mem_free(data_, capacity_);
} }
data = newdata; data_ = newdata;
capacity = newcapacity; capacity_ = newcapacity;
} }
} }
size_t capacity() const {
return capacity_;
}
protected: protected:
inline T* mem_allocate(size_t N) inline T* mem_allocate(size_t N)
{ {
@@ -230,9 +233,9 @@ protected:
} }
} }
T *data; T *data_;
size_t datasize; size_t datasize_;
size_t capacity; size_t capacity_;
}; };
CCL_NAMESPACE_END CCL_NAMESPACE_END