Page MenuHomec4science

my_vector.hh
No OneTemporary

File Metadata

Created
Mon, Nov 11, 03:18

my_vector.hh

/**
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2016 EPFL (Ecole Polytechnique Fédérale de
* Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des
* Solides)
*
* Tamaas is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Tamaas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Tamaas. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#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;
}
inline T & operator[](UInt i){
if (i >= _size) SURFACE_FATAL("overflow");
return _data[i];
}
inline const T & operator[](UInt i)const{
return _data[i];
}
inline UInt size() const{return _size;}
private:
T * _data;
UInt _size;
bool wrapped;
};
/* -------------------------------------------------------------------------- */
#endif /* MY_VECTOR_H */

Event Timeline