ElementIndex.hpp
Go to the documentation of this file.
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 /// \file
5 /// Defines class ElementIndex.
6 
7 #pragma once
8 
9 #include <array>
10 #include <cstddef>
11 #include <functional>
12 #include <iosfwd>
13 
15 
16 /// \cond
17 template <size_t>
18 struct ElementId;
19 /// \endcond
20 struct SegmentId;
21 
22 namespace ElementIndex_detail {
23 constexpr size_t block_id_bits = 7;
24 constexpr size_t refinement_bits = 5;
25 constexpr size_t max_refinement_level = 20;
26 static_assert(block_id_bits + refinement_bits + max_refinement_level ==
27  8 * sizeof(int),
28  "Bit representation requires padding or is too large");
29 static_assert(two_to_the(refinement_bits) >= max_refinement_level,
30  "Not enough bits to represent all refinement levels");
31 } // namespace ElementIndex_detail
32 
33 class SegmentIndex {
34  public:
35  SegmentIndex() noexcept = default;
36  SegmentIndex(size_t block_id, const SegmentId& segment_id) noexcept;
37  size_t block_id() const noexcept { return block_id_; }
38  size_t index() const noexcept { return index_; }
39  size_t refinement_level() const noexcept { return refinement_level_; }
40 
41  private:
42  unsigned block_id_ : ElementIndex_detail::block_id_bits;
43  unsigned refinement_level_ : ElementIndex_detail::refinement_bits;
44  unsigned index_ : ElementIndex_detail::max_refinement_level;
45 };
46 
47 template <size_t VolumeDim>
48 std::ostream& operator<<(std::ostream& s, const SegmentIndex& index) noexcept;
49 
50 /// \ingroup ParallelGroup
51 /// A class for indexing a Charm array by Element.
52 template <size_t VolumeDim>
53 class ElementIndex {
54  public:
55  ElementIndex() = default;
56  // clang-tidy: mark explicit: we want to allow conversion
57  ElementIndex(const ElementId<VolumeDim>& id) noexcept; // NOLINT
58  size_t block_id() const noexcept { return segments_[0].block_id(); }
59  const std::array<SegmentIndex, VolumeDim>& segments() const noexcept {
60  return segments_;
61  }
62 
63  private:
65 };
66 
67 template <size_t VolumeDim>
68 bool operator==(const ElementIndex<VolumeDim>& a,
69  const ElementIndex<VolumeDim>& b) noexcept;
70 template <size_t VolumeDim>
71 bool operator!=(const ElementIndex<VolumeDim>& a,
72  const ElementIndex<VolumeDim>& b) noexcept;
73 
74 template <size_t VolumeDim>
75 size_t hash_value(const ElementIndex<VolumeDim>& index) noexcept;
76 
77 namespace std {
78 template <size_t VolumeDim>
79 struct hash<ElementIndex<VolumeDim>> {
80  size_t operator()(const ElementIndex<VolumeDim>& x) const noexcept;
81 };
82 } // namespace std
83 
84 template <size_t VolumeDim>
85 std::ostream& operator<<(std::ostream& s,
86  const ElementIndex<VolumeDim>& index) noexcept;
Definition: ElementIndex.hpp:22
A SegmentId labels a segment of the interval [-1,1] and is used to identify the bounds of an Element ...
Definition: SegmentId.hpp:50
An ElementId uniquely labels an Element. It is constructed from the BlockId of the Block to which the...
Definition: ElementId.hpp:36
Definition: ElementIndex.hpp:33
constexpr T two_to_the(T n)
Compute 2 to the n for integral types.
Definition: ConstantExpressions.hpp:34
Define simple functions for constant expressions.
A class for indexing a Charm array by Element.
Definition: ElementIndex.hpp:53