Page MenuHomec4science

grid_tmpl.hh
No OneTemporary

File Metadata

Created
Thu, May 16, 04:54

grid_tmpl.hh

/**
*
* @author Lucas Frérot <lucas.frerot@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 __GRID_TMPL_HH__
#define __GRID_TMPL_HH__
/* -------------------------------------------------------------------------- */
#include "tamaas.hh"
#include "grid.hh"
__BEGIN_TAMAAS__
template <typename T, UInt dim>
template <typename T1>
void Grid<T, dim>::init(const T1 & n, UInt nb_components) {
std::copy(n.begin(), n.end(), this->n.begin());
this->nb_components = nb_components;
this->resize(this->n);
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
inline UInt Grid<T, dim>::computeSize() const {
UInt size = 1;
for (UInt i = 0 ; i < dim ; i++) size *= n[i];
size *= this->nb_components;
return size;
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
template <typename... T1>
inline UInt Grid<T, dim>::unpackOffset(UInt offset, UInt index_pos,
UInt index, T1... rest) const {
offset += index * strides[index_pos];
return unpackOffset(offset, index_pos+1, rest...); // tail-rec bb
}
template <typename T, UInt dim>
template <typename... T1>
inline UInt Grid<T, dim>::unpackOffset(UInt offset, UInt index_pos,
UInt index) const {
return offset + index * strides[index_pos];
}
template <typename T, UInt dim>
template <typename... T1>
inline T & Grid<T, dim>::operator()(T1... args) {
/// Checking access integrity
constexpr UInt nargs = sizeof...(T1);
static_assert(nargs == dim + 1 ||
nargs == 1 ||
nargs == dim,
"number of arguments in operator() does not match dimension");
constexpr UInt start = (nargs == 1) ? dim : 0;
UInt offset = unpackOffset(this->offset, start, args...);
return this->data[offset];
}
template <typename T, UInt dim>
template <typename... T1>
inline const T & Grid<T, dim>::operator()(T1... args) const {
/// Checking access integrity
constexpr UInt nargs = sizeof...(T1);
static_assert(nargs == dim + 1 ||
nargs == 1 ||
nargs == dim,
"number of arguments in operator() does not match dimension");
constexpr UInt start = (nargs == 1) ? dim : 0;
UInt offset = unpackOffset(this->offset, start, args...);
return this->data[offset];
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
inline UInt Grid<T, dim>::computeOffset(std::array<UInt, dim+1> tuple) const {
UInt offset = this->offset;
for (UInt i = 0 ; i < dim+1 ; i++) {
offset += tuple[i] * strides[i];
}
return offset;
}
template <typename T, UInt dim>
inline T & Grid<T, dim>::operator()(std::array<UInt, dim+1> tuple) {
UInt offset = computeOffset(tuple);
return this->data[offset];
}
template <typename T, UInt dim>
inline const T & Grid<T, dim>::operator()(std::array<UInt, dim+1> tuple) const {
UInt offset = computeOffset(tuple);
return this->data[offset];
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
Grid<T, dim> & Grid<T, dim>::operator=(const Grid<T, dim> & other) {
this->copy(other);
return *this;
}
template <typename T, UInt dim>
Grid<T, dim> & Grid<T, dim>::operator=(Grid<T, dim> & other) {
this->copy(other);
return *this;
}
template <typename T, UInt dim>
Grid<T, dim> & Grid<T, dim>::operator=(Grid<T, dim> && other) {
this->move(std::forward<Grid<T, dim>>(other));
return *this;
}
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
template <typename T1>
void Grid<T, dim>::copy(const Grid<T1, dim> & other) {
GridBase<T>::copy(other);
this->n = other.n;
this->strides = other.strides;
}
template <typename T, UInt dim>
template <typename T1>
void Grid<T, dim>::move(Grid<T1, dim> && other) {
GridBase<T>::move(std::forward<Grid<T1, dim>>(other));
this->n = std::move(other.n);
this->strides = std::move(other.strides);
}
/* -------------------------------------------------------------------------- */
/* Stream operator */
/* -------------------------------------------------------------------------- */
template <typename T, UInt dim>
inline std::ostream & operator<<(std::ostream & stream,
const Grid<T, dim> & _this) {
_this.printself(stream);
return stream;
}
__END_TAMAAS__
/* -------------------------------------------------------------------------- */
#endif // __GRID_TMPL_HH__

Event Timeline