Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F68522772
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
Thu, Jun 27, 19:22
Size
4 KB
Mime Type
text/x-c++
Expires
Sat, Jun 29, 19:22 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
18589110
Attached To
rTAMAAS tamaas
array.hh
View Options
/*
* SPDX-License-Indentifier: AGPL-3.0-or-later
*
* Copyright (©) 2016-2022 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 "logger.hh"
#include "tamaas.hh"
#include <memory>
#include <thrust/copy.h>
#include <thrust/fill.h>
#include <utility>
/* -------------------------------------------------------------------------- */
namespace tamaas {
/// Generic storage class with wrapping capacities
template <typename T>
struct Array final {
/// 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_n(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);
_reserved = std::exchange(v._reserved, 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, _reserved);
}
/// Copy operator
Array& operator=(const Array& v) {
this->wrapped = false;
if (v.size() != this->size())
this->resize(v.size());
thrust::copy_n(v._data, _size, _data);
return *this;
}
/// Move operator
Array& operator=(Array&& v) noexcept {
if (this != &v) {
if (not wrapped)
_alloc.deallocate(_data, _reserved);
_data = std::exchange(v._data, nullptr);
_size = std::exchange(v._size, 0);
_reserved = std::exchange(v._reserved, 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) {
_data = data;
_size = size;
wrapped = true;
_reserved = 0;
}
/// 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, _reserved);
_data = nullptr;
_size = 0;
_reserved = 0;
return;
}
// Do nothing
if (size == this->_size)
return;
// Allocate new data
_alloc.deallocate(_data, _reserved);
_data = _alloc.allocate(size);
_size = size;
_reserved = size;
}
/// Reserve storage space
void reserve(UInt size) {
if (_reserved >= size)
return;
auto new_data = _alloc.allocate(size);
if (new_data != _data) {
thrust::copy_n(_data, _size, new_data);
_alloc.deallocate(_data, _reserved);
_data = new_data;
_reserved = 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;
std::size_t _reserved = 0;
bool wrapped = false;
Allocator<T> _alloc;
};
} // namespace tamaas
/* -------------------------------------------------------------------------- */
#endif /* ARRAY_HH */
Event Timeline
Log In to Comment