Loading...
Searching...
No Matches
containerfactory.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
#ifndef DUNE_FUNCTIONS_BACKENDS_CONTAINERFACTORY_HH
#define DUNE_FUNCTIONS_BACKENDS_CONTAINERFACTORY_HH
#include <type_traits>
#include <array>
#include <vector>
#include <dune/common/exceptions.hh>
#include <dune/common/fvector.hh>
#include <dune/common/indices.hh>
#include <dune/common/tuplevector.hh>
#include <dune/functions/functionspacebases/containerdescriptors.hh>
namespace Dune::Functions {
namespace ContainerDescriptors {
namespace Impl {
template<class T>
struct ContainerFactory
{
void operator() (const Unknown& descriptor, const T& defaultValue) const
{
DUNE_THROW(Dune::NotImplemented, "Cannot create a vector. The container descriptor is unknown.");
}
template<class... ChildDescriptor>
auto operator() (const Tuple<ChildDescriptor...>& descriptor, const T& defaultValue) const
{
return unpackIntegerSequence([&](auto... ii) {
return Dune::TupleVector<decltype((*this)(descriptor[ii], defaultValue))...>{(*this)(descriptor[ii], defaultValue)...};
}, std::make_index_sequence<sizeof...(ChildDescriptor)>());
}
template<class ChildDescriptor, std::size_t n>
auto operator() (const Array<ChildDescriptor,n>& descriptor, const T& defaultValue) const
{
using ChildContainer = decltype((*this)(descriptor[0], defaultValue));
return unpackIntegerSequence([&](auto... ii) {
return std::array<ChildContainer, n>{(*this)(descriptor[ii], defaultValue)...};
}, std::make_index_sequence<n>());
}
template<class ChildDescriptor>
auto operator() (const Vector<ChildDescriptor>& descriptor, const T& defaultValue) const
{
using ChildContainer = decltype((*this)(descriptor[0], defaultValue));
auto result = std::vector<ChildContainer>();
result.reserve(descriptor.size());
for (std::size_t i = 0; i < descriptor.size(); ++i)
result.emplace_back((*this)(descriptor[i], defaultValue));
return result;
}
template<class ChildDescriptor, std::size_t n>
auto operator() (const UniformArray<ChildDescriptor,n>& descriptor, const T& defaultValue) const
{
using ChildContainer = decltype((*this)(descriptor[0], defaultValue));
auto childContainer = (*this)(descriptor[0], defaultValue);
return unpackIntegerSequence([&](auto... ii) {
return std::array<ChildContainer, n>{((void)(ii),childContainer)...};
}, std::make_index_sequence<n>());
}
template<class ChildDescriptor>
auto operator() (const UniformVector<ChildDescriptor>& descriptor, const T& defaultValue) const
{
using ChildContainer = decltype((*this)(descriptor[0], defaultValue));
auto childContainer = (*this)(descriptor[0], defaultValue);
return std::vector<ChildContainer>(descriptor.size(), childContainer);
}
// scalar types
auto operator() (const Value& descriptor, const T& defaultValue) const
{
return T(defaultValue);
}
};
} // end namespace Impl
} // end namespace ContainerDescriptors
template<class T, class ContainerDescriptor>
auto makeContainer (const ContainerDescriptor& descriptor, const T& defaultValue)
{
auto factory = ContainerDescriptors::Impl::ContainerFactory<T>{};
return factory(descriptor, defaultValue);
}
template<class T, class ContainerDescriptor>
auto makeContainer (const ContainerDescriptor& descriptor)
{
return makeContainer<T>(descriptor, T());
}
} // end namespace Dune::Functions
#endif // DUNE_FUNCTIONS_BACKENDS_CONTAINERFACTORY_HH
containerdescriptors.hh
Lightweight representation of (hierarchical) size and block structure extracted from a basis to descr...
Dune::Functions
Definition monomialset.hh:19
Dune::Functions::makeContainer
auto makeContainer(const ContainerDescriptor &descriptor, const T &defaultValue)
Construct a nested random access container compatible with the container descriptor.
Definition containerfactory.hh:102
Dune::Functions::ContainerDescriptors
Definition containerfactory.hh:20
Dune::Functions::ContainerDescriptors::Tuple
Dune::TupleVector< Children... > Tuple
Descriptor with all children of possibly different type.
Definition containerdescriptors.hh:82
Dune::Functions::ContainerDescriptors::Vector
std::vector< Child > Vector
Descriptor for vectors with all children of the same type and dynamic size.
Definition containerdescriptors.hh:112
Dune::Functions::ContainerDescriptors::Array
std::array< Child, n > Array
Descriptor for arrays with all children of the same type and static size.
Definition containerdescriptors.hh:98
Dune::Functions::ContainerDescriptors::Unknown
Fallback container descriptor if nothing else fits.
Definition containerdescriptors.hh:52
Dune::Functions::ContainerDescriptors::Value
The node in the descriptor tree representing a value placeholder.
Definition containerdescriptors.hh:71
Dune::Functions::ContainerDescriptors::UniformArray
Descriptor for arrays with all children identical and the number of children a static size.
Definition containerdescriptors.hh:117
Dune::Functions::ContainerDescriptors::UniformVector
Uniform descriptor with dynamic size.
Definition containerdescriptors.hh:156