diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..b8242dc --- /dev/null +++ b/.clang-format @@ -0,0 +1,13 @@ +--- +Language: Cpp +BasedOnStyle: LLVM +Standard: Cpp11 +IndentWidth: 2 +TabWidth: 4 +UseTab: Never +ColumnLimit: 80 +AccessModifierOffset: -2 +PointerAlignment: Middle +NamespaceIndentation: Inner +ContinuationIndentWidth: 4 +... diff --git a/src/core/array.hh b/src/core/array.hh index b1fc100..e7327e1 100644 --- a/src/core/array.hh +++ b/src/core/array.hh @@ -1,173 +1,162 @@ /** * @file * * @author Guillaume Anciaux * * @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 . * */ /* -------------------------------------------------------------------------- */ #ifndef __ARRAY__HH__ #define __ARRAY__HH__ /* -------------------------------------------------------------------------- */ #include "tamaas.hh" -#include #include +#include /* -------------------------------------------------------------------------- */ __BEGIN_TAMAAS__ /** * @brief Generic storage class with wrapping capacities */ -template -class Array { +template class Array { public: /// Default - Array(): - _data(nullptr), - _size(0), - wrapped(false), - _alloc(DEFAULT_ALLOCATOR()) - {} + Array() = default; /// Empty array of given size - Array(UInt size): - Array(DEFAULT_ALLOCATOR()) { - this->resize(size); - } + Array(UInt size) : Array() { this->resize(size); } /// Copy constructor (deep) - Array(const Array & v): - Array() { + Array(const Array & v) : Array() { this->resize(v._size); - std::copy(v._data, v._data+v._size, this->_data); + std::copy(v._data, v._data + v._size, this->_data); } /// Move constructor (transfers data ownership) - Array(Array&& v): - Array() { + Array(Array && v) : Array() { _data = std::exchange(v._data, nullptr); _size = std::exchange(v._size, 0); wrapped = std::exchange(v.wrapped, false); } /// Wrap array on data - Array(T * data, UInt size): - Array(DEFAULT_ALLOCATOR()) { - this->wrapMemory(data, size); - } + Array(T * data, UInt size) : Array() { this->wrapMemory(data, size); } /// Destructor - virtual ~Array(){ + virtual ~Array() { if (wrapped == false) _alloc.deallocate(_data, _size); } public: /// Copy operator - Array & operator=(const Array & v){ + Array & operator=(const Array & v) { this->wrapped = false; - if (v.size() != this->size()) this->resize(v.size()); + if (v.size() != this->size()) + this->resize(v.size()); #pragma omp parallel for - for (UInt i = 0 ; i < this->_size ; ++i) { + 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) _alloc.deallocate(_data, _size); + if (!wrapped) + _alloc.deallocate(_data, _size); _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){ + 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 * data() const{ return _data; } + const T * data() const { return _data; } /// Data pointer access (non-const) T * data() { return _data; } /// Data pointer setter - void setData(T * new_ptr) { _data = new_ptr;} + void setData(T * new_ptr) { _data = new_ptr; } /// Resize array - void resize(UInt size){ + void resize(UInt size) { if (wrapped == true) TAMAAS_EXCEPTION("cannot resize wrapped array"); // Erase array if (size == 0) { _alloc.deallocate(_data, _size); this->_data = nullptr; this->_size = 0; return; } // Do nothing - if (size == this->_size) return; + if (size == this->_size) + return; // Allocate new data _alloc.deallocate(_data, _size); this->_data = _alloc.allocate(size); this->_size = size; } /// Access operator - inline T & operator[] (UInt i) { + inline T & operator[](UInt i) { TAMAAS_ASSERT(i < _size, "memory overflow"); return _data[i]; } /// Access operator (const) - inline const T & operator[] (UInt i) 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;} + inline UInt size() const { return _size; } private: T * _data = nullptr; UInt _size = 0; bool wrapped = false; - DEFAULT_ALLOCATOR _alloc; + DEFAULT_ALLOCATOR _alloc = DEFAULT_ALLOCATOR(); }; __END_TAMAAS__ /* -------------------------------------------------------------------------- */ #endif /* __ARRAY__HH__ */