Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / mdspan / extensions.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \libinternal
36  * \file
37  * \brief GROMACS extensions to mdspan.
38  *
39  * \author Christian Blau <cblau@gwdg.de>
40  * \inlibraryapi
41  * \ingroup module_mdspan
42  */
43
44 #ifndef GMX_MDSPAN_EXTENSIONS_H_
45 #define GMX_MDSPAN_EXTENSIONS_H_
46
47 #include <algorithm>
48 #include <functional>
49
50 #include "gromacs/mdspan/mdspan.h"
51
52 namespace gmx
53 {
54
55 /*! \brief
56  * Free begin function addressing memory of a contiguously laid out basic_mdspan.
57  *
58  * \note Changing the elements that basic_mdspan views does not change
59  *       the view itself, so a single begin that takes a const view suffices.
60  */
61 template<class BasicMdspan>
62 constexpr std::enable_if_t<BasicMdspan::is_always_contiguous(), typename BasicMdspan::pointer>
63 begin(const BasicMdspan& basicMdspan)
64 {
65     return basicMdspan.data();
66 }
67
68 /*! \brief
69  * Free end function addressing memory of a contiguously laid out basic_mdspan.
70  *
71  * \note Changing the elements that basic_mdspan views does not change
72  *       the view itself, so a single end that takes a const view suffices.
73  */
74 template<class BasicMdspan>
75 constexpr std::enable_if_t<BasicMdspan::is_always_contiguous(), typename BasicMdspan::pointer>
76 end(const BasicMdspan& basicMdspan)
77 {
78     return basicMdspan.data() + basicMdspan.mapping().required_span_size();
79 }
80
81 //! Convenience type for often-used two dimensional extents
82 using dynamicExtents2D = extents<dynamic_extent, dynamic_extent>;
83
84 //! Convenience type for often-used three dimensional extents
85 using dynamicExtents3D = extents<dynamic_extent, dynamic_extent, dynamic_extent>;
86
87 //! Elementwise addition
88 template<class BasicMdspan>
89 constexpr BasicMdspan addElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
90 {
91     BasicMdspan result(span1);
92     std::transform(begin(span1),
93                    end(span1),
94                    begin(span2),
95                    begin(result),
96                    std::plus<typename BasicMdspan::element_type>());
97     return result;
98 }
99
100 //! Elementwise subtraction - left minus right
101 template<class BasicMdspan>
102 constexpr BasicMdspan subtractElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
103 {
104     BasicMdspan result(span1);
105     std::transform(begin(span1),
106                    end(span1),
107                    begin(span2),
108                    begin(result),
109                    std::minus<typename BasicMdspan::element_type>());
110     return result;
111 }
112
113 //! Elementwise multiplication
114 template<class BasicMdspan>
115 constexpr BasicMdspan multiplyElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
116 {
117     BasicMdspan result(span1);
118     std::transform(begin(span1),
119                    end(span1),
120                    begin(span2),
121                    begin(result),
122                    std::multiplies<typename BasicMdspan::element_type>());
123     return result;
124 }
125
126 //! Elementwise division - left / right
127 template<class BasicMdspan>
128 constexpr BasicMdspan divideElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
129 {
130     BasicMdspan result(span1);
131     std::transform(begin(span1),
132                    end(span1),
133                    begin(span2),
134                    begin(result),
135                    std::divides<typename BasicMdspan::element_type>());
136     return result;
137 }
138
139 } // namespace gmx
140
141 #endif // GMX_MDSPAN_EXTENSIONS_H_