Debian Codegraph @

src/atlas/array/helpers/ArrayWriter.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 #include "atlas/array.h"
14
15 //------------------------------------------------------------------------------
16
17 namespace atlas {
18 namespace array {
19 namespace helpers {
20
21 //------------------------------------------------------------------------------
22
23 /// Helper to assign a value to an array or arrayview
24 // template <typename Value>
25 struct array_writer;
26
27 //------------------------------------------------------------------------------
28
29 // Recursive function to apply value to every index
30 template <typename Value, int Rank, int Dim>
31 struct array_writer_impl {
32 template <typename View, typename... DimIndex>
33 static void apply(const View& arr, std::ostream& out, DimIndex... idxs) {
34 for (idx_t i = 0; i < arr.shape(Dim); ++i) {
35 array_writer_impl<Value, Rank, Dim + 1>::apply(arr, out, idxs..., i);
36 if (i < arr.shape(Dim) - 1)
37 out << " ";
38 }
39 }
40 };
41
42 // End of recursion when Dim == Rank
43 template <typename Value, int Rank>
44 struct array_writer_impl<Value, Rank, Rank> {
45 template <typename View, typename... DimIndex>
46 static void apply(const View& arr, std::ostream& out, DimIndex... idxs) {
47 out << arr(idxs...);
48 }
49 };
50
51 //------------------------------------------------------------------------------
52
53 struct array_writer {
54 template <typename Value, int Rank>
55 static void apply(const ArrayView<Value, Rank>& arr, std::ostream& out) {
56 array_writer_impl<Value, Rank, 0u>::apply(arr, out);
57 // Note: no need to apply variadic pack (idxs...)
58 }
59
60 template <typename Value, int Rank>
61 static void apply(const LocalView<Value, Rank>& arr, std::ostream& out) {
62 array_writer_impl<Value, Rank, 0u>::apply(arr, out);
63 // Note: no need to apply variadic pack (idxs...)
64 }
65 };
66
67 //------------------------------------------------------------------------------
68
69 } // namespace helpers
70 } // namespace array
71 } // namespace atlas