Debian Codegraph @

include/deal.II/base/table.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2002 - 2025 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_h
16 #define dealii_table_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/aligned_vector.h>
21 #include <deal.II/base/enable_observer_pointer.h>
22 #include <deal.II/base/exceptions.h>
23 #include <deal.II/base/linear_index_iterator.h>
24 #include <deal.II/base/memory_consumption.h>
25 #include <deal.II/base/table_indices.h>
26
27 #include <algorithm>
28 #include <cstddef>
29 #include <limits>
30
31 DEAL_II_NAMESPACE_OPEN
32
33 // forward declaration
34 #ifndef DOXYGEN
35 template <int N, typename T>
36 class TableBase;
37 template <int N, typename T>
38 class Table;
39 template <typename T>
40 class TransposeTable;
41 template <typename T>
42 class Table<1, T>;
43 template <typename T>
44 class Table<2, T>;
45 template <typename T>
46 class Table<3, T>;
47 template <typename T>
48 class Table<4, T>;
49 template <typename T>
50 class Table<5, T>;
51 template <typename T>
52 class Table<6, T>;
53 #endif
54
55
56
57 namespace internal
58 {
59 /**
60 * @internal Have a namespace in which we declare some classes that are used
61 * to access the elements of tables using the <tt>operator[]</tt>. These are
62 * quite technical, since they have to do their work recursively (due to the
63 * fact that the number of indices is not known, we have to return an
64 * iterator into the next lower dimension object if we access one object,
65 * until we are on the lowest level and can actually return a reference to
66 * the stored data type itself). This is so technical that you will not
67 * usually want to look at these classes at all, except possibly for
68 * educational reasons. None of the classes herein has a interface that you
69 * should use explicitly in your programs (except, of course, through access
70 * to the elements of tables with <tt>operator[]</tt>, which generates
71 * temporary objects of the types of this namespace).
72 */
73 namespace TableBaseAccessors
74 {
75 /**
76 * @internal Have a class which declares some nested alias, depending
77 * on its template parameters. The general template declares nothing, but
78 * there are more useful specializations regarding the last parameter
79 * indicating constness of the table for which accessor objects are to be
80 * generated in this namespace.
81 */
82 template <int N, typename T, bool Constness>
83 struct Types
84 {};
85
86 /**
87 * @internal Have a class which declares some nested alias, depending
88 * on its template parameters. Specialization for accessors to constant
89 * objects.
90 */
91 template <int N, typename T>
92 struct Types<N, T, true>
93 {
94 using value_type = const T;
95 using TableType = const TableBase<N, T>;
96
97 using iterator = typename AlignedVector<T>::const_iterator;
98 using const_iterator = typename AlignedVector<T>::const_iterator;
99
100 using reference = typename AlignedVector<T>::const_reference;
101 using const_reference = typename AlignedVector<T>::const_reference;
102 };
103
104 /**
105 * @internal Have a class which declares some nested alias, depending
106 * on its template parameters. Specialization for accessors to
107 * non-constant objects.
108 */
109 template <int N, typename T>
110 struct Types<N, T, false>
111 {
112 using value_type = T;
113 using TableType = TableBase<N, T>;
114
115 using iterator = typename AlignedVector<T>::iterator;
116 using const_iterator = typename AlignedVector<T>::const_iterator;
117
118 using reference = typename AlignedVector<T>::reference;
119 using const_reference = typename AlignedVector<T>::const_reference;
120 };
121
122
123 /**
124 * @internal Class that acts as accessor to subobjects of tables of type
125 * <tt>Table<N,T></tt>. The template parameter <tt>C</tt> may be either
126 * true or false, and indicates whether the objects worked on are constant
127 * or not (i.e. write access is only allowed if the value is false).
128 *
129 * Since with <tt>N</tt> indices, the effect of applying
130 * <tt>operator[]</tt> is getting access to something with <tt>N-1</tt>
131 * indices, we have to implement these accessor classes recursively, with
132 * stopping when we have only one index left. For the latter case, a
133 * specialization of this class is declared below, where calling
134 * <tt>operator[]</tt> gives you access to the objects actually stored by
135 * the table. In the value given to the index operator needs to be checked
136 * whether it is inside its bounds, for which we need to know which index
137 * of the table we are actually accessing presently. This is done through
138 * the template parameter <tt>P</tt>: it indicates, how many remaining
139 * indices there are. For a vector, <tt>P</tt> may only be one (and then
140 * the specialization below is used). For a table this value may be two,
141 * and when using <tt>operator[]</tt>, an object with <tt>P=1</tt>
142 * emerges.
143 *
144 * The value of <tt>P</tt> is also used to determine the stride: this
145 * object stores a pointer indicating the beginning of the range of
146 * objects that it may access. When we apply <tt>operator[]</tt> on this
147 * object, the resulting new accessor may only access a subset of these
148 * elements, and to know which subset we need to know the dimensions of
149 * the table and the present index, which is indicated by <tt>P</tt>.
150 *
151 * As stated for the entire namespace, you will not usually have to deal
152 * with these classes directly, and should not try to use their interface
153 * directly as it may change without notice. In fact, since the
154 * constructors are made private, you will not even be able to generate
155 * objects of this class, as they are only thought as temporaries for
156 * access to elements of the table class, not for passing them around as
157 * arguments of functions, etc.
158 */
159 template <int N, typename T, bool C, unsigned int P>
160 class Accessor
161 {
162 public:
163 using TableType = typename Types<N, T, C>::TableType;
164
165 using iterator = typename Types<N, T, C>::iterator;
166 using const_iterator = typename Types<N, T, C>::const_iterator;
167
168 using size_type = std::size_t;
169 using difference_type = std::ptrdiff_t;
170
171 private:
172 /**
173 * Constructor. Take a pointer to the table object to know about the
174 * sizes of the various dimensions, and a pointer to the subset of data
175 * we may access.
176 */
177 Accessor(const TableType &table, const iterator data);
178
179 public:
180 /**
181 * Copy constructor. This constructor is public so that one can pass
182 * sub-tables to functions as arguments, as in <code>f(table[i])</code>.
183 *
184 * Using this constructor is risky if accessors are stored longer than
185 * the table it points to. Don't do this.
186 */
187 Accessor(const Accessor &a);
188
189 /**
190 * Index operator. Performs a range check.
191 */
192 Accessor<N, T, C, P - 1>
193 operator[](const size_type i) const;
194
195 /**
196 * Exception for range check. Do not use global exception since this way
197 * we can output which index is the wrong one.
198 */
199 DeclException3(ExcIndexRange,
200 size_type,
201 size_type,
202 size_type,
203 << "Index " << N - P + 1 << "has a value of " << arg1
204 << " but needs to be in the range [" << arg2 << ',' << arg3
205 << "[.");
206
207 private:
208 /**
209 * Store the data given to the constructor. There are no non-const
210 * member functions of this class, so there is no reason not to make
211 * these elements constant.
212 */
213 const TableType &table;
214 const iterator data;
215
216 // declare some other classes
217 // as friends. make sure to
218 // work around bugs in some
219 // compilers
220 template <int N1, typename T1>
221 friend class dealii::Table;
222 template <int N1, typename T1, bool C1, unsigned int P1>
223 friend class Accessor;
224 friend class dealii::Table<N, T>;
225 friend class Accessor<N, T, C, P + 1>;
226 };
227
228
229
230 /**
231 * @internal Accessor class for tables. This is the specialization for the
232 * last index, which actually allows access to the elements of the table,
233 * rather than recursively returning access objects for further subsets.
234 * The same holds for this specialization as for the general template; see
235 * there for more information.
236 */
237 template <int N, typename T, bool C>
238 class Accessor<N, T, C, 1>
239 {
240 public:
241 /**
242 * Typedef constant and non-constant iterator types to the elements of
243 * this row, as well as all the other types usually required for the
244 * standard library algorithms.
245 */
246 using value_type = typename Types<N, T, C>::value_type;
247
248 using iterator = typename Types<N, T, C>::iterator;
249 using const_iterator = typename Types<N, T, C>::const_iterator;
250
251 using reference = typename Types<N, T, C>::reference;
252 using const_reference = typename Types<N, T, C>::const_reference;
253
254 using size_type = std::size_t;
255 using difference_type = std::ptrdiff_t;
256
257 /**
258 * Import an alias from the switch class above.
259 */
260 using TableType = typename Types<N, T, C>::TableType;
261
262 private:
263 /**
264 * Constructor. Take an iterator to the table object to know about the
265 * sizes of the various dimensions, and a iterator to the subset of data
266 * we may access (which in this particular case is only one row).
267 *
268 * The constructor is made private in order to prevent you having such
269 * objects around. The only way to create such objects is via the
270 * <tt>Table</tt> class, which only generates them as temporary objects.
271 * This guarantees that the accessor objects go out of scope earlier
272 * than the parent object, avoid problems with data consistency.
273 */
274 Accessor(const TableType &table, const iterator data);
275
276 public:
277 /**
278 * Copy constructor. This constructor is public so that one can pass
279 * sub-tables to functions as arguments, as in <code>f(table[i])</code>.
280 *
281 * Using this constructor is risky if accessors are stored longer than
282 * the table it points to. Don't do this.
283 */
284 Accessor(const Accessor &a);
285
286 /**
287 * Index operator. Performs a range check.
288 */
289 reference
290 operator[](const size_type) const;
291
292 /**
293 * Return the length of one row, i.e. the number of elements
294 * corresponding to the last index of the table object.
295 */
296 size_type
297 size() const;
298
299 /**
300 * Return an iterator to the first element of this row.
301 */
302 iterator
303 begin() const;
304
305 /**
306 * Return an iterator to the element past the end of this row.
307 */
308 iterator
309 end() const;
310
311 private:
312 /**
313 * Store the data given to the constructor. There are no non-const
314 * member functions of this class, so there is no reason not to make
315 * these elements constant.
316 */
317 const TableType &table;
318 const iterator data;
319
320 // declare some other classes
321 // as friends. make sure to
322 // work around bugs in some
323 // compilers
324 template <int N1, typename T1>
325 friend class dealii::Table;
326 template <int N1, typename T1, bool C1, unsigned int P1>
327 friend class Accessor;
328 friend class dealii::Table<2, T>;
329 friend class Accessor<N, T, C, 2>;
330 };
331 } // namespace TableBaseAccessors
332
333 } // namespace internal
334
335
336
337 /**
338 * A class holding a multi-dimensional array of objects of templated type.
339 * If the template parameter indicating the number of dimensions
340 * is one, then this class more or less represents a vector; if it is two then
341 * it is a matrix; and so on.
342 *
343 * This class specifically replaces attempts at higher-dimensional arrays like
344 * <tt>std::vector<std::vector<T>></tt>, or even higher nested constructs.
345 * These constructs have the disadvantage that they are hard to initialize, and
346 * most importantly that they are very inefficient if all rows of a matrix or
347 * higher-dimensional table have the same size
348 * (which is the usual case), since then the memory for each row is allocated
349 * independently, both wasting time and memory. This can be made more
350 * efficient by allocating only one chunk of memory for the entire object, which
351 * is what the current class does.
352 *
353 *
354 * <h3>Comparison with the Tensor class</h3>
355 *
356 * In some way, this class is similar to the Tensor class, in that it
357 * templatizes on the number of dimensions. However, there are two major
358 * differences. The first is that the Tensor class stores only numeric values
359 * (as <tt>double</tt>s), while the Table class stores arbitrary objects. The
360 * second is that the Tensor class has fixed sizes in each dimension, also given
361 * as a template argument, while this class can handle arbitrary and different
362 * sizes in each dimension.
363 *
364 * This has two consequences. First, since the size is not known at compile
365 * time, it has to do explicit memory allocation. Second, the layout of
366 * individual elements is not known at compile time, so access is slower than
367 * for the Tensor class where the number of elements are their location is
368 * known at compile time and the compiler can optimize with this knowledge
369 * (for example when unrolling loops). On the other hand, this class is of
370 * course more flexible, for example when you want a two-dimensional table
371 * with the number of rows equal to the number of degrees of freedom on a
372 * cell, and the number of columns equal to the number of quadrature points.
373 * Both numbers may only be known at run-time, so a flexible table is needed
374 * here. Furthermore, you may want to store, say, the gradients of shape
375 * functions, so the data type is not a single scalar value, but a tensor
376 * itself.
377 *
378 *
379 * <h3>Dealing with large data sets</h3>
380 *
381 * The Table classes (derived from this class) are frequently used to store
382 * large data tables. A modest example is given in step-53 where we store a
383 * $380 \times 220$ table of geographic elevation data for a region of
384 * Africa, and this data requires about 670 kB if memory; however,
385 * tables that store three- or more-dimensional data (say, information about
386 * the density, pressure, and temperature in the earth interior on a regular
387 * grid of `(latitude, longitude, depth)` points) can easily run into hundreds
388 * of megabytes or more. These tables are then often provided to classes
389 * such as InterpolatedTensorProductGridData or InterpolatedUniformGridData.
390 *
391 * If you need to load such tables on single-processor (or multi-threaded)
392 * jobs, then there is nothing you can do about the size of these tables: The
393 * table just has to fit into memory. But, if your program is parallelized
394 * via MPI, then a typical first implementation would create a table object
395 * on every process and fill it on every MPI process by reading the data
396 * from a file. This is inefficient from two perspectives:
397 * - You will have a lot of processes that are all trying to read from
398 * the same file at the same time.
399 * - In most cases, the data stored on every process is the same, and
400 * while every process needs to be able to read from a table, it is not
401 * necessary that every process stores its own table: All MPI processes
402 * that happen to be located on the same machine might as well store
403 * only one copy and make it available to each other via
404 * [shared memory](https://en.wikipedia.org/wiki/Shared_memory); in
405 * this model, only one MPI process per machine needs to store the data, and
406 * all other processes could then access it.
407 *
408 * Both of these use cases are enabled by the
409 * TableBase::replicate_across_communicator() function that is internally based
410 * on AlignedVector::replicate_across_communicator(). This function allows for
411 * workflows like the following where we put that MPI process with rank zero in
412 * charge of reading the data (but it could have been any other "root rank" as
413 * well):
414 * @code
415 * const unsigned int N=..., M=...; // table sizes, assumed known
416 * Table<2,double> data_table;
417 * const unsigned int root_rank = 0;
418 *
419 * if (Utilities::MPI::this_mpi_process(mpi_communicator) == root_rank)
420 * {
421 * data_table.resize (N,M);
422 *
423 * std::ifstream input_file ("data_file.dat");
424 * ...; // read the data from the file
425 * }
426 *
427 * // Now distribute to all processes
428 * data_table.replicate_across_communicator (mpi_communicator, root_rank);
429 * @endcode
430 *
431 * The last call in this code snippet makes sure that the data is made
432 * available on all non-root processes, either by re-creating a copy of
433 * the table in the other processes' memory space or, if possible,
434 * by creating copies in shared memory once for all processes located
435 * on each of the machines used by the MPI job.
436 *
437 * @ingroup data
438 */
439 template <int N, typename T>
440 class TableBase : public EnableObserverPointer
441 {
442 public:
443 using value_type = T;
444
445 /**
446 * Integer type used to count the number of elements in this container.
447 */
448 using size_type = typename AlignedVector<T>::size_type;
449
450
451 /**
452 * Default constructor. Set all dimensions to zero.
453 */
454 TableBase() = default;
455
456 /**
457 * Constructor. Initialize the array with the given dimensions in each index
458 * component.
459 */
460 explicit TableBase(const TableIndices<N> &sizes);
461
462 /**
463 * Constructor. Initialize the array with the given dimensions in each index
464 * component, and then initialize the elements of the table using the second
465 * and third argument by calling fill(entries,C_style_indexing).
466 */
467 template <typename InputIterator>
468 TableBase(const TableIndices<N> &sizes,
469 InputIterator entries,
470 const bool C_style_indexing = true);
471
472 /**
473 * Copy constructor. Performs a deep copy.
474 */
475 TableBase(const TableBase<N, T> &src);
476
477 /**
478 * Copy constructor. Performs a deep copy from a table object storing some
479 * other data type.
480 */
481 template <typename T2>
482 TableBase(const TableBase<N, T2> &src);
483
484 /**
485 * Move constructor. Transfers the contents of another Table.
486 */
487 TableBase(TableBase<N, T> &&src) noexcept;
488
489 /**
490 * Destructor. Free allocated memory.
491 */
492 ~TableBase() override = default;
493
494 /**
495 * Assignment operator. Copy all elements of <tt>src</tt> into the matrix.
496 * The size is adjusted if needed.
497 *
498 * We can't use the other, templatized version since if we don't declare
499 * this one, the compiler will happily generate a predefined copy operator
500 * which is not what we want.
501 */
502 TableBase<N, T> &
503 operator=(const TableBase<N, T> &src);
504
505 /**
506 * Copy operator. Copy all elements of <tt>src</tt> into the array. The size
507 * is adjusted if needed.
508 *
509 * This function requires that the type <tt>T2</tt> is convertible to
510 * <tt>T</tt>.
511 */
512 template <typename T2>
513 TableBase<N, T> &
514 operator=(const TableBase<N, T2> &src);
515
516 /**
517 * Move assignment operator. Transfer all elements of <tt>src</tt> into the
518 * table.
519 */
520 TableBase<N, T> &
521 operator=(TableBase<N, T> &&src) noexcept;
522
523 /**
524 * Test for equality of two tables.
525 */
526 bool
527 operator==(const TableBase<N, T> &T2) const;
528
529 /**
530 * Set all entries to their default value (i.e. copy them over with default
531 * constructed objects). Do not change the size of the table, though.
532 */
533 void
534 reset_values();
535
536 /**
537 * Set the dimensions of this object to the sizes given in the first
538 * argument, and allocate the required memory for table entries to
539 * accommodate these sizes. If @p omit_default_initialization
540 * is set to @p false, all elements of the table are set to a
541 * default constructed object for the element type. Otherwise the
542 * memory is left in an uninitialized or otherwise undefined state.
543 */
544 void
545 reinit(const TableIndices<N> &new_size,
546 const bool omit_default_initialization = false);
547
548 /**
549 * Set all dimensions to zero.
550 */
551 void
552 clear();
553
554 /**
555 * Size of the table in direction <tt>i</tt>.
556 */
557 size_type
558 size(const unsigned int i) const;
559
560 /**
561 * Return the sizes of this object in each direction.
562 */
563 const TableIndices<N> &
564 size() const;
565
566 /**
567 * Return the number of elements stored in this object, which is the product
568 * of the extensions in each dimension.
569 */
570 size_type
571 n_elements() const;
572
573 /**
574 * Return whether the object is empty, i.e. one of the directions is zero.
575 * This is equivalent to <tt>n_elements()==0</tt>.
576 */
577 bool
578 empty() const;
579
580 /**
581 * Fill this table (which is assumed to already have the correct size) from
582 * a source given by dereferencing the given forward iterator (which could,
583 * for example, be a pointer to the first element of an array, or an
584 * inserting std::istream_iterator). The second argument denotes whether the
585 * elements pointed to are arranged in a way that corresponds to the last
586 * index running fastest or slowest. The default is to use C-style indexing
587 * where the last index runs fastest (as opposed to Fortran-style where the
588 * first index runs fastest when traversing multidimensional arrays. For
589 * example, if you try to fill an object of type Table<2,T>, then calling
590 * this function with the default value for the second argument will result
591 * in the equivalent of doing
592 * @code
593 * Table<2,T> t;
594 * for (unsigned int i=0; i<t.size(0); ++i)
595 * for (unsigned int j=0; j<t.size(1); ++j)
596 * t[i][j] = *entries++;
597 * @endcode
598 * On the other hand, if the second argument to this function is false, then
599 * this would result in code of the following form:
600 * @code
601 * Table<2,T> t;
602 * for (unsigned int j=0; j<t.size(1); ++j)
603 * for (unsigned int i=0; i<t.size(0); ++i)
604 * t[i][j] = *entries++;
605 * @endcode
606 * Note the switched order in which we fill the table elements by traversing
607 * the given set of iterators.
608 *
609 * @param entries An iterator to a set of elements from which to initialize
610 * this table. It is assumed that iterator can be incremented and
611 * dereferenced a sufficient number of times to fill this table.
612 * @param C_style_indexing If true, run over elements of the table with the
613 * last index changing fastest as we dereference subsequent elements of the
614 * input range. If false, change the first index fastest.
615 */
616 template <typename InputIterator>
617 void
618 fill(InputIterator entries, const bool C_style_indexing = true);
619
620 /**
621 * Fill all table entries with the same value.
622 */
623 void
624 fill(const T &value);
625
626 /**
627 * Return a read-write reference to the indicated element.
628 */
629 typename AlignedVector<T>::reference
630 operator()(const TableIndices<N> &indices);
631
632 /**
633 * Return the value of the indicated element as a read-only reference.
634 *
635 * We return the requested value as a constant reference rather than by
636 * value since this object may hold data types that may be large, and we
637 * don't know here whether copying is expensive or not.
638 */
639 typename AlignedVector<T>::const_reference
640 operator()(const TableIndices<N> &indices) const;
641
642 /**
643 * This function replicates the state found on the process indicated by
644 * @p root_process across all processes of the MPI communicator. The current
645 * state found on any of the processes other than @p root_process is lost
646 * in this process. One can imagine this operation to act like a call to
647 * Utilities::MPI::broadcast() from the root process to all other processes,
648 * though in practice the function may try to move the data into shared
649 * memory regions on each of the machines that host MPI processes and
650 * let all MPI processes on this machine then access this shared memory
651 * region instead of keeping their own copy. See the general documentation
652 * of this class for a code example.
653 *
654 * The intent of this function is to quickly exchange large arrays from
655 * one process to others, rather than having to compute or create it on
656 * all processes. This is specifically the case for data loaded from
657 * disk -- say, large data tables -- that are more easily dealt with by
658 * reading once and then distributing across all processes in an MPI
659 * universe, than letting each process read the data from disk itself.
660 * Specifically, the use of shared memory regions allows for replicating
661 * the data only once per multicore machine in the MPI universe, rather
662 * than replicating data once for each MPI process. This results in
663 * large memory savings if the data is large on today's machines that
664 * can easily house several dozen MPI processes per shared memory
665 * space.
666 *
667 * This function does not imply a model of keeping data on different processes
668 * in sync, as LinearAlgebra::distributed::Vector and other vector classes do
669 * where there exists a notion of certain elements of the vector owned by each
670 * process and possibly ghost elements that are mirrored from its owning
671 * process to other processes. Rather, the elements of the current object are
672 * simply copied to the other processes, and it is useful to think of this
673 * operation as creating a set of `const` TableBase objects on all
674 * processes that should not be changed any more after the replication
675 * operation, as this is the only way to ensure that the vectors remain the
676 * same on all processes. This is particularly true because of the use of
677 * shared memory regions where any modification of a vector element on one MPI
678 * process may also result in a modification of elements visible on other
679 * processes, assuming they are located within one shared memory node.
680 *
681 * @note The use of shared memory between MPI processes requires
682 * that the detected MPI installation supports the necessary operations.
683 * This is the case for MPI 3.0 and higher.
684 *
685 * @note This function is not cheap. It needs to create sub-communicators
686 * of the provided @p communicator object, which is generally an expensive
687 * operation. Likewise, the generation of shared memory spaces is not
688 * a cheap operation. As a consequence, this function primarily makes
689 * sense when the goal is to share large read-only data tables among
690 * processes; examples are data tables that are loaded at start-up
691 * time and then used over the course of the run time of the program.
692 * In such cases, the start-up cost of running this function can be
693 * amortized over time, and the potential memory savings from not having to
694 * store the table on each process may be substantial on machines with
695 * large core counts on which many MPI processes run on the same machine.
696 *
697 * @note This function only makes sense if the data type `T` is
698 * "self-contained", i.e., all of its information is stored in its
699 * member variables, and if none of the member variables are pointers
700 * to other parts of the memory. This is because if a type `T` does
701 * have pointers to other parts of memory, then moving `T` into
702 * a shared memory space does not result in the other processes having
703 * access to data that the object points to with its member variable
704 * pointers: These continue to live only on one process, and are
705 * typically in memory areas not accessible to the other processes.
706 * As a consequence, the usual use case for this function is to share
707 * arrays of simple objects such as `double`s or `int`s.
708 *
709 * @note After calling this function, objects on different MPI processes
710 * share a common state. That means that certain operations become
711 * "collective", i.e., they must be called on all participating
712 * processors at the same time. In particular, you can no longer call
713 * resize(), reserve(), or clear() on one MPI process -- you have to do
714 * so on all processes at the same time, because they have to communicate
715 * for these operations. If you do not do so, you will likely get
716 * a deadlock that may be difficult to debug. By extension, this rule of
717 * only collectively resizing extends to this function itself: You can
718 * not call it twice in a row because that implies that first all but the
719 * `root_process` throw away their data, which is not a collective
720 * operation. Generally, these restrictions on what can and can not be
721 * done hint at the correctness of the comments above: You should treat
722 * an AlignedVector on which the current function has been called as
723 * `const`, on which no further operations can be performed until
724 * the destructor is called.
725 */
726 void
727 replicate_across_communicator(const MPI_Comm communicator,
728 const unsigned int root_process);
729
730 /**
731 * Swap the contents of this table and the other table @p v. One could do
732 * this operation with a temporary variable and copying over the data
733 * elements, but this function is significantly more efficient since it only
734 * swaps the pointers to the data of the two vectors and therefore does not
735 * need to allocate temporary storage and move data around.
736 *
737 * This function is analogous to the @p swap function of all C++
738 * standard containers. Also, there is a global function <tt>swap(u,v)</tt>
739 * that simply calls <tt>u.swap(v)</tt>, again in analogy to standard
740 * functions.
741 */
742 void
743 swap(TableBase<N, T> &v) noexcept;
744
745 /**
746 * Determine an estimate for the memory consumption (in bytes) of this
747 * object.
748 */
749 std::size_t
750 memory_consumption() const;
751
752 /**
753 * Write or read the data of this object to or from a stream for the purpose
754 * of serialization using the [BOOST serialization
755 * library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html).
756 */
757 template <class Archive>
758 void
759 serialize(Archive &ar, const unsigned int version);
760
761 protected:
762 /**
763 * Return the position of the indicated element within the array of elements
764 * stored one after the other. This function does no index checking.
765 */
766 size_type
767 position(const TableIndices<N> &indices) const;
768
769 /**
770 * Return a read-write reference to the indicated element.
771 *
772 * This function does no bounds checking and is only to be used internally
773 * and in functions already checked.
774 */
775 typename AlignedVector<T>::reference
776 el(const TableIndices<N> &indices);
777
778 /**
779 * Return the value of the indicated element as a read-only reference.
780 *
781 * This function does no bounds checking and is only to be used internally
782 * and in functions already checked.
783 *
784 * We return the requested value as a constant reference rather than by
785 * value since this object may hold data types that may be large, and we
786 * don't know here whether copying is expensive or not.
787 */
788 typename AlignedVector<T>::const_reference
789 el(const TableIndices<N> &indices) const;
790
791 protected:
792 /**
793 * Component-array.
794 */
795 AlignedVector<T> values;
796
797 /**
798 * Size in each direction of the table.
799 */
800 TableIndices<N> table_size;
801
802 // Make all other table classes friends.
803 template <int, typename>
804 friend class TableBase;
805 };
806
807
808 /**
809 * A class representing a table with arbitrary but fixed number of indices.
810 * This general template implements some additional functions over those
811 * provided by the TableBase class, such as indexing functions taking the
812 * correct number of arguments, etc.
813 *
814 * Rather than this general template, these functions are implemented in
815 * partial specializations of this class, with fixed numbers of dimensions.
816 * See there, and in the documentation of the base class for more information.
817 *
818 * @ingroup data
819 */
820 template <int N, typename T>
821 class Table;
822
823
824 /**
825 * A class representing a one-dimensional table, i.e. a vector-like class.
826 * The majority of the interface of this class is implemented in the
827 * TableBase base class. See there for an outline of the rationale for and
828 * interface of this class.
829 *
830 * @ingroup data
831 */
832 template <typename T>
833 class Table<1, T> : public TableBase<1, T>
834 {
835 public:
836 /**
837 * Integer type used to count the number of elements in this container.
838 */
839 using size_type = typename TableBase<1, T>::size_type;
840
841 /**
842 * Default constructor. Set all dimensions to zero.
843 */
844 Table() = default;
845
846 /**
847 * Constructor. Pass down the given dimension to the base class.
848 */
849 explicit Table(const size_type size);
850
851 /**
852 * Constructor. Create a table with a given size and initialize it from a
853 * set of iterators.
854 *
855 * This function is entirely equivalent to creating a table <code>t</code>
856 * of the given size and then calling
857 * @code
858 * t.fill (entries, C_style_indexing);
859 * @endcode
860 * on it, using the TableBase::fill() function where the arguments are
861 * explained in more detail. The point, however, is that that is only
862 * possible if the table can be changed after running the constructor,
863 * whereas calling the current constructor allows sizing and initializing an
864 * object right away so that it can be marked const.
865 *
866 * Using this constructor, you can do things like this:
867 * @code
868 * const double values[] = { 1, 2, 3 };
869 * const Table<1,double> t(3, entries, true);
870 * @endcode
871 * You can also initialize a table right from a file, using input iterators:
872 * @code
873 * std::ifstream input ("myfile");
874 * const Table<1,double> t(3,
875 * std::istream_iterator<double>(input),
876 * true);
877 * @endcode
878 *
879 *
880 * @param size The size of this one-dimensional table.
881 * @param entries An iterator to a set of elements from which to initialize
882 * this table. It is assumed that iterator can be incremented and
883 * dereferenced a sufficient number of times to fill this table.
884 * @param C_style_indexing If true, run over elements of the table with the
885 * last index changing fastest as we dereference subsequent elements of the
886 * input range. If false, change the first index fastest.
887 */
888 template <typename InputIterator>
889 Table(const size_type size,
890 InputIterator entries,
891 const bool C_style_indexing = true);
892
893 /**
894 * Access operator. Since this is a one-dimensional object, this simply
895 * accesses the requested data element. Returns a read-only reference.
896 */
897 typename AlignedVector<T>::const_reference
898 operator[](const size_type i) const;
899
900 /**
901 * Access operator. Since this is a one-dimensional object, this simply
902 * accesses the requested data element. Returns a read-write reference.
903 */
904 typename AlignedVector<T>::reference
905 operator[](const size_type i);
906
907 /**
908 * Access operator. Since this is a one-dimensional object, this simply
909 * accesses the requested data element. Returns a read-only reference.
910 */
911 typename AlignedVector<T>::const_reference
912 operator()(const size_type i) const;
913
914 /**
915 * Access operator. Since this is a one-dimensional object, this simply
916 * accesses the requested data element. Returns a read-write reference.
917 */
918 typename AlignedVector<T>::reference
919 operator()(const size_type i);
920
921 /**
922 * Make the variations of `operator()` from the base class available.
923 */
924 using TableBase<1, T>::operator();
925 };
926
927
928
929 /**
930 * A namespace for iterators and accessors for Table<2, T> and
931 * TransposeTable. These classes have special accessors (that is, compared to
932 * Table<3, T>) since they have a matrix-like structure; i.e., the accessors
933 * also provide row and column information and are designed to be compatible
934 * with the SparseMatrix and SparsityPattern iterator classes.
935 */
936 namespace MatrixTableIterators
937 {
938 /**
939 * @brief Enumeration describing the storage order (i.e., the in-memory
940 * layout) of a table class.
941 */
942 enum class Storage
943 {
944 /**
945 * The data are organized in row-major (i.e., C-style) order.
946 */
947 row_major,
948
949 /**
950 * The data are organized in column-major (i.e., Fortran-style) order.
951 */
952 column_major
953 };
954
955 // Forward declaration of the iterator class.
956 template <typename TableType, bool Constness, Storage storage_order>
957 class Iterator;
958
959 /**
960 * @brief %Accessor class template. This class is partially specialized for
961 * both values of <code>Constness</code>.
962 */
963 template <typename TableType, bool Constness, Storage storage_order>
964 class Accessor;
965
966 /**
967 * @brief %Accessor base class for Table<2, T> and TransposeTable.
968 *
969 * This class is compatible with the requirements for an %Accessor described
970 * in LinearIndexIterator: See the documentation of that class for a
971 * description of the split between iterators and accessors.
972 *
973 * @tparam TableType the type of the %Table, e.g., Table<2, T> or
974 * TransposeTable.
975 *
976 * @tparam Constness whether or not this object stores a constant pointer
977 * and can modify the provided table object.
978 *
979 * @tparam storage_order The storage scheme of the underlying table, e.g.,
980 * Storage::row_major for Table<2, T>.
981 */
982 template <typename TableType, bool Constness, Storage storage_order>
983 class AccessorBase
984 {
985 public:
986 /**
987 * Type of the stored pointer to the table.
988 */
989 using container_pointer_type =
990 std::conditional_t<Constness, const TableType *, TableType *>;
991
992 /**
993 * Value type of the underlying container.
994 */
995 using value_type = typename TableType::value_type;
996
997 /**
998 * Numerical type of the row and column indices of the table.
999 */
1000 using size_type = typename TableType::size_type;
1001
1002 /**
1003 * Default constructor.
1004 */
1005 AccessorBase();
1006
1007 /**
1008 * Constructor setting up the end iterator.
1009 */
1010 AccessorBase(const container_pointer_type table);
1011
1012 /**
1013 * Copy constructor from a non-const Accessor.
1014 */
1015 AccessorBase(const AccessorBase<TableType, false, storage_order> &);
1016
1017 /**
1018 * Constructor taking an array index.
1019 */
1020 AccessorBase(const container_pointer_type table,
1021 const std::ptrdiff_t linear_index);
1022
1023 /**
1024 * Comparison operator.
1025 */
1026 template <bool OtherConstness>
1027 friend bool
1028 operator==(
1029 const AccessorBase<TableType, Constness, storage_order> &left,
1030 const AccessorBase<TableType, OtherConstness, storage_order> &right)
1031 {
1032 return left.container == right.container &&
1033 left.linear_index == right.linear_index;
1034 }
1035
1036 /**
1037 * Get a constant reference to the value of the element represented by
1038 * this accessor.
1039 */
1040 const value_type &
1041 value() const;
1042
1043 /**
1044 * Conversion operator that returns a constant reference to the element.
1045 */
1046 operator const value_type &() const;
1047
1048 /**
1049 * Return the row of the current entry.
1050 */
1051 size_type
1052 row() const;
1053
1054 /**
1055 * Return the column of the current entry.
1056 */
1057 size_type
1058 column() const;
1059
1060 protected:
1061 /**
1062 * Pointer to the table.
1063 */
1064 container_pointer_type container;
1065
1066 /**
1067 * Current index.
1068 */
1069 std::ptrdiff_t linear_index;
1070
1071 /**
1072 * Check that <code>linear_index</code> corresponds to an entry that is
1073 * actually stored by the table (i.e., assert that
1074 * <code>linear_index</code> is nonnegative and less than
1075 * <code>container->size()</code>).
1076 */
1077 void
1078 assert_valid_linear_index() const;
1079
1080 // Make the const version a friend for copying.
1081 friend class AccessorBase<TableType, true, storage_order>;
1082
1083 // Make the underlying iterator class a friend.
1084 friend class LinearIndexIterator<
1085 Iterator<TableType, Constness, storage_order>,
1086 Accessor<TableType, Constness, storage_order>>;
1087 };
1088
1089 /**
1090 * @brief %Accessor class offering read-only access to elements of a
1091 * table. This is the same as the base class.
1092 */
1093 template <typename TableType, Storage storage_order>
1094 class Accessor<TableType, true, storage_order>
1095 : public AccessorBase<TableType, true, storage_order>
1096 {
1097 public:
1098 /**
1099 * Use the base class value type.
1100 */
1101 using value_type =
1102 typename AccessorBase<TableType, true, storage_order>::value_type;
1103
1104 /**
1105 * Use the base class size type.
1106 */
1107 using size_type =
1108 typename AccessorBase<TableType, true, storage_order>::size_type;
1109
1110 /**
1111 * Inherit the base class constructors.
1112 */
1113 using AccessorBase<TableType, true, storage_order>::AccessorBase;
1114 };
1115
1116 /**
1117 * @brief %Accessor class offering read and write access to the elements of
1118 * a table.
1119 */
1120 template <typename TableType, Storage storage_order>
1121 class Accessor<TableType, false, storage_order>
1122 : public AccessorBase<TableType, false, storage_order>
1123 {
1124 public:
1125 /**
1126 * Use the base class value type.
1127 */
1128 using value_type =
1129 typename AccessorBase<TableType, true, storage_order>::value_type;
1130
1131 /**
1132 * Use the base class size type.
1133 */
1134 using size_type =
1135 typename AccessorBase<TableType, true, storage_order>::size_type;
1136
1137 /**
1138 * Inherit the base class constructors.
1139 */
1140 using AccessorBase<TableType, false, storage_order>::AccessorBase;
1141
1142 /**
1143 * Assignment operator. This assigns a new value to the table entry at the
1144 * current row and column coordinates.
1145 */
1146 const Accessor<TableType, false, storage_order> &
1147 operator=(const value_type &) const;
1148
1149 /**
1150 * Move assignment operator. This assigns a new value to the table entry at
1151 * the current row and column coordinates.
1152 */
1153 const Accessor<TableType, false, storage_order> &
1154 operator=(value_type &&) const;
1155
1156 /**
1157 * Since we overload value() we have to explicitly use the base class
1158 * version.
1159 */
1160 using AccessorBase<TableType, false, storage_order>::value;
1161
1162 /**
1163 * Get a reference to the value of the element represented by
1164 * this accessor.
1165 */
1166 value_type &
1167 value() const;
1168
1169 /**
1170 * Conversion operator that returns a reference to the element.
1171 */
1172 operator value_type &();
1173 };
1174
1175 /**
1176 * @brief %Iterator class for both matrix-like tables, i.e., Table<2, T> and
1177 * TransposeTable.
1178 *
1179 * @tparam TableType the type of the %Table, e.g., Table<2, T> or
1180 * TransposeTable.
1181 *
1182 * @tparam Constness whether or not this is a constant iterator.
1183 *
1184 * @tparam storage_order The storage scheme of the underlying table, e.g.,
1185 * Storage::row_major for Table<2, T>.
1186 */
1187 template <typename TableType, bool Constness, Storage storage_order>
1188 class Iterator
1189 : public LinearIndexIterator<Iterator<TableType, Constness, storage_order>,
1190 Accessor<TableType, Constness, storage_order>>
1191 {
1192 public:
1193 /**
1194 * Size type used by the underlying table.
1195 */
1196 using size_type = typename TableType::size_type;
1197
1198 /**
1199 * Type of the stored pointer to the table.
1200 */
1201 using container_pointer_type =
1202 std::conditional_t<Constness, const TableType *, TableType *>;
1203
1204 /**
1205 * Constructor from an accessor.
1206 */
1207 Iterator(const Accessor<TableType, Constness, storage_order> &accessor);
1208
1209 /**
1210 * Constructor. Create the end iterator for a table.
1211 */
1212 Iterator(const container_pointer_type object);
1213
1214 /**
1215 * Constructor for a particular table entry.
1216 */
1217 Iterator(const container_pointer_type object,
1218 const size_type row,
1219 const size_type column);
1220
1221 /**
1222 * Copy constructor from a non-const iterator.
1223 */
1224 Iterator(const Iterator<TableType, false, storage_order> &i);
1225
1226 /**
1227 * Constructor for an entry with a particular linear index.
1228 */
1229 Iterator(const container_pointer_type container,
1230 const std::ptrdiff_t linear_index);
1231 };
1232 } // namespace MatrixTableIterators
1233
1234
1235
1236 /**
1237 * A class representing a two-dimensional table, i.e. a matrix of objects (not
1238 * necessarily only numbers).
1239 * The majority of the interface of this class is implemented in the
1240 * TableBase base class. See there for an outline of the rationale for and
1241 * interface of this class.
1242 *
1243 * This class also serves as the base class for the FullMatrix class
1244 * and consequently has a number of functions that are specific to
1245 * matrices and their needs.
1246 *
1247 * @ingroup data
1248 */
1249 template <typename T>
1250 class Table<2, T> : public TableBase<2, T>
1251 {
1252 public:
1253 /**
1254 * Integer type used to count the number of elements in this container.
1255 */
1256 using size_type = typename TableBase<2, T>::size_type;
1257
1258 /**
1259 * Typedef for the values in the table.
1260 */
1261 using value_type = typename AlignedVector<T>::value_type;
1262
1263 /**
1264 * Typedef for the references in the table.
1265 */
1266 using reference = typename AlignedVector<T>::reference;
1267
1268 /**
1269 * Typedef for the constant references in the table.
1270 */
1271 using const_reference = typename AlignedVector<T>::const_reference;
1272
1273 /**
1274 * Typedef for a constant iterator that traverses the table in column-major
1275 * order.
1276 */
1277 using const_iterator = MatrixTableIterators::
1278 Iterator<Table<2, T>, true, MatrixTableIterators::Storage::row_major>;
1279
1280 /**
1281 * Typedef for an iterator that traverses the table in column-major order.
1282 */
1283 using iterator = MatrixTableIterators::
1284 Iterator<Table<2, T>, false, MatrixTableIterators::Storage::row_major>;
1285
1286 /**
1287 * Default constructor. Set all dimensions to zero.
1288 */
1289 Table() = default;
1290
1291 /**
1292 * Constructor. Pass down the given dimensions to the base class.
1293 */
1294 Table(const size_type size1, const size_type size2);
1295
1296 /**
1297 * Constructor. Create a table with a given size and initialize it from a
1298 * set of iterators.
1299 *
1300 * This function is entirely equivalent to creating a table <code>t</code>
1301 * of the given size and then calling
1302 * @code
1303 * t.fill (entries, C_style_indexing);
1304 * @endcode
1305 * on it, using the TableBase::fill() function where the arguments are
1306 * explained in more detail. The point, however, is that that is only
1307 * possible if the table can be changed after running the constructor,
1308 * whereas calling the current constructor allows sizing and initializing an
1309 * object right away so that it can be marked const.
1310 *
1311 * Using this constructor, you can do things like this:
1312 * @code
1313 * const double values[] = { 1, 2, 3, 4, 5, 6 };
1314 * const Table<2,double> t(2, 3, entries, true);
1315 * @endcode
1316 * You can also initialize a table right from a file, using input iterators:
1317 * @code
1318 * std::ifstream input ("myfile");
1319 * const Table<2,double> t(2, 3,
1320 * std::istream_iterator<double>(input),
1321 * true);
1322 * @endcode
1323 *
1324 *
1325 * @param size1 The size of this table in the first dimension.
1326 * @param size2 The size of this table in the second dimension.
1327 * @param entries An iterator to a set of elements from which to initialize
1328 * this table. It is assumed that iterator can be incremented and
1329 * dereferenced a sufficient number of times to fill this table.
1330 * @param C_style_indexing If true, run over elements of the table with the
1331 * last index changing fastest as we dereference subsequent elements of the
1332 * input range. If false, change the first index fastest.
1333 */
1334 template <typename InputIterator>
1335 Table(const size_type size1,
1336 const size_type size2,
1337 InputIterator entries,
1338 const bool C_style_indexing = true);
1339
1340 /**
1341 * Reinitialize the object. This function is mostly here for compatibility
1342 * with the earlier <tt>vector2d</tt> class. Passes down to the base class
1343 * by converting the arguments to the data type requested by the base class.
1344 */
1345 void
1346 reinit(const size_type size1,
1347 const size_type size2,
1348 const bool omit_default_initialization = false);
1349
1350 using TableBase<2, T>::reinit;
1351
1352 /**
1353 * Access operator. Generate an object that accesses the requested row of
1354 * this two-dimensional table. Range checks are performed.
1355 *
1356 * This version of the function only allows read access.
1357 */
1358 dealii::internal::TableBaseAccessors::Accessor<2, T, true, 1>
1359 operator[](const size_type i) const;
1360
1361 /**
1362 * Access operator. Generate an object that accesses the requested row of
1363 * this two-dimensional table. Range checks are performed.
1364 *
1365 * This version of the function allows read-write access.
1366 */
1367 dealii::internal::TableBaseAccessors::Accessor<2, T, false, 1>
1368 operator[](const size_type i);
1369
1370 /**
1371 * Direct access to one element of the table by specifying all indices at
1372 * the same time. Range checks are performed.
1373 *
1374 * This version of the function only allows read access.
1375 */
1376 typename AlignedVector<T>::const_reference
1377 operator()(const size_type i, const size_type j) const;
1378
1379 /**
1380 * Direct access to one element of the table by specifying all indices at
1381 * the same time. Range checks are performed.
1382 *
1383 * This version of the function allows read-write access.
1384 */
1385 typename AlignedVector<T>::reference
1386 operator()(const size_type i, const size_type j);
1387
1388 /**
1389 * Make the variations of `operator()` from the base class available.
1390 */
1391 using TableBase<2, T>::operator();
1392
1393 /**
1394 * Number of rows. This function really makes only sense since we have a
1395 * two-dimensional object here.
1396 */
1397 size_type
1398 n_rows() const;
1399
1400 /**
1401 * Number of columns. This function really makes only sense since we have a
1402 * two-dimensional object here.
1403 */
1404 size_type
1405 n_cols() const;
1406
1407 /**
1408 * Return an iterator pointing to the first entry.
1409 */
1410 iterator
1411 begin();
1412
1413 /**
1414 * Return a constant iterator pointing to the first entry.
1415 */
1416 const_iterator
1417 begin() const;
1418
1419 /**
1420 * Return an iterator pointing to one past the last entry.
1421 */
1422 iterator
1423 end();
1424
1425 /**
1426 * Return a constant iterator pointing to one past the last entry.
1427 */
1428 const_iterator
1429 end() const;
1430
1431 protected:
1432 /**
1433 * Return a read-write reference to the element <tt>(i,j)</tt>.
1434 *
1435 * This function does no bounds checking and is only to be used internally
1436 * and in functions already checked.
1437 *
1438 * These functions are mainly here for compatibility with a former
1439 * implementation of these table classes for 2d arrays, then called
1440 * <tt>vector2d</tt>.
1441 */
1442 typename AlignedVector<T>::reference
1443 el(const size_type i, const size_type j);
1444
1445 /**
1446 * Return the value of the element <tt>(i,j)</tt> as a read-only reference.
1447 *
1448 * This function does no bounds checking and is only to be used internally
1449 * and in functions already checked.
1450 *
1451 * We return the requested value as a constant reference rather than by
1452 * value since this object may hold data types that may be large, and we
1453 * don't know here whether copying is expensive or not.
1454 *
1455 * These functions are mainly here for compatibility with a former
1456 * implementation of these table classes for 2d arrays, then called
1457 * <tt>vector2d</tt>.
1458 */
1459 typename AlignedVector<T>::const_reference
1460 el(const size_type i, const size_type j) const;
1461
1462 // Make the AccessorBase class a friend so that it may directly index into
1463 // the array.
1464 friend class MatrixTableIterators::
1465 AccessorBase<Table<2, T>, true, MatrixTableIterators::Storage::row_major>;
1466 friend class MatrixTableIterators::
1467 AccessorBase<Table<2, T>, false, MatrixTableIterators::Storage::row_major>;
1468
1469 // Make the mutable accessor class a friend so that we can write to array
1470 // entries with iterators.
1471 friend class MatrixTableIterators::
1472 Accessor<Table<2, T>, false, MatrixTableIterators::Storage::row_major>;
1473 };
1474
1475
1476
1477 /**
1478 * A class representing a three-dimensional table of objects (not necessarily
1479 * only numbers).
1480 * The majority of the interface of this class is implemented in the
1481 * TableBase base class.See there for an outline of the rationale for and
1482 * interface of this class.
1483 *
1484 * @ingroup data
1485 */
1486 template <typename T>
1487 class Table<3, T> : public TableBase<3, T>
1488 {
1489 public:
1490 /**
1491 * Integer type used to count the number of elements in this container.
1492 */
1493 using size_type = typename TableBase<3, T>::size_type;
1494
1495 /**
1496 * Default constructor. Set all dimensions to zero.
1497 */
1498 Table() = default;
1499
1500 /**
1501 * Constructor. Pass down the given dimensions to the base class.
1502 */
1503 Table(const size_type size1, const size_type size2, const size_type size3);
1504
1505 /**
1506 * Constructor. Create a table with a given size and initialize it from a
1507 * set of iterators.
1508 *
1509 * This function is entirely equivalent to creating a table <code>t</code>
1510 * of the given size and then calling
1511 * @code
1512 * t.fill (entries, C_style_indexing);
1513 * @endcode
1514 * on it, using the TableBase::fill() function where the arguments are
1515 * explained in more detail. The point, however, is that that is only
1516 * possible if the table can be changed after running the constructor,
1517 * whereas calling the current constructor allows sizing and initializing an
1518 * object right away so that it can be marked const.
1519 *
1520 * Using this constructor, you can do things like this (shown here for a
1521 * two-dimensional table, but the same works for the current class):
1522 * @code
1523 * const double values[] = { 1, 2, 3, 4, 5, 6 };
1524 * const Table<2,double> t(2, 3, entries, true);
1525 * @endcode
1526 * You can also initialize a table right from a file, using input iterators:
1527 * @code
1528 * std::ifstream input ("myfile");
1529 * const Table<2,double> t(2, 3,
1530 * std::istream_iterator<double>(input),
1531 * true);
1532 * @endcode
1533 *
1534 *
1535 * @param size1 The size of this table in the first dimension.
1536 * @param size2 The size of this table in the second dimension.
1537 * @param size3 The size of this table in the third dimension.
1538 * @param entries An iterator to a set of elements from which to initialize
1539 * this table. It is assumed that iterator can be incremented and
1540 * dereferenced a sufficient number of times to fill this table.
1541 * @param C_style_indexing If true, run over elements of the table with the
1542 * last index changing fastest as we dereference subsequent elements of the
1543 * input range. If false, change the first index fastest.
1544 */
1545 template <typename InputIterator>
1546 Table(const size_type size1,
1547 const size_type size2,
1548 const size_type size3,
1549 InputIterator entries,
1550 const bool C_style_indexing = true);
1551
1552 /**
1553 * Reinitialize the object. Passes down to the base class
1554 * by converting the arguments to the data type requested by the base class.
1555 */
1556 void
1557 reinit(const size_type size1,
1558 const size_type size2,
1559 const size_type size3,
1560 const bool omit_default_initialization = false);
1561
1562 using TableBase<3, T>::reinit;
1563
1564 /**
1565 * Access operator. Generate an object that accesses the requested
1566 * two-dimensional subobject of this three-dimensional table. Range checks are
1567 * performed.
1568 *
1569 * This version of the function only allows read access.
1570 */
1571 dealii::internal::TableBaseAccessors::Accessor<3, T, true, 2>
1572 operator[](const size_type i) const;
1573
1574 /**
1575 * Access operator. Generate an object that accesses the requested
1576 * two-dimensional subobject of this three-dimensional table. Range
1577 * checks are performed.
1578 *
1579 * This version of the function allows read-write access.
1580 */
1581 dealii::internal::TableBaseAccessors::Accessor<3, T, false, 2>
1582 operator[](const size_type i);
1583
1584 /**
1585 * Direct access to one element of the table by specifying all indices at
1586 * the same time. Range checks are performed.
1587 *
1588 * This version of the function only allows read access.
1589 */
1590 typename AlignedVector<T>::const_reference
1591 operator()(const size_type i, const size_type j, const size_type k) const;
1592
1593
1594 /**
1595 * Direct access to one element of the table by specifying all indices at
1596 * the same time. Range checks are performed.
1597 *
1598 * This version of the function allows read-write access.
1599 */
1600 typename AlignedVector<T>::reference
1601 operator()(const size_type i, const size_type j, const size_type k);
1602
1603 /**
1604 * Make the variations of `operator()` from the base class available.
1605 */
1606 using TableBase<3, T>::operator();
1607 };
1608
1609
1610
1611 /**
1612 * A class representing a four-dimensional table of objects (not necessarily
1613 * only numbers).
1614 * The majority of the interface of this class is implemented in the
1615 * TableBase base class. See there for an outline of the rationale for and
1616 * interface of this class.
1617 *
1618 * @ingroup data
1619 */
1620 template <typename T>
1621 class Table<4, T> : public TableBase<4, T>
1622 {
1623 public:
1624 /**
1625 * Integer type used to count the number of elements in this container.
1626 */
1627 using size_type = typename TableBase<4, T>::size_type;
1628
1629 /**
1630 * Default constructor. Set all dimensions to zero.
1631 */
1632 Table() = default;
1633
1634 /**
1635 * Constructor. Pass down the given dimensions to the base class.
1636 */
1637 Table(const size_type size1,
1638 const size_type size2,
1639 const size_type size3,
1640 const size_type size4);
1641
1642 /**
1643 * Access operator. Generate an object that accesses the requested
1644 * three-dimensional subobject of this four-dimensional table. Range checks
1645 * are performed.
1646 *
1647 * This version of the function only allows read access.
1648 */
1649 dealii::internal::TableBaseAccessors::Accessor<4, T, true, 3>
1650 operator[](const size_type i) const;
1651
1652 /**
1653 * Access operator. Generate an object that accesses the requested
1654 * three-dimensional subobject of this four-dimensional table. Range checks
1655 * are performed.
1656 *
1657 * This version of the function allows read-write access.
1658 */
1659 dealii::internal::TableBaseAccessors::Accessor<4, T, false, 3>
1660 operator[](const size_type i);
1661
1662 /**
1663 * Direct access to one element of the table by specifying all indices at
1664 * the same time. Range checks are performed.
1665 *
1666 * This version of the function only allows read access.
1667 */
1668 typename AlignedVector<T>::const_reference
1669 operator()(const size_type i,
1670 const size_type j,
1671 const size_type k,
1672 const size_type l) const;
1673
1674
1675 /**
1676 * Direct access to one element of the table by specifying all indices at
1677 * the same time. Range checks are performed.
1678 *
1679 * This version of the function allows read-write access.
1680 */
1681 typename AlignedVector<T>::reference
1682 operator()(const size_type i,
1683 const size_type j,
1684 const size_type k,
1685 const size_type l);
1686
1687 /**
1688 * Make the variations of `operator()` from the base class available.
1689 */
1690 using TableBase<4, T>::operator();
1691 };
1692
1693
1694
1695 /**
1696 * A class representing a five-dimensional table of objects (not necessarily
1697 * only numbers).
1698 * The majority of the interface of this class is implemented in the
1699 * TableBase base class. See there for an outline of the rationale for and
1700 * interface of this class.
1701 *
1702 * @ingroup data
1703 */
1704 template <typename T>
1705 class Table<5, T> : public TableBase<5, T>
1706 {
1707 public:
1708 /**
1709 * Integer type used to count the number of elements in this container.
1710 */
1711 using size_type = typename TableBase<5, T>::size_type;
1712
1713
1714 /**
1715 * Default constructor. Set all dimensions to zero.
1716 */
1717 Table() = default;
1718
1719 /**
1720 * Constructor. Pass down the given dimensions to the base class.
1721 */
1722 Table(const size_type size1,
1723 const size_type size2,
1724 const size_type size3,
1725 const size_type size4,
1726 const size_type size5);
1727
1728 /**
1729 * Access operator. Generate an object that accesses the requested
1730 * four-dimensional subobject of this five-dimensional table. Range checks are
1731 * performed.
1732 *
1733 * This version of the function only allows read access.
1734 */
1735 dealii::internal::TableBaseAccessors::Accessor<5, T, true, 4>
1736 operator[](const size_type i) const;
1737
1738 /**
1739 * Access operator. Generate an object that accesses the requested
1740 * four-dimensional subobject of this five-dimensional table. Range checks are
1741 * performed.
1742 *
1743 * This version of the function allows read-write access.
1744 */
1745 dealii::internal::TableBaseAccessors::Accessor<5, T, false, 4>
1746 operator[](const size_type i);
1747
1748 /**
1749 * Direct access to one element of the table by specifying all indices at
1750 * the same time. Range checks are performed.
1751 *
1752 * This version of the function only allows read access.
1753 */
1754 typename AlignedVector<T>::const_reference
1755 operator()(const size_type i,
1756 const size_type j,
1757 const size_type k,
1758 const size_type l,
1759 const size_type m) const;
1760
1761 /**
1762 * Direct access to one element of the table by specifying all indices at
1763 * the same time. Range checks are performed.
1764 *
1765 * This version of the function allows read-write access.
1766 */
1767 typename AlignedVector<T>::reference
1768 operator()(const size_type i,
1769 const size_type j,
1770 const size_type k,
1771 const size_type l,
1772 const size_type m);
1773
1774 /**
1775 * Make the variations of `operator()` from the base class available.
1776 */
1777 using TableBase<5, T>::operator();
1778 };
1779
1780
1781
1782 /**
1783 * A class representing a six-dimensional table of objects (not necessarily
1784 * only numbers).
1785 * The majority of the interface of this class is implemented in the
1786 * TableBase base class. See there for an outline of the rationale for and
1787 * interface of this class.
1788 *
1789 * @ingroup data
1790 */
1791 template <typename T>
1792 class Table<6, T> : public TableBase<6, T>
1793 {
1794 public:
1795 /**
1796 * Integer type used to count the number of elements in this container.
1797 */
1798 using size_type = typename TableBase<6, T>::size_type;
1799
1800 /**
1801 * Default constructor. Set all dimensions to zero.
1802 */
1803 Table() = default;
1804
1805 /**
1806 * Constructor. Pass down the given dimensions to the base class.
1807 */
1808 Table(const size_type size1,
1809 const size_type size2,
1810 const size_type size3,
1811 const size_type size4,
1812 const size_type size5,
1813 const size_type size6);
1814
1815 /**
1816 * Access operator. Generate an object that accesses the requested
1817 * five-dimensional subobject of this six-dimensional table. Range checks are
1818 * performed.
1819 *
1820 * This version of the function only allows read access.
1821 */
1822 dealii::internal::TableBaseAccessors::Accessor<6, T, true, 5>
1823 operator[](const size_type i) const;
1824
1825 /**
1826 * Access operator. Generate an object that accesses the requested
1827 * five-dimensional subobject of this six-dimensional table. Range checks are
1828 * performed.
1829 *
1830 * This version of the function allows read-write access.
1831 */
1832 dealii::internal::TableBaseAccessors::Accessor<6, T, false, 5>
1833 operator[](const size_type i);
1834
1835 /**
1836 * Direct access to one element of the table by specifying all indices at
1837 * the same time. Range checks are performed.
1838 *
1839 * This version of the function only allows read access.
1840 */
1841 typename AlignedVector<T>::const_reference
1842 operator()(const size_type i,
1843 const size_type j,
1844 const size_type k,
1845 const size_type l,
1846 const size_type m,
1847 const size_type n) const;
1848
1849 /**
1850 * Direct access to one element of the table by specifying all indices at
1851 * the same time. Range checks are performed.
1852 *
1853 * This version of the function allows read-write access.
1854 */
1855 typename AlignedVector<T>::reference
1856 operator()(const size_type i,
1857 const size_type j,
1858 const size_type k,
1859 const size_type l,
1860 const size_type m,
1861 const size_type n);
1862
1863 /**
1864 * Make the variations of `operator()` from the base class available.
1865 */
1866 using TableBase<6, T>::operator();
1867 };
1868
1869
1870 /**
1871 * A class representing a seven-dimensional table of objects (not necessarily
1872 * only numbers).
1873 * The majority of the interface of this class is implemented in the
1874 * TableBase base class. See there for an outline of the rationale for and
1875 * interface of this class.
1876 *
1877 * @ingroup data
1878 */
1879 template <typename T>
1880 class Table<7, T> : public TableBase<7, T>
1881 {
1882 public:
1883 /**
1884 * Integer type used to count the number of elements in this container.
1885 */
1886 using size_type = typename TableBase<7, T>::size_type;
1887
1888 /**
1889 * Default constructor. Set all dimensions to zero.
1890 */
1891 Table() = default;
1892
1893 /**
1894 * Constructor. Pass down the given dimensions to the base class.
1895 */
1896 Table(const size_type size1,
1897 const size_type size2,
1898 const size_type size3,
1899 const size_type size4,
1900 const size_type size5,
1901 const size_type size6,
1902 const size_type size7);
1903
1904 /**
1905 * Access operator. Generate an object that accesses the requested
1906 * six-dimensional subobject of this seven-dimensional table. Range checks are
1907 * performed.
1908 *
1909 * This version of the function only allows read access.
1910 */
1911 dealii::internal::TableBaseAccessors::Accessor<7, T, true, 6>
1912 operator[](const size_type i) const;
1913
1914 /**
1915 * Access operator. Generate an object that accesses the requested
1916 * six-dimensional subobject of this seven-dimensional table. Range checks are
1917 * performed.
1918 *
1919 * This version of the function allows read-write access.
1920 */
1921 dealii::internal::TableBaseAccessors::Accessor<7, T, false, 6>
1922 operator[](const size_type i);
1923
1924 /**
1925 * Direct access to one element of the table by specifying all indices at
1926 * the same time. Range checks are performed.
1927 *
1928 * This version of the function only allows read access.
1929 */
1930 typename AlignedVector<T>::const_reference
1931 operator()(const size_type i,
1932 const size_type j,
1933 const size_type k,
1934 const size_type l,
1935 const size_type m,
1936 const size_type n,
1937 const size_type o) const;
1938
1939 /**
1940 * Direct access to one element of the table by specifying all indices at
1941 * the same time. Range checks are performed.
1942 *
1943 * This version of the function allows read-write access.
1944 */
1945 typename AlignedVector<T>::reference
1946 operator()(const size_type i,
1947 const size_type j,
1948 const size_type k,
1949 const size_type l,
1950 const size_type m,
1951 const size_type n,
1952 const size_type o);
1953
1954 /**
1955 * Make the variations of `operator()` from the base class available.
1956 */
1957 using TableBase<7, T>::operator();
1958 };
1959
1960
1961 /**
1962 * A class representing a transpose two-dimensional table, i.e. a matrix of
1963 * objects (not necessarily only numbers) in column first numbering (FORTRAN
1964 * convention). The only real difference is therefore really in the storage
1965 * format.
1966 *
1967 * This class copies the functions of Table<2,T>, but the element access and
1968 * the dimensions will be for the transpose ordering of the data field in
1969 * TableBase.
1970 *
1971 * @ingroup data
1972 */
1973 template <typename T>
1974 class TransposeTable : public TableBase<2, T>
1975 {
1976 public:
1977 /**
1978 * Integer type used to count the number of elements in this container.
1979 */
1980 using size_type = typename TableBase<2, T>::size_type;
1981
1982 /**
1983 * Typedef for the values in the table.
1984 */
1985 using value_type = typename AlignedVector<T>::value_type;
1986
1987 /**
1988 * Typedef for the references in the table.
1989 */
1990 using reference = typename AlignedVector<T>::reference;
1991
1992 /**
1993 * Typedef for the constant references in the table.
1994 */
1995 using const_reference = typename AlignedVector<T>::const_reference;
1996
1997 /**
1998 * Typedef for a constant iterator that traverses the table in column-major
1999 * order.
2000 */
2001 using const_iterator =
2002 MatrixTableIterators::Iterator<TransposeTable<T>,
2003 true,
2004 MatrixTableIterators::Storage::column_major>;
2005
2006 /**
2007 * Typedef for an iterator that traverses the table in column-major order.
2008 */
2009 using iterator =
2010 MatrixTableIterators::Iterator<TransposeTable<T>,
2011 false,
2012 MatrixTableIterators::Storage::column_major>;
2013
2014 /**
2015 * Default constructor. Set all dimensions to zero.
2016 */
2017 TransposeTable() = default;
2018
2019 /**
2020 * Constructor. Pass down the given dimensions to the base class.
2021 */
2022 TransposeTable(const size_type size1, const size_type size2);
2023
2024 /**
2025 * Reinitialize the object. This function is mostly here for compatibility
2026 * with the earlier <tt>vector2d</tt> class. Passes down to the base class
2027 * by converting the arguments to the data type requested by the base class.
2028 */
2029 void
2030 reinit(const size_type size1,
2031 const size_type size2,
2032 const bool omit_default_initialization = false);
2033
2034 /**
2035 * Direct access to one element of the table by specifying all indices at
2036 * the same time. Range checks are performed.
2037 *
2038 * This version of the function only allows read access.
2039 */
2040 const_reference
2041 operator()(const size_type i, const size_type j) const;
2042
2043 /**
2044 * Direct access to one element of the table by specifying all indices at
2045 * the same time. Range checks are performed.
2046 *
2047 * This version of the function allows read-write access.
2048 */
2049 reference
2050 operator()(const size_type i, const size_type j);
2051
2052 /**
2053 * Number of rows. This function really makes only sense since we have a
2054 * two-dimensional object here.
2055 */
2056 size_type
2057 n_rows() const;
2058
2059 /**
2060 * Number of columns. This function really makes only sense since we have a
2061 * two-dimensional object here.
2062 */
2063 size_type
2064 n_cols() const;
2065
2066 /**
2067 * Return an iterator pointing to the first entry.
2068 */
2069 iterator
2070 begin();
2071
2072 /**
2073 * Return a constant iterator pointing to the first entry.
2074 */
2075 const_iterator
2076 begin() const;
2077
2078 /**
2079 * Return an iterator pointing to one past the last entry.
2080 */
2081 iterator
2082 end();
2083
2084 /**
2085 * Return a constant iterator pointing to one past the last entry.
2086 */
2087 const_iterator
2088 end() const;
2089
2090 protected:
2091 /**
2092 * Return a read-write reference to the element <tt>(i,j)</tt>.
2093 *
2094 * This function does no bounds checking and is only to be used internally
2095 * and in functions already checked.
2096 *
2097 * These functions are mainly here for compatibility with a former
2098 * implementation of these table classes for 2d arrays, then called
2099 * <tt>vector2d</tt>.
2100 */
2101 reference
2102 el(const size_type i, const size_type j);
2103
2104 /**
2105 * Return the value of the element <tt>(i,j)</tt> as a read-only reference.
2106 *
2107 * This function does no bounds checking and is only to be used internally
2108 * and in functions already checked.
2109 *
2110 * We return the requested value as a constant reference rather than by
2111 * value since this object may hold data types that may be large, and we
2112 * don't know here whether copying is expensive or not.
2113 *
2114 * These functions are mainly here for compatibility with a former
2115 * implementation of these table classes for 2d arrays, then called
2116 * <tt>vector2d</tt>.
2117 */
2118 const_reference
2119 el(const size_type i, const size_type j) const;
2120
2121 // Make the AccessorBase class a friend so that it may directly index into
2122 // the array.
2123 friend class MatrixTableIterators::AccessorBase<
2124 TransposeTable<T>,
2125 true,
2126 MatrixTableIterators::Storage::column_major>;
2127 friend class MatrixTableIterators::AccessorBase<
2128 TransposeTable<T>,
2129 false,
2130 MatrixTableIterators::Storage::column_major>;
2131
2132 // Make the mutable accessor class a friend so that we can write to array
2133 // entries with iterators.
2134 friend class MatrixTableIterators::Accessor<
2135 TransposeTable<T>,
2136 false,
2137 MatrixTableIterators::Storage::column_major>;
2138 };
2139
2140
2141
2142 /* --------------------- Template and inline functions ---------------- */
2143
2144 #ifndef DOXYGEN
2145
2146 template <int N, typename T>
2147 TableBase<N, T>::TableBase(const TableIndices<N> &sizes)
2148 {
2149 reinit(sizes);
2150 }
2151
2152
2153
2154 template <int N, typename T>
2155 template <typename InputIterator>
2156 TableBase<N, T>::TableBase(const TableIndices<N> &sizes,
2157 InputIterator entries,
2158 const bool C_style_indexing)
2159 {
2160 reinit(sizes);
2161 fill(entries, C_style_indexing);
2162 }
2163
2164
2165
2166 template <int N, typename T>
2167 TableBase<N, T>::TableBase(const TableBase<N, T> &src)
2168 : EnableObserverPointer()
2169 , values(src.values)
2170 , table_size(src.table_size)
2171 {}
2172
2173
2174
2175 template <int N, typename T>
2176 template <typename T2>
2177 TableBase<N, T>::TableBase(const TableBase<N, T2> &src)
2178 {
2179 reinit(src.table_size);
2180 if (src.n_elements() != 0)
2181 std::copy(src.values.begin(), src.values.end(), values.begin());
2182 }
2183
2184
2185
2186 template <int N, typename T>
2187 TableBase<N, T>::TableBase(TableBase<N, T> &&src) noexcept
2188 : EnableObserverPointer(std::move(src))
2189 , values(std::move(src.values))
2190 , table_size(src.table_size)
2191 {
2192 src.table_size = TableIndices<N>();
2193 }
2194
2195
2196
2197 template <int N, typename T>
2198 template <class Archive>
2199 inline void
2200 TableBase<N, T>::serialize(Archive &ar, const unsigned int)
2201 {
2202 ar &static_cast<EnableObserverPointer &>(*this);
2203
2204 ar &values &table_size;
2205 }
2206
2207
2208
2209 namespace internal
2210 {
2211 namespace TableBaseAccessors
2212 {
2213 template <int N, typename T, bool C, unsigned int P>
2214 inline Accessor<N, T, C, P>::Accessor(const TableType &table,
2215 const iterator data)
2216 : table(table)
2217 , data(data)
2218 {}
2219
2220
2221
2222 template <int N, typename T, bool C, unsigned int P>
2223 inline Accessor<N, T, C, P>::Accessor(const Accessor &a)
2224 : table(a.table)
2225 , data(a.data)
2226 {}
2227
2228
2229
2230 template <int N, typename T, bool C, unsigned int P>
2231 inline Accessor<N, T, C, P - 1>
2232 Accessor<N, T, C, P>::operator[](const size_type i) const
2233 {
2234 AssertIndexRange(i, table.size()[N - P]);
2235
2236 // access i-th
2237 // subobject. optimize on the
2238 // case i==0
2239 if (i == 0)
2240 return Accessor<N, T, C, P - 1>(table, data);
2241 else
2242 {
2243 // note: P>1, otherwise the
2244 // specialization would have
2245 // been taken!
2246 size_type subobject_size = table.size()[N - 1];
2247 for (int p = P - 1; p > 1; --p)
2248 subobject_size *= table.size()[N - p];
2249 const iterator new_data = data + i * subobject_size;
2250 return Accessor<N, T, C, P - 1>(table, new_data);
2251 }
2252 }
2253
2254
2255
2256 template <int N, typename T, bool C>
2257 inline Accessor<N, T, C, 1>::Accessor(const TableType &table,
2258 const iterator data)
2259 : table(table)
2260 , data(data)
2261 {}
2262
2263
2264
2265 template <int N, typename T, bool C>
2266 inline Accessor<N, T, C, 1>::Accessor(const Accessor &a)
2267 : table(a.table)
2268 , data(a.data)
2269 {}
2270
2271
2272
2273 template <int N, typename T, bool C>
2274 inline typename Accessor<N, T, C, 1>::reference
2275 Accessor<N, T, C, 1>::operator[](const size_type i) const
2276 {
2277 AssertIndexRange(i, table.size()[N - 1]);
2278 return *(data + i);
2279 }
2280
2281
2282
2283 template <int N, typename T, bool C>
2284 inline typename Accessor<N, T, C, 1>::size_type
2285 Accessor<N, T, C, 1>::size() const
2286 {
2287 return table.size()[N - 1];
2288 }
2289
2290
2291
2292 template <int N, typename T, bool C>
2293 inline typename Accessor<N, T, C, 1>::iterator
2294 Accessor<N, T, C, 1>::begin() const
2295 {
2296 return data;
2297 }
2298
2299
2300
2301 template <int N, typename T, bool C>
2302 inline typename Accessor<N, T, C, 1>::iterator
2303 Accessor<N, T, C, 1>::end() const
2304 {
2305 return data + table.size()[N - 1];
2306 }
2307 } // namespace TableBaseAccessors
2308 } // namespace internal
2309
2310
2311
2312 template <int N, typename T>
2313 inline TableBase<N, T> &
2314 TableBase<N, T>::operator=(const TableBase<N, T> &m)
2315 {
2316 if (!m.empty())
2317 values = m.values;
2318 reinit(m.size(), true);
2319
2320 return *this;
2321 }
2322
2323
2324
2325 template <int N, typename T>
2326 template <typename T2>
2327 inline TableBase<N, T> &
2328 TableBase<N, T>::operator=(const TableBase<N, T2> &m)
2329 {
2330 reinit(m.size(), true);
2331 if (!empty())
2332 std::copy(m.values.begin(),
2333 m.values.begin() + n_elements(),
2334 values.begin());
2335
2336 return *this;
2337 }
2338
2339
2340
2341 template <int N, typename T>
2342 inline TableBase<N, T> &
2343 TableBase<N, T>::operator=(TableBase<N, T> &&m) noexcept
2344 {
2345 static_cast<EnableObserverPointer &>(*this) =
2346 std::move(static_cast<EnableObserverPointer &>(m));
2347 values = std::move(m.values);
2348 table_size = m.table_size;
2349 m.table_size = TableIndices<N>();
2350
2351 return *this;
2352 }
2353
2354
2355
2356 template <int N, typename T>
2357 inline bool
2358 TableBase<N, T>::operator==(const TableBase<N, T> &T2) const
2359 {
2360 return (values == T2.values);
2361 }
2362
2363
2364
2365 template <int N, typename T>
2366 inline void
2367 TableBase<N, T>::reset_values()
2368 {
2369 // use parallel set operation
2370 if (n_elements() != 0)
2371 values.fill(T());
2372 }
2373
2374
2375
2376 template <int N, typename T>
2377 inline void
2378 TableBase<N, T>::fill(const T &value)
2379 {
2380 if (n_elements() != 0)
2381 values.fill(value);
2382 }
2383
2384
2385
2386 template <int N, typename T>
2387 inline void
2388 TableBase<N, T>::replicate_across_communicator(const MPI_Comm communicator,
2389 const unsigned int root_process)
2390 {
2391 // Replicate first the actual data, then also exchange the
2392 // extents of the table
2393 values.replicate_across_communicator(communicator, root_process);
2394
2395 table_size =
2396 Utilities::MPI::broadcast(communicator, table_size, root_process);
2397 }
2398
2399
2400
2401 template <int N, typename T>
2402 inline void
2403 TableBase<N, T>::reinit(const TableIndices<N> &new_sizes,
2404 const bool omit_default_initialization)
2405 {
2406 table_size = new_sizes;
2407
2408 const size_type new_size = n_elements();
2409
2410 // if zero size was given: free all memory
2411 if (new_size == 0)
2412 {
2413 values.resize(0);
2414 // set all sizes to zero, even
2415 // if one was previously
2416 // nonzero. This simplifies
2417 // some assertions.
2418 table_size = TableIndices<N>();
2419
2420 return;
2421 }
2422
2423 // adjust values field. If it was empty before, we can simply call resize(),
2424 // which can set all the data fields. Otherwise, select the fast resize and
2425 // manually fill in all the elements as required by the design of this
2426 // class. (Selecting another code for the empty case ensures that we touch
2427 // the memory only once for non-trivial classes that need to initialize the
2428 // memory also in resize_fast.)
2429 if (!omit_default_initialization)
2430 {
2431 if (values.empty())
2432 values.resize(new_size);
2433 else
2434 {
2435 values.resize_fast(new_size);
2436 values.fill();
2437 }
2438 }
2439 else
2440 values.resize_fast(new_size);
2441 }
2442
2443
2444
2445 template <int N, typename T>
2446 inline void
2447 TableBase<N, T>::clear()
2448 {
2449 values.clear();
2450 table_size = TableIndices<N>();
2451 }
2452
2453
2454
2455 template <int N, typename T>
2456 inline const TableIndices<N> &
2457 TableBase<N, T>::size() const
2458 {
2459 return table_size;
2460 }
2461
2462
2463
2464 template <int N, typename T>
2465 inline typename TableBase<N, T>::size_type
2466 TableBase<N, T>::size(const unsigned int i) const
2467 {
2468 AssertIndexRange(i, N);
2469 return table_size[i];
2470 }
2471
2472
2473
2474 template <int N, typename T>
2475 inline typename TableBase<N, T>::size_type
2476 TableBase<N, T>::n_elements() const
2477 {
2478 size_type s = 1;
2479 for (unsigned int n = 0; n < N; ++n)
2480 s *= table_size[n];
2481 return s;
2482 }
2483
2484
2485
2486 template <int N, typename T>
2487 inline bool
2488 TableBase<N, T>::empty() const
2489 {
2490 return (n_elements() == 0);
2491 }
2492
2493
2494
2495 namespace internal
2496 {
2497 namespace TableImplementation
2498 {
2499 template <typename InputIterator, typename T>
2500 void
2501 fill_Fortran_style(InputIterator entries, TableBase<1, T> &table)
2502 {
2503 using size_type = typename TableBase<1, T>::size_type;
2504 for (size_type i = 0; i < table.size()[0]; ++i)
2505 table(TableIndices<1>(i)) = *entries++;
2506 }
2507
2508
2509 template <typename InputIterator, typename T>
2510 void
2511 fill_Fortran_style(InputIterator entries, TableBase<2, T> &table)
2512 {
2513 using size_type = typename TableBase<2, T>::size_type;
2514 for (size_type j = 0; j < table.size()[1]; ++j)
2515 for (size_type i = 0; i < table.size()[0]; ++i)
2516 table(TableIndices<2>(i, j)) = *entries++;
2517 }
2518
2519
2520 template <typename InputIterator, typename T>
2521 void
2522 fill_Fortran_style(InputIterator entries, TableBase<3, T> &table)
2523 {
2524 using size_type = typename TableBase<3, T>::size_type;
2525 for (size_type k = 0; k < table.size()[2]; ++k)
2526 for (size_type j = 0; j < table.size()[1]; ++j)
2527 for (size_type i = 0; i < table.size()[0]; ++i)
2528 table(TableIndices<3>(i, j, k)) = *entries++;
2529 }
2530
2531
2532 template <typename InputIterator, typename T, int N>
2533 void
2534 fill_Fortran_style(InputIterator, TableBase<N, T> &)
2535 {
2536 DEAL_II_NOT_IMPLEMENTED();
2537 }
2538 } // namespace TableImplementation
2539 } // namespace internal
2540
2541
2542 template <int N, typename T>
2543 template <typename InputIterator>
2544 inline void
2545 TableBase<N, T>::fill(InputIterator entries, const bool C_style_indexing)
2546 {
2547 Assert(n_elements() != 0, ExcMessage("Trying to fill an empty matrix."));
2548
2549 if (C_style_indexing)
2550 for (typename AlignedVector<T>::iterator p = values.begin();
2551 p != values.end();
2552 ++p)
2553 *p = *entries++;
2554 else
2555 internal::TableImplementation::fill_Fortran_style(entries, *this);
2556 }
2557
2558
2559
2560 template <int N, typename T>
2561 inline void
2562 TableBase<N, T>::swap(TableBase<N, T> &v) noexcept
2563 {
2564 values.swap(v.values);
2565 std::swap(table_size, v.table_size);
2566 }
2567
2568
2569
2570 template <int N, typename T>
2571 inline std::size_t
2572 TableBase<N, T>::memory_consumption() const
2573 {
2574 return sizeof(*this) + MemoryConsumption::memory_consumption(values);
2575 }
2576
2577
2578
2579 template <int N, typename T>
2580 inline typename TableBase<N, T>::size_type
2581 TableBase<N, T>::position(const TableIndices<N> &indices) const
2582 {
2583 // specialize this for the
2584 // different numbers of dimensions,
2585 // to make the job somewhat easier
2586 // for the compiler. have the
2587 // general formula nevertheless:
2588 switch (N)
2589 {
2590 case 1:
2591 return indices[0];
2592 case 2:
2593 return size_type(indices[0]) * table_size[1] + indices[1];
2594 case 3:
2595 return ((size_type(indices[0]) * table_size[1] + indices[1]) *
2596 table_size[2] +
2597 indices[2]);
2598 default:
2599 {
2600 unsigned int s = indices[0];
2601 for (unsigned int n = 1; n < N; ++n)
2602 s = s * table_size[n] + indices[n];
2603 return s;
2604 }
2605 }
2606 }
2607
2608
2609
2610 template <int N, typename T>
2611 inline typename AlignedVector<T>::const_reference
2612 TableBase<N, T>::operator()(const TableIndices<N> &indices) const
2613 {
2614 for (unsigned int n = 0; n < N; ++n)
2615 AssertIndexRange(indices[n], table_size[n]);
2616 return el(indices);
2617 }
2618
2619
2620
2621 template <int N, typename T>
2622 inline typename AlignedVector<T>::reference
2623 TableBase<N, T>::operator()(const TableIndices<N> &indices)
2624 {
2625 for (unsigned int n = 0; n < N; ++n)
2626 AssertIndexRange(indices[n], table_size[n]);
2627 return el(indices);
2628 }
2629
2630
2631
2632 template <int N, typename T>
2633 inline typename AlignedVector<T>::const_reference
2634 TableBase<N, T>::el(const TableIndices<N> &indices) const
2635 {
2636 return values[position(indices)];
2637 }
2638
2639
2640
2641 template <int N, typename T>
2642 inline typename AlignedVector<T>::reference
2643 TableBase<N, T>::el(const TableIndices<N> &indices)
2644 {
2645 AssertIndexRange(position(indices), values.size());
2646 return values[position(indices)];
2647 }
2648
2649
2650
2651 template <typename T>
2652 inline Table<1, T>::Table(const size_type size)
2653 : TableBase<1, T>(TableIndices<1>(size))
2654 {}
2655
2656
2657
2658 template <typename T>
2659 template <typename InputIterator>
2660 inline Table<1, T>::Table(const size_type size,
2661 InputIterator entries,
2662 const bool C_style_indexing)
2663 : TableBase<1, T>(TableIndices<1>(size), entries, C_style_indexing)
2664 {}
2665
2666
2667
2668 template <typename T>
2669 inline typename AlignedVector<T>::const_reference
2670 Table<1, T>::operator[](const size_type i) const
2671 {
2672 AssertIndexRange(i, this->table_size[0]);
2673 return this->values[i];
2674 }
2675
2676
2677
2678 template <typename T>
2679 inline typename AlignedVector<T>::reference
2680 Table<1, T>::operator[](const size_type i)
2681 {
2682 AssertIndexRange(i, this->table_size[0]);
2683 return this->values[i];
2684 }
2685
2686
2687
2688 template <typename T>
2689 inline typename AlignedVector<T>::const_reference
2690 Table<1, T>::operator()(const size_type i) const
2691 {
2692 AssertIndexRange(i, this->table_size[0]);
2693 return this->values[i];
2694 }
2695
2696
2697
2698 template <typename T>
2699 inline typename AlignedVector<T>::reference
2700 Table<1, T>::operator()(const size_type i)
2701 {
2702 AssertIndexRange(i, this->table_size[0]);
2703 return this->values[i];
2704 }
2705
2706
2707 //---------------------------------------------------------------------------
2708
2709
2710
2711 template <typename T>
2712 inline Table<2, T>::Table(const size_type size1, const size_type size2)
2713 : TableBase<2, T>(TableIndices<2>(size1, size2))
2714 {}
2715
2716
2717
2718 template <typename T>
2719 template <typename InputIterator>
2720 inline Table<2, T>::Table(const size_type size1,
2721 const size_type size2,
2722 InputIterator entries,
2723 const bool C_style_indexing)
2724 : TableBase<2, T>(TableIndices<2>(size1, size2), entries, C_style_indexing)
2725 {}
2726
2727
2728
2729 template <typename T>
2730 inline void
2731 Table<2, T>::reinit(const size_type size1,
2732 const size_type size2,
2733 const bool omit_default_initialization)
2734 {
2735 this->TableBase<2, T>::reinit(TableIndices<2>(size1, size2),
2736 omit_default_initialization);
2737 }
2738
2739
2740
2741 template <typename T>
2742 inline dealii::internal::TableBaseAccessors::Accessor<2, T, true, 1>
2743 Table<2, T>::operator[](const size_type i) const
2744 {
2745 AssertIndexRange(i, this->table_size[0]);
2746 return dealii::internal::TableBaseAccessors::Accessor<2, T, true, 1>(
2747 *this, this->values.begin() + size_type(i) * n_cols());
2748 }
2749
2750
2751
2752 template <typename T>
2753 inline dealii::internal::TableBaseAccessors::Accessor<2, T, false, 1>
2754 Table<2, T>::operator[](const size_type i)
2755 {
2756 AssertIndexRange(i, this->table_size[0]);
2757 return dealii::internal::TableBaseAccessors::Accessor<2, T, false, 1>(
2758 *this, this->values.begin() + size_type(i) * n_cols());
2759 }
2760
2761
2762
2763 template <typename T>
2764 inline typename AlignedVector<T>::const_reference
2765 Table<2, T>::operator()(const size_type i, const size_type j) const
2766 {
2767 AssertIndexRange(i, this->table_size[0]);
2768 AssertIndexRange(j, this->table_size[1]);
2769 return this->values[size_type(i) * this->table_size[1] + j];
2770 }
2771
2772
2773
2774 template <typename T>
2775 inline typename AlignedVector<T>::reference
2776 Table<2, T>::operator()(const size_type i, const size_type j)
2777 {
2778 AssertIndexRange(i, this->table_size[0]);
2779 AssertIndexRange(j, this->table_size[1]);
2780 return this->values[size_type(i) * this->table_size[1] + j];
2781 }
2782
2783
2784
2785 template <typename T>
2786 inline typename AlignedVector<T>::const_reference
2787 Table<2, T>::el(const size_type i, const size_type j) const
2788 {
2789 return this->values[size_type(i) * this->table_size[1] + j];
2790 }
2791
2792
2793
2794 template <typename T>
2795 inline typename AlignedVector<T>::reference
2796 Table<2, T>::el(const size_type i, const size_type j)
2797 {
2798 return this->values[size_type(i) * this->table_size[1] + j];
2799 }
2800
2801
2802
2803 template <typename T>
2804 inline typename Table<2, T>::size_type
2805 Table<2, T>::n_rows() const
2806 {
2807 return this->table_size[0];
2808 }
2809
2810
2811
2812 template <typename T>
2813 inline typename Table<2, T>::size_type
2814 Table<2, T>::n_cols() const
2815 {
2816 return this->table_size[1];
2817 }
2818
2819
2820
2821 template <typename T>
2822 inline typename Table<2, T>::iterator
2823 Table<2, T>::begin()
2824 {
2825 return typename Table<2, T>::iterator(this, 0, 0);
2826 }
2827
2828
2829
2830 template <typename T>
2831 inline typename Table<2, T>::const_iterator
2832 Table<2, T>::begin() const
2833 {
2834 return typename Table<2, T>::const_iterator(this, 0, 0);
2835 }
2836
2837
2838
2839 template <typename T>
2840 inline typename Table<2, T>::iterator
2841 Table<2, T>::end()
2842 {
2843 return typename Table<2, T>::iterator(this);
2844 }
2845
2846
2847
2848 template <typename T>
2849 inline typename Table<2, T>::const_iterator
2850 Table<2, T>::end() const
2851 {
2852 return typename Table<2, T>::const_iterator(this);
2853 }
2854
2855
2856
2857 //---------------------------------------------------------------------------
2858 namespace MatrixTableIterators
2859 {
2860 namespace internal
2861 {
2862 // Internal calculation routines for AccessorBase. These do not do any
2863 // checking whatsoever.
2864 template <typename TableType, Storage storage_order>
2865 inline std::ptrdiff_t
2866 get_row_index(const std::ptrdiff_t linear_index,
2867 const TableType *const container)
2868 {
2869 switch (storage_order)
2870 {
2871 case Storage::row_major:
2872 return linear_index / container->n_cols();
2873 case Storage::column_major:
2874 return linear_index % container->n_rows();
2875 default:
2876 DEAL_II_ASSERT_UNREACHABLE();
2877 }
2878 return {};
2879 }
2880
2881
2882
2883 template <typename TableType, Storage storage_order>
2884 inline std::ptrdiff_t
2885 get_column_index(const std::ptrdiff_t linear_index,
2886 const TableType *const container)
2887 {
2888 switch (storage_order)
2889 {
2890 case Storage::row_major:
2891 return linear_index % container->n_cols();
2892 case Storage::column_major:
2893 return linear_index / container->n_rows();
2894 default:
2895 DEAL_II_ASSERT_UNREACHABLE();
2896 }
2897 return {};
2898 }
2899 } // namespace internal
2900
2901
2902
2903 template <typename TableType, bool Constness, Storage storage_order>
2904 inline AccessorBase<TableType, Constness, storage_order>::AccessorBase()
2905 : container(nullptr)
2906 , linear_index(std::numeric_limits<decltype(linear_index)>::max())
2907 {}
2908
2909
2910
2911 template <typename TableType, bool Constness, Storage storage_order>
2912 inline AccessorBase<TableType, Constness, storage_order>::AccessorBase(
2913 const container_pointer_type table)
2914 : container(table)
2915 , linear_index(container->values.size())
2916 {}
2917
2918
2919
2920 template <typename TableType, bool Constness, Storage storage_order>
2921 inline AccessorBase<TableType, Constness, storage_order>::AccessorBase(
2922 const AccessorBase<TableType, false, storage_order> &a)
2923 : container(a.container)
2924 , linear_index(a.linear_index)
2925 {}
2926
2927
2928
2929 template <typename TableType, bool Constness, Storage storage_order>
2930 inline AccessorBase<TableType, Constness, storage_order>::AccessorBase(
2931 const container_pointer_type table,
2932 const std::ptrdiff_t index)
2933 : container(table)
2934 , linear_index(index)
2935 {
2936 Assert(0 <= linear_index &&
2937 std::size_t(linear_index) < container->values.size() + 1,
2938 ExcMessage("The current iterator points outside of the table and is "
2939 "not the end iterator."));
2940 }
2941
2942
2943
2944 template <typename TableType, bool Constness, Storage storage_order>
2945 inline const typename AccessorBase<TableType, Constness, storage_order>::
2946 value_type &
2947 AccessorBase<TableType, Constness, storage_order>::value() const
2948 {
2949 assert_valid_linear_index();
2950 return this->container->values[linear_index];
2951 }
2952
2953
2954
2955 template <typename TableType, bool Constness, Storage storage_order>
2956 inline AccessorBase<TableType, Constness, storage_order>::
2957 operator const value_type &() const
2958 {
2959 assert_valid_linear_index();
2960 return this->container->values[linear_index];
2961 }
2962
2963
2964
2965 template <typename TableType, bool Constness, Storage storage_order>
2966 inline typename AccessorBase<TableType, Constness, storage_order>::size_type
2967 AccessorBase<TableType, Constness, storage_order>::row() const
2968 {
2969 assert_valid_linear_index();
2970 return static_cast<std::size_t>(
2971 internal::get_row_index<TableType, storage_order>(linear_index,
2972 container));
2973 }
2974
2975
2976
2977 template <typename TableType, bool Constness, Storage storage_order>
2978 inline typename AccessorBase<TableType, Constness, storage_order>::size_type
2979 AccessorBase<TableType, Constness, storage_order>::column() const
2980 {
2981 assert_valid_linear_index();
2982 return static_cast<std::size_t>(
2983 internal::get_column_index<TableType, storage_order>(linear_index,
2984 container));
2985 }
2986
2987
2988
2989 template <typename TableType, bool Constness, Storage storage_order>
2990 inline void
2991 AccessorBase<TableType, Constness, storage_order>::assert_valid_linear_index()
2992 const
2993 {
2994 # ifdef DEBUG // avoid unused variable warnings by guarding everything
2995 Assert(container != nullptr,
2996 ExcMessage("This accessor has been default-constructed and does not "
2997 "have a corresponding table."));
2998 Assert(!container->empty(),
2999 ExcMessage("An empty table has no rows or columns."));
3000 Assert(0 <= linear_index &&
3001 std::size_t(linear_index) < container->values.size(),
3002 ExcMessage("The current iterator points outside of the table."));
3003 const std::ptrdiff_t row_n =
3004 internal::get_row_index<TableType, storage_order>(linear_index,
3005 container);
3006 const std::ptrdiff_t column_n =
3007 internal::get_column_index<TableType, storage_order>(linear_index,
3008 container);
3009 Assert(0 <= column_n && std::size_t(column_n) < container->n_cols(),
3010 ExcMessage("The current iterator points outside the table."));
3011 Assert(0 <= row_n && std::size_t(row_n) < container->n_rows(),
3012 ExcMessage("The current iterator points outside the table."));
3013 # endif
3014 }
3015
3016
3017
3018 template <typename TableType, Storage storage_order>
3019 inline const Accessor<TableType, false, storage_order> &
3020 Accessor<TableType, false, storage_order>::operator=(
3021 const typename Accessor<TableType, false, storage_order>::value_type &t)
3022 const
3023 {
3024 this->assert_valid_linear_index();
3025 this->container->values[this->linear_index] = t;
3026 return *this;
3027 }
3028
3029
3030
3031 template <typename TableType, Storage storage_order>
3032 inline const Accessor<TableType, false, storage_order> &
3033 Accessor<TableType, false, storage_order>::operator=(
3034 typename Accessor<TableType, false, storage_order>::value_type &&t) const
3035 {
3036 this->assert_valid_linear_index();
3037 this->container->values[this->linear_index] = t;
3038 return *this;
3039 }
3040
3041
3042
3043 template <typename TableType, Storage storage_order>
3044 inline typename Accessor<TableType, false, storage_order>::value_type &
3045 Accessor<TableType, false, storage_order>::value() const
3046 {
3047 this->assert_valid_linear_index();
3048 return this->container->values[this->linear_index];
3049 }
3050
3051
3052
3053 template <typename TableType, Storage storage_order>
3054 inline Accessor<TableType, false, storage_order>::operator value_type &()
3055 {
3056 this->assert_valid_linear_index();
3057 return this->container->values[this->linear_index];
3058 }
3059
3060
3061
3062 template <typename TableType, bool Constness, Storage storage_order>
3063 inline Iterator<TableType, Constness, storage_order>::Iterator(
3064 const Accessor<TableType, Constness, storage_order> &a)
3065 : LinearIndexIterator<Iterator<TableType, Constness, storage_order>,
3066 Accessor<TableType, Constness, storage_order>>(a)
3067 {}
3068
3069
3070
3071 template <typename TableType, bool Constness, Storage storage_order>
3072 inline Iterator<TableType, Constness, storage_order>::Iterator(
3073 const container_pointer_type table)
3074 : LinearIndexIterator<Iterator<TableType, Constness, storage_order>,
3075 Accessor<TableType, Constness, storage_order>>(
3076 Accessor<TableType, Constness, storage_order>(table))
3077 {}
3078
3079
3080
3081 template <typename TableType, bool Constness, Storage storage_order>
3082 inline Iterator<TableType, Constness, storage_order>::Iterator(
3083 const Iterator<TableType, false, storage_order> &i)
3084 : LinearIndexIterator<Iterator<TableType, Constness, storage_order>,
3085 Accessor<TableType, Constness, storage_order>>(*i)
3086 {}
3087
3088
3089
3090 template <typename TableType, bool Constness, Storage storage_order>
3091 inline Iterator<TableType, Constness, storage_order>::Iterator(
3092 const container_pointer_type table,
3093 const size_type row_n,
3094 const size_type col_n)
3095 : Iterator(table,
3096 storage_order == Storage::row_major ?
3097 table->n_cols() * row_n + col_n :
3098 table->n_rows() * col_n + row_n)
3099 {}
3100
3101
3102
3103 template <typename TableType, bool Constness, Storage storage_order>
3104 inline Iterator<TableType, Constness, storage_order>::Iterator(
3105 const container_pointer_type table,
3106 const std::ptrdiff_t linear_index)
3107 : LinearIndexIterator<Iterator<TableType, Constness, storage_order>,
3108 Accessor<TableType, Constness, storage_order>>(
3109 Accessor<TableType, Constness, storage_order>(table, linear_index))
3110 {}
3111 } // namespace MatrixTableIterators
3112
3113
3114
3115 //---------------------------------------------------------------------------
3116 template <typename T>
3117 inline TransposeTable<T>::TransposeTable(const size_type size1,
3118 const size_type size2)
3119 : TableBase<2, T>(TableIndices<2>(size2, size1))
3120 {}
3121
3122
3123
3124 template <typename T>
3125 inline void
3126 TransposeTable<T>::reinit(const size_type size1,
3127 const size_type size2,
3128 const bool omit_default_initialization)
3129 {
3130 this->TableBase<2, T>::reinit(TableIndices<2>(size2, size1),
3131 omit_default_initialization);
3132 }
3133
3134
3135
3136 template <typename T>
3137 inline typename TransposeTable<T>::const_reference
3138 TransposeTable<T>::operator()(const size_type i, const size_type j) const
3139 {
3140 AssertIndexRange(i, this->table_size[1]);
3141 AssertIndexRange(j, this->table_size[0]);
3142 return this->values[size_type(j) * this->table_size[1] + i];
3143 }
3144
3145
3146
3147 template <typename T>
3148 inline typename TransposeTable<T>::reference
3149 TransposeTable<T>::operator()(const size_type i, const size_type j)
3150 {
3151 AssertIndexRange(i, this->table_size[1]);
3152 AssertIndexRange(j, this->table_size[0]);
3153 return this->values[size_type(j) * this->table_size[1] + i];
3154 }
3155
3156
3157
3158 template <typename T>
3159 inline typename TransposeTable<T>::const_reference
3160 TransposeTable<T>::el(const size_type i, const size_type j) const
3161 {
3162 return this->values[size_type(j) * this->table_size[1] + i];
3163 }
3164
3165
3166
3167 template <typename T>
3168 inline typename TransposeTable<T>::reference
3169 TransposeTable<T>::el(const size_type i, const size_type j)
3170 {
3171 return this->values[size_type(j) * this->table_size[1] + i];
3172 }
3173
3174
3175
3176 template <typename T>
3177 inline typename TransposeTable<T>::size_type
3178 TransposeTable<T>::n_rows() const
3179 {
3180 return this->table_size[1];
3181 }
3182
3183
3184
3185 template <typename T>
3186 inline typename TransposeTable<T>::size_type
3187 TransposeTable<T>::n_cols() const
3188 {
3189 return this->table_size[0];
3190 }
3191
3192
3193
3194 template <typename T>
3195 inline typename TransposeTable<T>::iterator
3196 TransposeTable<T>::begin()
3197 {
3198 return typename TransposeTable<T>::iterator(this, 0, 0);
3199 }
3200
3201
3202
3203 template <typename T>
3204 inline typename TransposeTable<T>::const_iterator
3205 TransposeTable<T>::begin() const
3206 {
3207 return typename TransposeTable<T>::const_iterator(this, 0, 0);
3208 }
3209
3210
3211
3212 template <typename T>
3213 inline typename TransposeTable<T>::iterator
3214 TransposeTable<T>::end()
3215 {
3216 return typename TransposeTable<T>::iterator(this);
3217 }
3218
3219
3220
3221 template <typename T>
3222 inline typename TransposeTable<T>::const_iterator
3223 TransposeTable<T>::end() const
3224 {
3225 return typename TransposeTable<T>::const_iterator(this);
3226 }
3227 //---------------------------------------------------------------------------
3228
3229
3230
3231 template <typename T>
3232 inline Table<3, T>::Table(const size_type size1,
3233 const size_type size2,
3234 const size_type size3)
3235 : TableBase<3, T>(TableIndices<3>(size1, size2, size3))
3236 {}
3237
3238
3239
3240 template <typename T>
3241 template <typename InputIterator>
3242 inline Table<3, T>::Table(const size_type size1,
3243 const size_type size2,
3244 const size_type size3,
3245 InputIterator entries,
3246 const bool C_style_indexing)
3247 : TableBase<3, T>(TableIndices<3>(size1, size2, size3),
3248 entries,
3249 C_style_indexing)
3250 {}
3251
3252
3253
3254 template <typename T>
3255 inline void
3256 Table<3, T>::reinit(const size_type size1,
3257 const size_type size2,
3258 const size_type size3,
3259 const bool omit_default_initialization)
3260 {
3261 this->TableBase<3, T>::reinit(TableIndices<3>(size1, size2, size3),
3262 omit_default_initialization);
3263 }
3264
3265
3266
3267 template <typename T>
3268 inline dealii::internal::TableBaseAccessors::Accessor<3, T, true, 2>
3269 Table<3, T>::operator[](const size_type i) const
3270 {
3271 AssertIndexRange(i, this->table_size[0]);
3272 const size_type subobject_size =
3273 size_type(this->table_size[1]) * this->table_size[2];
3274 return (dealii::internal::TableBaseAccessors::Accessor<3, T, true, 2>(
3275 *this, this->values.begin() + i * subobject_size));
3276 }
3277
3278
3279
3280 template <typename T>
3281 inline dealii::internal::TableBaseAccessors::Accessor<3, T, false, 2>
3282 Table<3, T>::operator[](const size_type i)
3283 {
3284 AssertIndexRange(i, this->table_size[0]);
3285 const size_type subobject_size =
3286 size_type(this->table_size[1]) * this->table_size[2];
3287 return (dealii::internal::TableBaseAccessors::Accessor<3, T, false, 2>(
3288 *this, this->values.begin() + i * subobject_size));
3289 }
3290
3291
3292
3293 template <typename T>
3294 inline typename AlignedVector<T>::const_reference
3295 Table<3, T>::operator()(const size_type i,
3296 const size_type j,
3297 const size_type k) const
3298 {
3299 AssertIndexRange(i, this->table_size[0]);
3300 AssertIndexRange(j, this->table_size[1]);
3301 AssertIndexRange(k, this->table_size[2]);
3302 return this
3303 ->values[(size_type(i) * this->table_size[1] + j) * this->table_size[2] +
3304 k];
3305 }
3306
3307
3308
3309 template <typename T>
3310 inline typename AlignedVector<T>::reference
3311 Table<3, T>::operator()(const size_type i, const size_type j, const size_type k)
3312 {
3313 AssertIndexRange(i, this->table_size[0]);
3314 AssertIndexRange(j, this->table_size[1]);
3315 AssertIndexRange(k, this->table_size[2]);
3316 return this
3317 ->values[(size_type(i) * this->table_size[1] + j) * this->table_size[2] +
3318 k];
3319 }
3320
3321
3322
3323 template <typename T>
3324 inline Table<4, T>::Table(const size_type size1,
3325 const size_type size2,
3326 const size_type size3,
3327 const size_type size4)
3328 : TableBase<4, T>(TableIndices<4>(size1, size2, size3, size4))
3329 {}
3330
3331
3332
3333 template <typename T>
3334 inline dealii::internal::TableBaseAccessors::Accessor<4, T, true, 3>
3335 Table<4, T>::operator[](const size_type i) const
3336 {
3337 AssertIndexRange(i, this->table_size[0]);
3338 const size_type subobject_size =
3339 size_type(this->table_size[1]) * this->table_size[2] * this->table_size[3];
3340 return (dealii::internal::TableBaseAccessors::Accessor<4, T, true, 3>(
3341 *this, this->values.begin() + i * subobject_size));
3342 }
3343
3344
3345
3346 template <typename T>
3347 inline dealii::internal::TableBaseAccessors::Accessor<4, T, false, 3>
3348 Table<4, T>::operator[](const size_type i)
3349 {
3350 AssertIndexRange(i, this->table_size[0]);
3351 const size_type subobject_size =
3352 size_type(this->table_size[1]) * this->table_size[2] * this->table_size[3];
3353 return (dealii::internal::TableBaseAccessors::Accessor<4, T, false, 3>(
3354 *this, this->values.begin() + i * subobject_size));
3355 }
3356
3357
3358
3359 template <typename T>
3360 inline typename AlignedVector<T>::const_reference
3361 Table<4, T>::operator()(const size_type i,
3362 const size_type j,
3363 const size_type k,
3364 const size_type l) const
3365 {
3366 AssertIndexRange(i, this->table_size[0]);
3367 AssertIndexRange(j, this->table_size[1]);
3368 AssertIndexRange(k, this->table_size[2]);
3369 AssertIndexRange(l, this->table_size[3]);
3370 return this
3371 ->values[((size_type(i) * this->table_size[1] + j) * this->table_size[2] +
3372 k) *
3373 this->table_size[3] +
3374 l];
3375 }
3376
3377
3378
3379 template <typename T>
3380 inline typename AlignedVector<T>::reference
3381 Table<4, T>::operator()(const size_type i,
3382 const size_type j,
3383 const size_type k,
3384 const size_type l)
3385 {
3386 AssertIndexRange(i, this->table_size[0]);
3387 AssertIndexRange(j, this->table_size[1]);
3388 AssertIndexRange(k, this->table_size[2]);
3389 AssertIndexRange(l, this->table_size[3]);
3390 return this
3391 ->values[((size_type(i) * this->table_size[1] + j) * this->table_size[2] +
3392 k) *
3393 this->table_size[3] +
3394 l];
3395 }
3396
3397
3398
3399 template <typename T>
3400 inline Table<5, T>::Table(const size_type size1,
3401 const size_type size2,
3402 const size_type size3,
3403 const size_type size4,
3404 const size_type size5)
3405 : TableBase<5, T>(TableIndices<5>(size1, size2, size3, size4, size5))
3406 {}
3407
3408
3409
3410 template <typename T>
3411 inline dealii::internal::TableBaseAccessors::Accessor<5, T, true, 4>
3412 Table<5, T>::operator[](const size_type i) const
3413 {
3414 AssertIndexRange(i, this->table_size[0]);
3415 const size_type subobject_size = size_type(this->table_size[1]) *
3416 this->table_size[2] * this->table_size[3] *
3417 this->table_size[4];
3418 return (dealii::internal::TableBaseAccessors::Accessor<5, T, true, 4>(
3419 *this, this->values.begin() + i * subobject_size));
3420 }
3421
3422
3423
3424 template <typename T>
3425 inline dealii::internal::TableBaseAccessors::Accessor<5, T, false, 4>
3426 Table<5, T>::operator[](const size_type i)
3427 {
3428 AssertIndexRange(i, this->table_size[0]);
3429 const size_type subobject_size = size_type(this->table_size[1]) *
3430 this->table_size[2] * this->table_size[3] *
3431 this->table_size[4];
3432 return (dealii::internal::TableBaseAccessors::Accessor<5, T, false, 4>(
3433 *this, this->values.begin() + i * subobject_size));
3434 }
3435
3436
3437
3438 template <typename T>
3439 inline typename AlignedVector<T>::const_reference
3440 Table<5, T>::operator()(const size_type i,
3441 const size_type j,
3442 const size_type k,
3443 const size_type l,
3444 const size_type m) const
3445 {
3446 AssertIndexRange(i, this->table_size[0]);
3447 AssertIndexRange(j, this->table_size[1]);
3448 AssertIndexRange(k, this->table_size[2]);
3449 AssertIndexRange(l, this->table_size[3]);
3450 AssertIndexRange(m, this->table_size[4]);
3451 return this
3452 ->values[(((size_type(i) * this->table_size[1] + j) * this->table_size[2] +
3453 k) *
3454 this->table_size[3] +
3455 l) *
3456 this->table_size[4] +
3457 m];
3458 }
3459
3460
3461
3462 template <typename T>
3463 inline typename AlignedVector<T>::reference
3464 Table<5, T>::operator()(const size_type i,
3465 const size_type j,
3466 const size_type k,
3467 const size_type l,
3468 const size_type m)
3469 {
3470 AssertIndexRange(i, this->table_size[0]);
3471 AssertIndexRange(j, this->table_size[1]);
3472 AssertIndexRange(k, this->table_size[2]);
3473 AssertIndexRange(l, this->table_size[3]);
3474 AssertIndexRange(m, this->table_size[4]);
3475 return this
3476 ->values[(((size_type(i) * this->table_size[1] + j) * this->table_size[2] +
3477 k) *
3478 this->table_size[3] +
3479 l) *
3480 this->table_size[4] +
3481 m];
3482 }
3483
3484
3485
3486 template <typename T>
3487 inline Table<6, T>::Table(const size_type size1,
3488 const size_type size2,
3489 const size_type size3,
3490 const size_type size4,
3491 const size_type size5,
3492 const size_type size6)
3493 : TableBase<6, T>()
3494 {
3495 TableIndices<6> table_indices;
3496 table_indices[0] = size1;
3497 table_indices[1] = size2;
3498 table_indices[2] = size3;
3499 table_indices[3] = size4;
3500 table_indices[4] = size5;
3501 table_indices[5] = size6;
3502
3503 TableBase<6, T>::reinit(table_indices);
3504 }
3505
3506
3507
3508 template <typename T>
3509 inline dealii::internal::TableBaseAccessors::Accessor<6, T, true, 5>
3510 Table<6, T>::operator[](const size_type i) const
3511 {
3512 AssertIndexRange(i, this->table_size[0]);
3513 const size_type subobject_size = size_type(this->table_size[1]) *
3514 this->table_size[2] * this->table_size[3] *
3515 this->table_size[4] * this->table_size[5];
3516 return (dealii::internal::TableBaseAccessors::Accessor<6, T, true, 5>(
3517 *this, this->values.begin() + i * subobject_size));
3518 }
3519
3520
3521
3522 template <typename T>
3523 inline dealii::internal::TableBaseAccessors::Accessor<6, T, false, 5>
3524 Table<6, T>::operator[](const size_type i)
3525 {
3526 AssertIndexRange(i, this->table_size[0]);
3527 const size_type subobject_size = size_type(this->table_size[1]) *
3528 this->table_size[2] * this->table_size[3] *
3529 this->table_size[4] * this->table_size[5];
3530 return (dealii::internal::TableBaseAccessors::Accessor<6, T, false, 5>(
3531 *this, this->values.begin() + i * subobject_size));
3532 }
3533
3534
3535
3536 template <typename T>
3537 inline typename AlignedVector<T>::const_reference
3538 Table<6, T>::operator()(const size_type i,
3539 const size_type j,
3540 const size_type k,
3541 const size_type l,
3542 const size_type m,
3543 const size_type n) const
3544 {
3545 AssertIndexRange(i, this->table_size[0]);
3546 AssertIndexRange(j, this->table_size[1]);
3547 AssertIndexRange(k, this->table_size[2]);
3548 AssertIndexRange(l, this->table_size[3]);
3549 AssertIndexRange(m, this->table_size[4]);
3550 AssertIndexRange(n, this->table_size[5]);
3551 return this
3552 ->values[((((size_type(i) * this->table_size[1] + j) * this->table_size[2] +
3553 k) *
3554 this->table_size[3] +
3555 l) *
3556 this->table_size[4] +
3557 m) *
3558 this->table_size[5] +
3559 n];
3560 }
3561
3562
3563
3564 template <typename T>
3565 inline typename AlignedVector<T>::reference
3566 Table<6, T>::operator()(const size_type i,
3567 const size_type j,
3568 const size_type k,
3569 const size_type l,
3570 const size_type m,
3571 const size_type n)
3572 {
3573 AssertIndexRange(i, this->table_size[0]);
3574 AssertIndexRange(j, this->table_size[1]);
3575 AssertIndexRange(k, this->table_size[2]);
3576 AssertIndexRange(l, this->table_size[3]);
3577 AssertIndexRange(m, this->table_size[4]);
3578 AssertIndexRange(n, this->table_size[5]);
3579 return this
3580 ->values[((((size_type(i) * this->table_size[1] + j) * this->table_size[2] +
3581 k) *
3582 this->table_size[3] +
3583 l) *
3584 this->table_size[4] +
3585 m) *
3586 this->table_size[5] +
3587 n];
3588 }
3589
3590
3591
3592 template <typename T>
3593 inline Table<7, T>::Table(const size_type size1,
3594 const size_type size2,
3595 const size_type size3,
3596 const size_type size4,
3597 const size_type size5,
3598 const size_type size6,
3599 const size_type size7)
3600 : TableBase<7, T>()
3601 {
3602 TableIndices<7> table_indices;
3603 table_indices[0] = size1;
3604 table_indices[1] = size2;
3605 table_indices[2] = size3;
3606 table_indices[3] = size4;
3607 table_indices[4] = size5;
3608 table_indices[5] = size6;
3609 table_indices[6] = size7;
3610
3611 TableBase<7, T>::reinit(table_indices);
3612 }
3613
3614
3615
3616 template <typename T>
3617 inline dealii::internal::TableBaseAccessors::Accessor<7, T, true, 6>
3618 Table<7, T>::operator[](const size_type i) const
3619 {
3620 AssertIndexRange(i, this->table_size[0]);
3621 const size_type subobject_size =
3622 size_type(this->table_size[1]) * this->table_size[2] * this->table_size[3] *
3623 this->table_size[4] * this->table_size[5] * this->table_size[6];
3624 return (dealii::internal::TableBaseAccessors::Accessor<7, T, true, 6>(
3625 *this, this->values.begin() + i * subobject_size));
3626 }
3627
3628
3629
3630 template <typename T>
3631 inline dealii::internal::TableBaseAccessors::Accessor<7, T, false, 6>
3632 Table<7, T>::operator[](const size_type i)
3633 {
3634 AssertIndexRange(i, this->table_size[0]);
3635 const size_type subobject_size =
3636 size_type(this->table_size[1]) * this->table_size[2] * this->table_size[3] *
3637 this->table_size[4] * this->table_size[5] * this->table_size[6];
3638 return (dealii::internal::TableBaseAccessors::Accessor<7, T, false, 6>(
3639 *this, this->values.begin() + i * subobject_size));
3640 }
3641
3642
3643
3644 template <typename T>
3645 inline typename AlignedVector<T>::const_reference
3646 Table<7, T>::operator()(const size_type i,
3647 const size_type j,
3648 const size_type k,
3649 const size_type l,
3650 const size_type m,
3651 const size_type n,
3652 const size_type o) const
3653 {
3654 AssertIndexRange(i, this->table_size[0]);
3655 AssertIndexRange(j, this->table_size[1]);
3656 AssertIndexRange(k, this->table_size[2]);
3657 AssertIndexRange(l, this->table_size[3]);
3658 AssertIndexRange(m, this->table_size[4]);
3659 AssertIndexRange(n, this->table_size[5]);
3660 AssertIndexRange(o, this->table_size[6]);
3661 return this->values
3662 [(((((size_type(i) * this->table_size[1] + j) * this->table_size[2] + k) *
3663 this->table_size[3] +
3664 l) *
3665 this->table_size[4] +
3666 m) *
3667 this->table_size[5] +
3668 n) *
3669 this->table_size[6] +
3670 o];
3671 }
3672
3673
3674
3675 template <typename T>
3676 inline typename AlignedVector<T>::reference
3677 Table<7, T>::operator()(const size_type i,
3678 const size_type j,
3679 const size_type k,
3680 const size_type l,
3681 const size_type m,
3682 const size_type n,
3683 const size_type o)
3684 {
3685 AssertIndexRange(i, this->table_size[0]);
3686 AssertIndexRange(j, this->table_size[1]);
3687 AssertIndexRange(k, this->table_size[2]);
3688 AssertIndexRange(l, this->table_size[3]);
3689 AssertIndexRange(m, this->table_size[4]);
3690 AssertIndexRange(n, this->table_size[5]);
3691 AssertIndexRange(o, this->table_size[6]);
3692 return this->values
3693 [(((((size_type(i) * this->table_size[1] + j) * this->table_size[2] + k) *
3694 this->table_size[3] +
3695 l) *
3696 this->table_size[4] +
3697 m) *
3698 this->table_size[5] +
3699 n) *
3700 this->table_size[6] +
3701 o];
3702 }
3703
3704
3705 #endif // DOXYGEN
3706
3707
3708
3709 /**
3710 * Global function @p swap which overloads the default implementation of the
3711 * C++ standard library which uses a temporary object. The function simply
3712 * exchanges the data of the two tables.
3713 */
3714 template <int N, typename T>
3715 inline void
3716 swap(TableBase<N, T> &u, TableBase<N, T> &v) noexcept
3717 {
3718 u.swap(v);
3719 }
3720
3721 DEAL_II_NAMESPACE_CLOSE
3722
3723 #endif