Page MenuHomec4science

array.hh
No OneTemporary

File Metadata

Created
Thu, May 9, 09:10

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 "tamaas.hh"
#include <utility>
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
template <typename T>
class Array {
public:
/// Trivial constructor
Array() {}
/// Empty array of given size
Array(UInt size):
Array() {
this->resize(size);
}
/// Copy constructor (deep)
Array(const Array & v):
Array() {
this->resize(v._size);
std::copy(v._data, v._data+v._size, this->_data);
}
/// Move constructor (transfers data ownership)
Array(Array&& v):
Array() {
_data = v._data; v._data = nullptr;
_size = v._size; v._size = 0;
wrapped = v.wrapped; v.wrapped = false;
}
/// Wrap array on data
Array(T * data, UInt size):
Array() {
this->wrapMemory(data, size);
}
/// Destructor
virtual ~Array(){
if (wrapped == false){
delete [] _data;
}
}
public:
/// Copy operator
Array & operator=(const Array & v){
this->wrapped = false;
if (v.size() != this->size()) this->resize(v.size());
#pragma omp parallel for
for (UInt i = 0 ; i < this->_size ; ++i) {
_data[i] = v._data[i];
}
return *this;
}
/// Move operator
Array & operator=(Array && v) {
if (this != &v) {
if (!wrapped) delete[] _data;
_data = v._data;
_size = v._size;
wrapped = v.wrapped;
v._data = nullptr;
v._size = 0;
v.wrapped = false;
}
return *this;
}
/// Wrap a memory pointer
void wrapMemory(T * data, UInt size) {
this->_data = data;
this->_size = size;
this->wrapped = true;
}
/// Assign a sigle value to the array
void assign(UInt size, const T & value){
this->resize(size);
#pragma omp parallel for
for (UInt i = 0; i < size; i++) {
_data[i] = value;
}
}
void setWrapped(bool w) { wrapped = w; }
/// Data pointer access (const)
const T * getPtr() const{ return _data; }
/// Data pointer access (non-const)
T * getPtr() { return _data; }
/// Data pointer setter
void setPtr(T * new_ptr) { _data = new_ptr;}
/// Resize array
void resize(UInt size){
if (wrapped == true)
TAMAAS_EXCEPTION("cannot resize wrapped surface");
if (size == 0) {
delete [] this->_data;
this->_data = nullptr;
this->_size = 0;
return;
}
if (size == this->_size) return;
delete [] _data;
this->_data = new T[size];
this->_size = size;
}
/// Access operator
inline T & operator[] (UInt i) {
TAMAAS_ASSERT(i < _size, "memory overflow");
return _data[i];
}
/// Access operator (const)
inline const T & operator[] (UInt i) const {
TAMAAS_ASSERT(i < _size, "memory overflow");
return _data[i];
}
/// Get size of array
inline UInt size() const {return _size;}
private:
T * _data = nullptr;
UInt _size = 0;
bool wrapped = false;
};
__END_TAMAAS__
/* -------------------------------------------------------------------------- */
#endif /* __ARRAY__HH__ */

Event Timeline