dune-geometry  2.5.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 #ifndef DUNE_GEOMETRY_TYPEINDEX_HH
4 #define DUNE_GEOMETRY_TYPEINDEX_HH
5 
12 #include <cstddef>
13 
14 #include "type.hh"
15 
16 namespace Dune
17 {
20  {
27  inline static constexpr std::size_t regular_size(std::size_t dim)
28  {
29  // The following expression is derived from the expression for
30  // GlobalGeometryTypeIndex::regular_offset(). Subtracting
31  // regular_offset(dim+1)-regular_offset(dim) we get:
32  //
33  // ((1 << dim+1) >> 1) - ((1 << dim) >> 1)
34  //
35  // We always have
36  //
37  // dim >= 0,
38  //
39  // so
40  //
41  // (1 << dim+1) >= 2 and (1 << dim+2) % 2 == 0.
42  //
43  // So if we apply a single right-shift to that, we will never lose any
44  // set bits, thus
45  //
46  // ((1 << dim+1) >> 1) == (1 << dim)
47  return (1 << dim) - ((1 << dim) >> 1);
48  }
49 
50  public:
56  inline static constexpr std::size_t size(std::size_t dim)
57  {
58  // one for "none"
59  return regular_size(dim) + 1;
60  }
61 
68  inline static std::size_t index(const GeometryType &gt)
69  {
70  if(gt.isNone())
71  {
72  return regular_size(gt.dim());
73  }
74  else
75  {
76  return gt.id() >> 1;
77  }
78  }
79 
81  inline static GeometryType type(std::size_t dim, std::size_t index) {
82  if(index == regular_size(dim)) {
83  GeometryType gt;
84  gt.makeNone(dim);
85  return gt;
86  }
87  else {
88  // the cast to unsigned makes sure this is interpreted as the topology
89  // ID constructor
90  return GeometryType(unsigned(index << 1), dim);
91  }
92  }
93  };
94 
97  {
105  inline static constexpr std::size_t regular_offset(std::size_t dim)
106  {
107  // The number of regular geometry types in a given dimension is
108  // 2^(dim-1). For dim==0 this would yield 1/2 geometry types (which is
109  // obviously bogus, dim==0 has one regular geometry type, the point).
110  // The following expression relies on 1 >> 1 == 0 to treat dim==0
111  // specially.
112  return (1 << dim) >> 1;
113  }
114 
115  public:
120  inline static constexpr std::size_t offset(std::size_t dim)
121  {
122  // dim times "none"
123  return regular_offset(dim) + dim;
124  }
125 
132  inline static constexpr std::size_t size(std::size_t maxdim)
133  {
134  return offset(maxdim+1);
135  }
136 
145  inline static std::size_t index(const GeometryType &gt)
146  {
147  return offset(gt.dim()) + LocalGeometryTypeIndex::index(gt);
148  }
149  };
150 } // namespace Dune
151 
152 #endif // DUNE_GEOMETRY_TYPEINDEX_HH
static std::size_t index(const GeometryType &gt)
Compute the index for the given geometry type over all dimensions.
Definition: typeindex.hh:145
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:120
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:267
unsigned int dim() const
Return dimension of the type.
Definition: type.hh:565
Compute indices for geometry types, taking the dimension into account.
Definition: typeindex.hh:96
Definition: affinegeometry.hh:18
bool isNone() const
Return true if entity is a singular of any dimension.
Definition: type.hh:560
A unique label for each type of element that can occur in a grid.
Compute per-dimension indices for geometry types.
Definition: typeindex.hh:19
static GeometryType type(std::size_t dim, std::size_t index)
compute the geometry type for the given local index and dimension
Definition: typeindex.hh:81
static std::size_t index(const GeometryType &gt)
Compute the index for the given geometry type within its dimension.
Definition: typeindex.hh:68
unsigned int id() const
Return the topology id the type.
Definition: type.hh:570
static constexpr std::size_t size(std::size_t dim)
Compute total number of geometry types for the given dimension.
Definition: typeindex.hh:56
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:132
void makeNone(unsigned int dim)
Make a singular of given dimension.
Definition: type.hh:451