Page MenuHomec4science

grid_base.hh
No OneTemporary

File Metadata

Created
Tue, May 14, 07:48

grid_base.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_BASE_HH__
#define __GRID_BASE_HH__
/* -------------------------------------------------------------------------- */
#include "tamaas.hh"
#include "array.hh"
#include <cstddef>
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
template <typename T>
class GridBase {
public:
/// Constructor by default
GridBase():data(), nb_components(1) {}
/// Destructor
virtual ~GridBase() {}
/* -------------------------------------------------------------------------- */
/* Iterator class */
/* -------------------------------------------------------------------------- */
struct iterator {
/// constructor
iterator(T * start, ptrdiff_t offset):ptr(start), offset(offset) {}
/// destructor
~iterator() {}
/// pre-increment
iterator & operator++() { ptr += offset; return *this; }
/// comparison
bool operator<(const iterator & a) { return ptr < a.ptr; }
/// increment with given offset
iterator & operator+=(ptrdiff_t a) { ptr += a*offset; return *this;}
/// dereference iterator
T * operator*() { return ptr; }
/// needed for OpenMP range calculations
Int operator-(const iterator & a) const { return (ptr - a.ptr)/offset; }
private:
T * ptr;
ptrdiff_t offset;
};
public:
/// Begin iterator with n components
iterator begin(UInt n = 1) { return iterator(data.getPtr(), n); }
/// End iterator with n components
iterator end(UInt n = 1) { return iterator(data.getPtr()+computeSize(), n); }
public:
/// Compute size
virtual UInt computeSize() const = 0;
/// Get internal data pointer (const)
const T * getInternalData() const {return this->data.getPtr();}
/// Get internal data pointer (non-const)
T * getInternalData() {return this->data.getPtr();}
/// Get number of components
UInt getNbComponents() const {return nb_components;}
/// Set number of components
void setNbComponents(UInt n) {nb_components = n;}
protected:
Array<T> data;
UInt nb_components;
};
__END_TAMAAS__
#endif // __GRID_BASE_HH__

Event Timeline