Debian Codegraph @

src/atlas/array/helpers/ArrayAssigner.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 <type_traits>
14
15 #include "atlas/array.h"
16 #include "atlas/runtime/Exception.h"
17
18 //------------------------------------------------------------------------------
19
20 namespace atlas {
21 namespace array {
22 namespace helpers {
23
24 //------------------------------------------------------------------------------
25
26 /// Helper to assign a value to an array or arrayview
27 template <typename Value, unsigned int Rank>
28 struct array_assigner;
29
30 //------------------------------------------------------------------------------
31
32 // Recursive function to apply value to every index
33 template <typename Value, unsigned int Rank, unsigned int Dim>
34 struct array_assigner_impl {
35 template <typename View, typename... DimIndex>
36 static void apply(View& arr, Value value, DimIndex... idxs) {
37 for (idx_t i = 0; i < arr.shape(Dim); ++i) {
38 array_assigner_impl<Value, Rank, Dim + 1>::apply(arr, value, idxs..., i);
39 }
40 }
41
42 template <typename View, typename Iterator, typename... DimIndex>
43 static void apply(View& arr, Iterator& it, DimIndex... idxs) {
44 for (idx_t i = 0; i < arr.shape(Dim); ++i) {
45 array_assigner_impl<Value, Rank, Dim + 1>::apply(arr, it, idxs..., i);
46 }
47 }
48 };
49
50 // End of recursion when Dim == Rank
51 template <typename Value, unsigned int Rank>
52 struct array_assigner_impl<Value, Rank, Rank> {
53 template <typename View, typename... DimIndex>
54 static void apply(View& arr, Value value, DimIndex... idxs) {
55 arr(idxs...) = value;
56 }
57
58 template <typename View, typename Iterator, typename... DimIndex>
59 static void apply(View& arr, Iterator& it, DimIndex... idxs) {
60 arr(idxs...) = *it;
61 ++it;
62 }
63 };
64
65 //------------------------------------------------------------------------------
66
67 template <typename Value, unsigned int Rank>
68 struct array_assigner {
69 template <typename T>
70 static void apply(Array& arr, Value value) {
71 return apply(make_host_view<T, Rank>(arr), value);
72 }
73
74 static void apply(ArrayView<Value, Rank>& arr, Value value) {
75 array_assigner_impl<Value, Rank, 0u>::apply(arr, value);
76 // Note: no need to apply variadic pack (idxs...)
77 }
78
79 template <typename Iterable>
80 static void apply(ArrayView<Value, Rank>& arr, const Iterable& iterable) {
81 typename Iterable::const_iterator it = iterable.begin();
82 array_assigner_impl<Value, Rank, 0u>::apply(arr, it);
83 ATLAS_ASSERT(it = iterable.end());
84 }
85
86 template <typename Iterable>
87 static void apply(IndexView<Value, Rank>& arr, const Iterable& iterable) {
88 typename Iterable::const_iterator it = iterable.begin();
89 array_assigner_impl<Value, Rank, 0u>::apply(arr, it);
90 ATLAS_ASSERT(it = iterable.end());
91 }
92
93
94 static void apply(LocalView<Value, Rank>& arr, Value value) {
95 array_assigner_impl<Value, Rank, 0u>::apply(arr, value);
96 // Note: no need to apply variadic pack (idxs...)
97 }
98 };
99
100 //------------------------------------------------------------------------------
101
102 } // namespace helpers
103 } // namespace array
104 } // namespace atlas