Page MenuHomec4science

grid_view.hh
No OneTemporary

File Metadata

Created
Tue, Jul 30, 22:30

grid_view.hh

/**
* @file
*
* @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_VIEW_HH__
#define __GRID_VIEW_HH__
/* -------------------------------------------------------------------------- */
#include "grid.hh"
#include "tamaas.hh"
#include <vector>
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
/* -------------------------------------------------------------------------- */
/**
* @brief View type on grid
* This is a view on a *contiguous* chunk of data defined by a grid
*/
template <template <typename, UInt> class Base, typename T, UInt base_dim,
UInt dim>
class GridView : public Base<T, dim> {
public:
/// Constructor
GridView(GridBase<T>& grid_base, const std::vector<UInt>& multi_index);
/// Move constructor
GridView(GridView&& o)
: Base<T, dim>(std::forward<Base<T, dim>>(o)),
grid(std::exchange(o.grid, nullptr)), indexes(std::move(o.indexes)) {}
/// Destructor
virtual ~GridView() = default;
protected:
Base<T, base_dim>* grid;
std::vector<UInt> indexes;
};
/* -------------------------------------------------------------------------- */
template <template <typename, UInt> class Base, typename T, UInt base_dim,
typename... Args>
GridView<Base, T, base_dim, base_dim - sizeof...(Args)>
make_view(Base<T, base_dim>& base, Args... indices) {
std::vector<UInt> multi_index = {static_cast<UInt>(indices)...};
return GridView<Base, T, base_dim, base_dim - sizeof...(Args)>(base,
multi_index);
}
/* -------------------------------------------------------------------------- */
/* Template implementation */
/* -------------------------------------------------------------------------- */
template <template <typename, UInt> class Base, typename T, UInt base_dim,
UInt dim>
GridView<Base, T, base_dim, dim>::GridView(GridBase<T>& grid_base,
const std::vector<UInt>& multi_index)
: Base<T, dim>(), grid(nullptr), indexes(multi_index) {
static_assert(base_dim >= dim,
"view dimension must be >= than the base class");
// Checking view type
grid = dynamic_cast<Base<T, base_dim>*>(&grid_base);
if (!grid)
TAMAAS_EXCEPTION("given base type is incompatible with view");
constexpr UInt dim_offset = base_dim - dim;
if (dim_offset != multi_index.size())
TAMAAS_EXCEPTION("Number of blocked indices does not match view dimension");
std::copy(grid->sizes().begin() + dim_offset, grid->sizes().end(),
this->n.begin());
std::copy(grid->getStrides().begin() + dim_offset, grid->getStrides().end(),
this->strides.begin());
auto offset =
std::inner_product(multi_index.begin(), multi_index.end(),
grid->getStrides().begin(), 0);
this->nb_components = grid->getNbComponents();
this->data.wrapMemory(grid->getInternalData() + offset, this->computeSize());
}
/* -------------------------------------------------------------------------- */
__END_TAMAAS__
#endif // __GRID_VIEW_HH__

Event Timeline