Loading...
Searching...
No Matches
utility.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-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-PDELab-exception
#ifndef DUNE_TYPETREE_UTILITY_HH
#define DUNE_TYPETREE_UTILITY_HH
#include <memory>
#include <tuple>
#include <type_traits>
#include <utility>
#include <algorithm>
#include <dune/common/shared_ptr.hh>
#include <dune/common/indices.hh>
#include <dune/typetree/nodeinterface.hh>
#include <dune/typetree/nodetags.hh>
namespace Dune {
namespace TypeTree {
#ifndef DOXYGEN
template<typename T>
std::shared_ptr<T> convert_arg(const T& t)
{
return std::make_shared<T>(t);
}
template<typename T>
std::shared_ptr<T> convert_arg(T& t)
{
return stackobject_to_shared_ptr(t);
}
template<typename BaseType, typename T>
T& assertGridViewType(T& t)
{
static_assert((std::is_same<typename BaseType::Traits::GridViewType,
typename T::Traits::GridViewType>::value),
"GridViewType must be equal in all components of composite type");
return t;
}
// only bind to real rvalues
template<typename T>
typename std::enable_if<!std::is_lvalue_reference<T>::value,std::shared_ptr<T> >::type convert_arg(T&& t)
{
return std::make_shared<T>(std::forward<T>(t));
}
namespace Experimental {
template<class BinaryOp, class Arg>
constexpr decltype(auto)
left_fold(const BinaryOp& binary_op, Arg&& arg)
{
return std::forward<Arg>(arg);
}
template<class BinaryOp, class Init, class Arg0, class... Args>
constexpr decltype(auto)
left_fold(const BinaryOp& binary_op, Init&& init, Arg0&& arg_0, Args&&... args)
{
return left_fold(
binary_op,
binary_op(std::forward<Init>(init), std::forward<Arg0>(arg_0)),
std::forward<Args>(args)...);
}
} // namespace Experimental
#endif // DOXYGEN
template<typename Tree, typename Tag = StartTag>
struct TreeInfo
{
private:
// Start the tree traversal
typedef TreeInfo<Tree,NodeTag<Tree>> NodeInfo;
public:
static const std::size_t depth = NodeInfo::depth;
static const std::size_t nodeCount = NodeInfo::nodeCount;
static const std::size_t leafCount = NodeInfo::leafCount;
};
#ifndef DOXYGEN
// ********************************************************************************
// TreeInfo specializations for the different node types
// ********************************************************************************
// leaf node
template<typename Node>
struct TreeInfo<Node,LeafNodeTag>
{
static const std::size_t depth = 1;
static const std::size_t nodeCount = 1;
static const std::size_t leafCount = 1;
};
// power node - exploit the fact that all children are identical
template<typename Node>
struct TreeInfo<Node,PowerNodeTag>
{
typedef TreeInfo<typename Node::ChildType,NodeTag<typename Node::ChildType>> ChildInfo;
static const std::size_t depth = 1 + ChildInfo::depth;
static const std::size_t nodeCount = 1 + StaticDegree<Node>::value * ChildInfo::nodeCount;
static const std::size_t leafCount = StaticDegree<Node>::value * ChildInfo::leafCount;
};
namespace {
// TMP for iterating over the children of a composite node
// identical for both composite node implementations
template<typename Node, std::size_t k, std::size_t n>
struct generic_compositenode_children_info
{
typedef generic_compositenode_children_info<Node,k+1,n> NextChild;
// extract child info
typedef typename Node::template Child<k>::Type Child;
typedef NodeTag<Child> ChildTag;
typedef TreeInfo<Child,ChildTag> ChildInfo;
// combine information of current child with info about following children
static const std::size_t maxDepth = ChildInfo::depth > NextChild::maxDepth ? ChildInfo::depth : NextChild::maxDepth;
static const std::size_t nodeCount = ChildInfo::nodeCount + NextChild::nodeCount;
static const std::size_t leafCount = ChildInfo::leafCount + NextChild::leafCount;
};
// End of recursion
template<typename Node, std::size_t n>
struct generic_compositenode_children_info<Node,n,n>
{
static const std::size_t maxDepth = 0;
static const std::size_t nodeCount = 0;
static const std::size_t leafCount = 0;
};
} // anonymous namespace
// Struct for building information about composite node
template<typename Node>
struct GenericCompositeNodeInfo
{
typedef generic_compositenode_children_info<Node,0,StaticDegree<Node>::value> Children;
static const std::size_t depth = 1 + Children::maxDepth;
static const std::size_t nodeCount = 1 + Children::nodeCount;
static const std::size_t leafCount = Children::leafCount;
};
// CompositeNode: delegate to GenericCompositeNodeInfo
template<typename Node>
struct TreeInfo<Node,CompositeNodeTag>
: public GenericCompositeNodeInfo<Node>
{};
#endif // DOXYGEN
using Dune::index_constant;
namespace Indices = Dune::Indices;
} // namespace TypeTree
} //namespace Dune
#endif // DUNE_TYPETREE_UTILITY_HH
nodetags.hh
nodeinterface.hh
Dune
Definition accumulate_static.hh:17
Dune::TypeTree
Definition accumulate_static.hh:18
Dune::TypeTree::Experimental
Definition accumulate_static.hh:576
Dune::TypeTree::Experimental::Info::leafCount
auto leafCount(const Tree &tree)
The number of leaf nodes in the Tree.
Definition visitor.hh:532
Dune::TypeTree::Experimental::Info::nodeCount
auto nodeCount(const Tree &tree)
The total number of nodes in the Tree.
Definition visitor.hh:524
Dune::TypeTree::Experimental::Info::depth
auto depth(const Tree &tree)
The depth of the TypeTree.
Definition visitor.hh:508
Dune::TypeTree::LeafNodeTag
Tag designating a leaf node.
Definition nodetags.hh:18
Dune::TypeTree::CompositeNodeTag
Tag designating a composite node.
Definition nodetags.hh:27
Dune::TypeTree::TreeInfo
Struct for obtaining some basic structural information about a TypeTree.
Definition utility.hh:121
Dune::TypeTree::TreeInfo< Tree, NodeTag< Tree > >::leafCount
static const std::size_t leafCount
Definition utility.hh:136
Dune::TypeTree::TreeInfo< Tree, NodeTag< Tree > >::depth
static const std::size_t depth
Definition utility.hh:130
Dune::TypeTree::TreeInfo< Tree, NodeTag< Tree > >::nodeCount
static const std::size_t nodeCount
Definition utility.hh:133