Page MenuHomec4science

my_vector.hh
No OneTemporary

File Metadata

Created
Fri, Jul 5, 04:27

my_vector.hh

#ifndef MY_VECTOR_H
#define MY_VECTOR_H
/* -------------------------------------------------------------------------- */
#include <cstring>
/* -------------------------------------------------------------------------- */
template <typename T>
class MyVector {
public:
MyVector(const MyVector & v){
this->_data = NULL;
this->_size = 0;
this->wrapped = false;
this->resize(v._size);
memcpy(this->_data,v.getPtr(),this->_size*sizeof(T));
}
MyVector & operator=(const MyVector & v){
this->wrapped = false;
if (v.size() != this->size()) this->resize(v.size());
memcpy(this->_data,v.getPtr(),this->_size*sizeof(T));
return *this;
}
MyVector(){
this->_data = NULL;
this->_size = 0;
this->wrapped = false;
}
MyVector(UInt size){
this->_size = 0;
this->resize(size);
this->wrapped = false;
}
MyVector(T * data, UInt size){
this->wrapped = false;
this->size = 0;
this->wrapMemory(data,size);
}
void wrapMemory(T * data, UInt size){
this->_data = data;
this->_size = size;
this->wrapped = true;
}
~MyVector(){
if (wrapped == false && _data != NULL){
delete [] _data;
}
}
void assign(UInt size, const T & value){
this->resize(size);
for (UInt i = 0; i < size; i++) {
_data[i] = value;
}
}
const T * getPtr() const{ return _data; }
T * getPtr() { return _data; }
void resize(UInt size){
if (size == 0 && wrapped == false) {
if (_data != NULL) {
delete [] this->_data;
this->_data = NULL;
}
this->_size = 0;
return;
}
if (size == this->_size) return;
if (wrapped == true) SURFACE_FATAL("cannot resize wrapped surface");
if (_data != NULL) delete [] _data;
this->_data = new T[size];
this->_size = size;
}
T & operator[](UInt i){
if (i >= _size) SURFACE_FATAL("overflow");
return _data[i];
}
const T & operator[](UInt i)const{
return _data[i];
}
UInt size() const{return _size;}
private:
T * _data;
UInt _size;
bool wrapped;
};
/* -------------------------------------------------------------------------- */
#endif /* MY_VECTOR_H */

Event Timeline