Page MenuHomec4science

array.hh
No OneTemporary

File Metadata

Created
Tue, Nov 5, 03:35

array.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 __ARRAY__HH__
#define __ARRAY__HH__
/* -------------------------------------------------------------------------- */
#include <cstring>
#include "tamaas.hh"
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
template <typename T>
class Array {
public:
/// Copy constructor (deep)
Array(const Array & v):
_data(NULL),
_size(0),
wrapped(false) {
this->resize(v._size);
memcpy(this->_data,v.getPtr(),this->_size*sizeof(T));
}
Array():
_data(NULL),
_size(0),
wrapped(false) {}
Array(UInt size):
_data(NULL),
_size(0),
wrapped(false) {
this->resize(size);
this->wrapped = false;
}
Array(T * data, UInt size):
_data(NULL),
_size(0),
wrapped(false) {
this->wrapMemory(data,size);
}
~Array(){
if (wrapped == false && _data != NULL){
delete [] _data;
}
}
Array & operator=(const Array & v){
this->wrapped = false;
if (v.size() != this->size()) this->resize(v.size());
memcpy(this->_data,v.getPtr(),this->_size*sizeof(T));
return *this;
}
void wrapMemory(T * data, UInt size) {
this->_data = data;
this->_size = size;
this->wrapped = true;
}
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 setPtr(T * new_ptr) { _data = new_ptr;}
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){
TAMAAS_ASSERT(i < _size, "memory overflow");
return _data[i];
}
inline const T & operator[](UInt i)const{
TAMAAS_ASSERT(i < _size, "memory overflow");
return _data[i];
}
inline UInt size() const{return _size;}
private:
T * _data;
UInt _size;
bool wrapped;
};
__END_TAMAAS__
/* -------------------------------------------------------------------------- */
#endif /* __ARRAY__HH__ */

Event Timeline