Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F85132056
array.hh
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Sep 27, 00:07
Size
4 KB
Mime Type
text/x-c++
Expires
Sun, Sep 29, 00:07 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
21106157
Attached To
rTAMAAS tamaas
array.hh
View Options
/**
* @file
* @section LICENSE
*
* Copyright (©) 2016-19 EPFL (École Polytechnique Fédérale de Lausanne),
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#ifndef __ARRAY__HH__
#define __ARRAY__HH__
/* -------------------------------------------------------------------------- */
#include "tamaas.hh"
#include <thrust/fill.h>
#include <thrust/copy.h>
#include <memory>
#include <utility>
/* -------------------------------------------------------------------------- */
namespace tamaas {
/**
* @brief Generic storage class with wrapping capacities
*/
template <typename T>
class Array final {
public:
/// Default
Array() = default;
/// Empty array of given size
Array(UInt size) : Array() { this->resize(size); }
/// Copy constructor (deep)
Array(const Array& v) : Array() {
this->resize(v._size);
thrust::copy(v._data, v._data + v._size, this->_data);
}
/// Move constructor (transfers data ownership)
Array(Array&& v) noexcept : 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() { this->wrapMemory(data, size); }
/// Destructor
~Array() {
if (not wrapped)
_alloc.deallocate(_data, _size);
}
public:
/// Copy operator
Array& operator=(const Array& v) {
this->wrapped = false;
if (v.size() != this->size())
this->resize(v.size());
thrust::copy(v._data, v._data + _size, _data);
return *this;
}
/// Move operator
Array& operator=(Array&& v) noexcept {
if (this != &v) {
if (not wrapped)
_alloc.deallocate(_data, _size);
_data = std::exchange(v._data, nullptr);
_size = std::exchange(v._size, 0);
wrapped = std::exchange(v.wrapped, false);
}
return *this;
}
void wrap(Array & other) {
this->wrapMemory(other._data, other._size);
}
void wrap(const Array & other) {
this->wrapMemory(other._data, other._size);
}
/// 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);
thrust::fill(_data, _data + size, value);
}
void setWrapped(bool w) { wrapped = w; }
/// Data pointer access (const)
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; }
/// Resize array
void resize(UInt size) {
if (wrapped)
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;
// Allocate new data
_alloc.deallocate(_data, _size);
this->_data = _alloc.allocate(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;
DEFAULT_ALLOCATOR(T) _alloc = DEFAULT_ALLOCATOR(T)();
};
} // namespace tamaas
/* -------------------------------------------------------------------------- */
#endif /* __ARRAY__HH__ */
Event Timeline
Log In to Comment