Dune Core Modules (2.9.0)

typeindex.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_GEOMETRY_TYPEINDEX_HH
6#define DUNE_GEOMETRY_TYPEINDEX_HH
7
14#include <cstddef>
15
16#include <dune/common/indices.hh>
17#include <dune/common/hybridutilities.hh>
18
19#include "type.hh"
20
21namespace Dune
22{
25 {
32 inline static constexpr std::size_t regular_size(std::size_t dim)
33 {
34 // The following expression is derived from the expression for
35 // GlobalGeometryTypeIndex::regular_offset(). Subtracting
36 // regular_offset(dim+1)-regular_offset(dim) we get:
37 //
38 // ((1 << dim+1) >> 1) - ((1 << dim) >> 1)
39 //
40 // We always have
41 //
42 // dim >= 0,
43 //
44 // so
45 //
46 // (1 << dim+1) >= 2 and (1 << dim+2) % 2 == 0.
47 //
48 // So if we apply a single right-shift to that, we will never lose any
49 // set bits, thus
50 //
51 // ((1 << dim+1) >> 1) == (1 << dim)
52 return (1 << dim) - ((1 << dim) >> 1);
53 }
54
55 public:
61 inline static constexpr std::size_t size(std::size_t dim)
62 {
63 // one for "none"
64 return regular_size(dim) + 1;
65 }
66
73 inline static constexpr std::size_t index(const GeometryType &gt)
74 {
75 return gt.isNone() ? regular_size(gt.dim()) : (gt.id() >> 1);
76 }
77
79 inline static constexpr GeometryType type(std::size_t dim, std::size_t index) {
80 return (index == regular_size(dim)) ?
82 // the cast to unsigned makes sure this is interpreted as the topology
83 // ID constructor
84 GeometryType(static_cast< unsigned int >(index << 1), dim);
85 }
86 };
87
90 {
98 inline static constexpr std::size_t regular_offset(std::size_t dim)
99 {
100 // The number of regular geometry types in a given dimension is
101 // 2^(dim-1). For dim==0 this would yield 1/2 geometry types (which is
102 // obviously bogus, dim==0 has one regular geometry type, the point).
103 // The following expression relies on 1 >> 1 == 0 to treat dim==0
104 // specially.
105 return (1 << dim) >> 1;
106 }
107
108 public:
113 inline static constexpr std::size_t offset(std::size_t dim)
114 {
115 // dim times "none"
116 return regular_offset(dim) + dim;
117 }
118
125 inline static constexpr std::size_t size(std::size_t maxdim)
126 {
127 return offset(maxdim+1);
128 }
129
138 inline static constexpr std::size_t index(const GeometryType &gt)
139 {
140 return offset(gt.dim()) + LocalGeometryTypeIndex::index(gt);
141 }
142 };
143
144 namespace Impl {
145
146 // Map a dynamic GeometryType to a static integral_constant<GeometryType::Id, ...>
147 template<int dim, class F>
148 static auto toGeometryTypeIdConstant(const GeometryType& gt, F&& f) {
149 // Transform LocalGeometryTypeIndex to GeometryType::Id
150 auto callWithId = [&](auto index) {
151 static constexpr auto id = LocalGeometryTypeIndex::type(dim, decltype(index)::value).toId();
152 return f(std::integral_constant<GeometryType::Id, id>{});
153 };
154 // switchCases needs a fallback to determine the return type.
155 auto fallBack = [&]() { return callWithId(Dune::Indices::_0); };
156 // Iterate over all _regular_ GeometryType indices for given dimension
157 auto allIndices = std::make_index_sequence<LocalGeometryTypeIndex::size(dim)-1>{};
158 return Dune::Hybrid::switchCases(allIndices, LocalGeometryTypeIndex::index(gt), callWithId, fallBack);
159 }
160
161 } // namespace Impl
162} // namespace Dune
163
164#endif // DUNE_GEOMETRY_TYPEINDEX_HH
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:125
constexpr Id toId() const
Create an Id representation of this GeometryType.
Definition: type.hh:221
Compute indices for geometry types, taking the dimension into account.
Definition: typeindex.hh:90
static constexpr std::size_t index(const GeometryType &gt)
Compute the index for the given geometry type over all dimensions.
Definition: typeindex.hh:138
static constexpr std::size_t offset(std::size_t dim)
Compute the starting index for a given dimension including irregular geometry types.
Definition: typeindex.hh:113
static constexpr std::size_t size(std::size_t maxdim)
Compute total number of geometry types up to and including the given dimension.
Definition: typeindex.hh:125
Compute per-dimension indices for geometry types.
Definition: typeindex.hh:25
static constexpr std::size_t size(std::size_t dim)
Compute total number of geometry types for the given dimension.
Definition: typeindex.hh:61
static constexpr GeometryType type(std::size_t dim, std::size_t index)
compute the geometry type for the given local index and dimension
Definition: typeindex.hh:79
static constexpr std::size_t index(const GeometryType &gt)
Compute the index for the given geometry type within its dimension.
Definition: typeindex.hh:73
constexpr index_constant< 0 > _0
Compile time index with value 0.
Definition: indices.hh:53
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:158
constexpr GeometryType none(unsigned int dim)
Returns a GeometryType representing a singular of dimension dim.
Definition: type.hh:481
constexpr decltype(auto) switchCases(const Cases &cases, const Value &value, Branches &&branches, ElseBranch &&elseBranch)
Switch statement.
Definition: hybridutilities.hh:461
Dune namespace.
Definition: alignedallocator.hh:13
A unique label for each type of element that can occur in a grid.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)