Apply clang-format to source 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, 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 three dimensional extents
82 using dynamicExtents3D = extents<dynamic_extent, dynamic_extent, dynamic_extent>;
83
84 //! Elementwise addition
85 template<class BasicMdspan>
86 constexpr BasicMdspan addElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
87 {
88     BasicMdspan result(span1);
89     std::transform(begin(span1), end(span1), begin(span2), begin(result),
90                    std::plus<typename BasicMdspan::element_type>());
91     return result;
92 }
93
94 //! Elementwise subtraction - left minus right
95 template<class BasicMdspan>
96 constexpr BasicMdspan subtractElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
97 {
98     BasicMdspan result(span1);
99     std::transform(begin(span1), end(span1), begin(span2), begin(result),
100                    std::minus<typename BasicMdspan::element_type>());
101     return result;
102 }
103
104 //! Elementwise multiplication
105 template<class BasicMdspan>
106 constexpr BasicMdspan multiplyElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
107 {
108     BasicMdspan result(span1);
109     std::transform(begin(span1), end(span1), begin(span2), begin(result),
110                    std::multiplies<typename BasicMdspan::element_type>());
111     return result;
112 }
113
114 //! Elementwise division - left / right
115 template<class BasicMdspan>
116 constexpr BasicMdspan divideElementwise(const BasicMdspan& span1, const BasicMdspan& span2)
117 {
118     BasicMdspan result(span1);
119     std::transform(begin(span1), end(span1), begin(span2), begin(result),
120                    std::divides<typename BasicMdspan::element_type>());
121     return result;
122 }
123
124 } // namespace gmx
125
126 #endif // GMX_MDSPAN_EXTENSIONS_H_