Page MenuHomec4science

grid_base.hh
No OneTemporary

File Metadata

Created
Sun, May 12, 07:19

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__
/// Namespace for view index classes
namespace view {
/// Index class
struct index {
index(Int i = -1):i(i){}
Int i;
};
/// Blocked index class
struct blocked : public index {
blocked(Int i):index(i){}
};
/// Free index class
struct free : public index {
free():index(-1){}
};
}
template <typename T>
class GridBase {
public:
/// Constructor by default
GridBase() = default;
/// Copy constructor
GridBase(const GridBase & o): data(o.data),
nb_components(o.nb_components),
offset(o.offset) {}
/// Move constructor (transfers data ownership)
GridBase(GridBase&& o): data(std::move(o.data)),
nb_components(o.nb_components),
offset(o.offset) {}
/// Destructor
virtual ~GridBase() = default;
/* -------------------------------------------------------------------------- */
/* Iterator class */
/* -------------------------------------------------------------------------- */
protected:
struct iterator {
/// constructor
iterator(T * start, ptrdiff_t offset):ptr(start), offset(offset) {}
/// destructor
~iterator() {}
/// pre-increment
inline iterator & operator++() { ptr += offset; return *this; }
/// comparison
inline bool operator<(const iterator & a) { return ptr < a.ptr; }
/// inequality
inline bool operator!=(const iterator & a) { return ptr != a.ptr; }
/// increment with given offset
inline iterator & operator+=(ptrdiff_t a) { ptr += a*offset; return *this;}
/// dereference iterator
inline T & operator*() { return *ptr; }
/// needed for OpenMP range calculations
inline Int operator-(const iterator & a) const { return (ptr - a.ptr)/offset; }
protected:
T * ptr;
ptrdiff_t offset;
};
public:
/// Begin iterator with n components
virtual iterator begin(UInt n = 1) { return iterator(data.getPtr(), n); }
/// End iterator with n components
virtual 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;}
/// Get offset
UInt getOffset() const {return offset;}
public:
template <typename T1>
void copy(const GridBase<T1> & other) {
data = other.data;
nb_components = other.nb_components;
offset = other.offset;
}
template <typename T1>
void move(GridBase<T1> && other) {
data = std::move(other.data);
nb_components = other.nb_components;
offset = other.offset;
other.nb_components = 0;
other.offset = 0;
}
protected:
Array<T> data;
UInt nb_components = 1;
UInt offset = 0;
};
__END_TAMAAS__
#endif // __GRID_BASE_HH__

Event Timeline