Debian Codegraph @

include/deal.II/base/table_indices.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2005 - 2023 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_table_indices_h
16 #define dealii_table_indices_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/template_constraints.h>
23
24 #include <algorithm>
25 #include <iterator>
26 #include <ostream>
27
28
29 DEAL_II_NAMESPACE_OPEN
30
31
32 /**
33 * A class representing a fixed size array of indices.
34 *
35 * It is used in tensorial objects like the TableBase and SymmetricTensor
36 * classes to represent a nested choice of indices.
37 *
38 * @tparam N The number of indices stored in each object.
39 *
40 * @ingroup data
41 */
42 template <int N>
43 class TableIndices
44 {
45 public:
46 static_assert(N > 0,
47 "TableIndices objects need to represent at least one index.");
48
49
50 /**
51 * Default constructor. This constructor sets all indices to zero.
52 */
53 constexpr TableIndices() = default;
54
55 /**
56 * Constructor. Initializes the indices stored by this object by the given
57 * arguments @p indices
58 *
59 * This constructor will result in a compiler error if
60 * the number of arguments given is different from the number of the
61 * indices this class stores (i.e., the template argument `N` of
62 * this class), or if any of the arguments is not of some integer type.
63 */
64 template <typename... T>
65 constexpr TableIndices(const T... indices);
66
67 /**
68 * Read-only access the value of the <tt>i</tt>th index.
69 */
70 constexpr std::size_t
71 operator[](const unsigned int i) const;
72
73 /**
74 * Write access the value of the <tt>i</tt>th index.
75 */
76 constexpr std::size_t &
77 operator[](const unsigned int i);
78
79 /**
80 * Compare two index fields for equality.
81 */
82 constexpr bool
83 operator==(const TableIndices<N> &other) const;
84
85 /**
86 * Compare two index fields for inequality.
87 */
88 constexpr bool
89 operator!=(const TableIndices<N> &other) const;
90
91 /**
92 * Sort the indices in ascending order. While this operation is not very
93 * useful for Table objects, it is used for the SymmetricTensor class.
94 */
95 DEAL_II_HOST
96 DEAL_II_CONSTEXPR void
97 sort();
98
99 /**
100 * Write or read the data of this object to or from a stream for the purpose
101 * of serialization using the [BOOST serialization
102 * library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html).
103 */
104 template <class Archive>
105 void
106 serialize(Archive &ar, const unsigned int version);
107
108 protected:
109 /**
110 * Store the indices in an array.
111 */
112 std::size_t indices[N]{};
113 };
114
115
116
117 /* --------------------- Template and inline functions ---------------- */
118
119 template <int N>
120 template <typename... T>
121 constexpr TableIndices<N>::TableIndices(const T... args)
122 : indices{static_cast<std::size_t>(args)...}
123 {
124 static_assert(
125 internal::TemplateConstraints::all_true<std::is_integral_v<T>...>::value,
126 "Not all of the parameters have integral type!");
127 static_assert(sizeof...(T) == N, "Wrong number of constructor arguments!");
128 }
129
130
131 template <int N>
132 constexpr inline std::size_t
133 TableIndices<N>::operator[](const unsigned int i) const
134 {
135 AssertIndexRange(i, N);
136 return indices[i];
137 }
138
139
140 template <int N>
141 constexpr inline std::size_t &
142 TableIndices<N>::operator[](const unsigned int i)
143 {
144 AssertIndexRange(i, N);
145 return indices[i];
146 }
147
148
149 template <int N>
150 constexpr bool
151 TableIndices<N>::operator==(const TableIndices<N> &other) const
152 {
153 return std::equal(std::begin(indices),
154 std::end(indices),
155 std::begin(other.indices));
156 }
157
158
159 template <int N>
160 constexpr bool
161 TableIndices<N>::operator!=(const TableIndices<N> &other) const
162 {
163 return !(*this == other);
164 }
165
166
167 template <int N>
168 DEAL_II_HOST DEAL_II_CONSTEXPR inline void
169 TableIndices<N>::sort()
170 {
171 std::sort(std::begin(indices), std::end(indices));
172 }
173
174
175 template <int N>
176 template <class Archive>
177 inline void
178 TableIndices<N>::serialize(Archive &ar, const unsigned int)
179 {
180 ar &indices;
181 }
182
183
184 /**
185 * Output operator for TableIndices objects; reports them in a list like this:
186 * <code>[i1,i2,...]</code>.
187 *
188 * @relatesalso TableIndices
189 */
190 template <int N>
191 std::ostream &
192 operator<<(std::ostream &out, const TableIndices<N> &indices)
193 {
194 out << '[';
195 for (unsigned int i = 0; i < N; ++i)
196 {
197 out << indices[i];
198 if (i + 1 != N)
199 out << ',';
200 }
201 out << ']';
202
203 return out;
204 }
205
206
207 DEAL_II_NAMESPACE_CLOSE
208
209 #endif