Page MenuHomec4science

ranges.hh
No OneTemporary

File Metadata

Created
Tue, May 14, 13:31

ranges.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 RANGES_HH
#define RANGES_HH
/* -------------------------------------------------------------------------- */
#include "iterator.hh"
#include "static_types.hh"
/* -------------------------------------------------------------------------- */
namespace tamaas {
/* -------------------------------------------------------------------------- */
/// Range class for complex iterators
template <typename LocalType, typename ValueType, UInt local_size>
class Range {
class iterator : public iterator_::iterator<ValueType> {
using parent = iterator_::iterator<ValueType>;
public:
using value_type = LocalType;
using reference = value_type;
iterator(const parent& o) : parent(o) {}
inline reference operator*() { return LocalType(this->data); }
inline reference operator*() const { return LocalType(this->data); }
};
public:
// useful type trait
template <typename Container>
inline static constexpr bool is_valid_container_v = std::is_same_v<
typename std::remove_cv_t<typename std::decay_t<Container>::value_type>,
typename std::remove_cv_t<ValueType>>;
public:
using value_type = LocalType;
using reference = value_type&&;
/// Construct from a container
template <class Container>
Range(Container&& cont) : _begin(cont.begin()), _end(cont.end()) {
if (cont.getNbComponents() != local_size)
TAMAAS_EXCEPTION(
"Number of components does not match local tensor type size");
_begin.setStep(local_size);
_end.setStep(local_size);
}
/// Construct from two iterators
Range(iterator _begin, iterator _end) : _begin(_begin), _end(_end) {}
iterator begin() { return _begin; }
iterator end() { return _end; }
Range headless() const {
iterator shift(_begin);
++shift;
return Range{shift, _end};
}
private:
iterator _begin, _end;
};
/// Make range with proxy type
template <typename LocalType, class Container>
typename std::enable_if<
is_proxy<LocalType>::value,
Range<LocalType, typename LocalType::value_type, LocalType::size>>::type
range(Container&& cont) {
using RangeClass =
Range<LocalType, typename LocalType::value_type, LocalType::size>;
static_assert(
RangeClass::template is_valid_container_v<Container>,
"Mismatch between range's value type and container's value type");
return RangeClass(std::forward<Container>(cont));
}
/// Make range with standard type
template <typename LocalType, class Container>
typename std::enable_if<not is_proxy<LocalType>::value,
Range<LocalType, LocalType, 1>>::type
range(Container&& cont) {
using RangeClass = Range<LocalType, LocalType, 1>;
static_assert(
RangeClass::template is_valid_container_v<Container>,
"Mismatch between range's value type and container's value type");
return RangeClass(std::forward<Container>(cont));
}
/* -------------------------------------------------------------------------- */
} // namespace tamaas
#endif

Event Timeline