Cycles: Implement reseve() for aligned array class

The title says it all actually, just support reserving memory in the array class.
This commit is contained in:
Sergey Sharybin 2015-08-23 15:50:31 +02:00
parent 2230130099
commit 7be6dba091

@ -105,6 +105,7 @@ public:
{ {
data = NULL; data = NULL;
datasize = 0; datasize = 0;
capacity = 0;
} }
array(size_t newsize) array(size_t newsize)
@ -112,10 +113,12 @@ public:
if(newsize == 0) { if(newsize == 0) {
data = NULL; data = NULL;
datasize = 0; datasize = 0;
capacity = 0;
} }
else { else {
data = (T*)util_aligned_malloc(sizeof(T)*newsize, alignment); data = (T*)util_aligned_malloc(sizeof(T)*newsize, alignment);
datasize = newsize; datasize = newsize;
capacity = datasize;
} }
} }
@ -129,11 +132,13 @@ public:
if(from.datasize == 0) { if(from.datasize == 0) {
data = NULL; data = NULL;
datasize = 0; datasize = 0;
capacity = 0;
} }
else { else {
data = (T*)util_aligned_malloc(sizeof(T)*from.datasize, alignment); data = (T*)util_aligned_malloc(sizeof(T)*from.datasize, alignment);
memcpy(data, from.data, from.datasize*sizeof(T)); memcpy(data, from.data, from.datasize*sizeof(T));
datasize = from.datasize; datasize = from.datasize;
capacity = datasize;
} }
return *this; return *this;
@ -142,6 +147,7 @@ public:
array& operator=(const vector<T>& from) array& operator=(const vector<T>& from)
{ {
datasize = from.size(); datasize = from.size();
capacity = datasize;
data = NULL; data = NULL;
if(datasize > 0) { if(datasize > 0) {
@ -163,13 +169,15 @@ public:
clear(); clear();
} }
else if(newsize != datasize) { else if(newsize != datasize) {
if(newsize > capacity) {
T *newdata = (T*)util_aligned_malloc(sizeof(T)*newsize, alignment); T *newdata = (T*)util_aligned_malloc(sizeof(T)*newsize, alignment);
if(data) { if(data) {
memcpy(newdata, data, ((datasize < newsize)? datasize: newsize)*sizeof(T)); memcpy(newdata, data, ((datasize < newsize)? datasize: newsize)*sizeof(T));
util_aligned_free(data); util_aligned_free(data);
} }
data = newdata; data = newdata;
capacity = newsize;
}
datasize = newsize; datasize = newsize;
} }
} }
@ -179,6 +187,7 @@ public:
util_aligned_free(data); util_aligned_free(data);
data = NULL; data = NULL;
datasize = 0; datasize = 0;
capacity = 0;
} }
size_t size() const size_t size() const
@ -192,9 +201,22 @@ public:
return data[i]; return data[i];
} }
void reserve(size_t newcapacity) {
if(newcapacity > capacity) {
T *newdata = (T*)util_aligned_malloc(sizeof(T)*newcapacity, alignment);
if(data) {
memcpy(newdata, data, ((datasize < newcapacity)? datasize: newcapacity)*sizeof(T));
util_aligned_free(data);
}
data = newdata;
capacity = newcapacity;
}
}
protected: protected:
T *data; T *data;
size_t datasize; size_t datasize;
size_t capacity;
}; };
CCL_NAMESPACE_END CCL_NAMESPACE_END