Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F64691958
ranges.hh
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, May 28, 18:17
Size
3 KB
Mime Type
text/x-c++
Expires
Thu, May 30, 18:17 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17945212
Attached To
rTAMAAS tamaas
ranges.hh
View Options
/**
* @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>
struct is_valid_container
: std::is_same<
std::remove_cv_t<typename std::decay_t<Container>::value_type>,
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 ("
<< cont.getNbComponents() << ", expected " << local_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);
return Range{++shift, _end};
}
private:
iterator _begin, _end;
};
/// Make range with proxy type
template <typename LocalType, class Container>
std::enable_if_t<
is_proxy<LocalType>::value,
Range<LocalType, typename LocalType::value_type, LocalType::size>>
range(Container&& cont) {
using RangeClass =
Range<LocalType, typename LocalType::value_type, LocalType::size>;
static_assert(
RangeClass::template is_valid_container<Container>::value,
"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>
std::enable_if_t<not is_proxy<LocalType>::value, Range<LocalType, LocalType, 1>>
range(Container&& cont) {
using RangeClass = Range<LocalType, LocalType, 1>;
static_assert(
RangeClass::template is_valid_container<Container>::value,
"Mismatch between range's value type and container's value type");
return RangeClass(std::forward<Container>(cont));
}
/* -------------------------------------------------------------------------- */
} // namespace tamaas
#endif
Event Timeline
Log In to Comment