Add replacements for pbc enumerations
[gromacs.git] / src / gromacs / mdspan / extensions.h
blob4b774151645ae620170230c6224b61ad09dc2775
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 /*! \libinternal
36 * \file
37 * \brief GROMACS extensions to mdspan.
39 * \author Christian Blau <cblau@gwdg.de>
40 * \inlibraryapi
41 * \ingroup module_mdspan
44 #ifndef GMX_MDSPAN_EXTENSIONS_H_
45 #define GMX_MDSPAN_EXTENSIONS_H_
47 #include <algorithm>
48 #include <functional>
50 #include "gromacs/mdspan/mdspan.h"
52 namespace gmx
55 /*! \brief
56 * Free begin function addressing memory of a contiguously laid out basic_mdspan.
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.
61 template <class BasicMdspan>
62 constexpr std::enable_if_t<BasicMdspan::is_always_contiguous(),
63 typename BasicMdspan::pointer>
64 begin(const BasicMdspan &basicMdspan)
66 return basicMdspan.data();
69 /*! \brief
70 * Free end function addressing memory of a contiguously laid out basic_mdspan.
72 * \note Changing the elements that basic_mdspan views does not change
73 * the view itself, so a single end that takes a const view suffices.
75 template <class BasicMdspan>
76 constexpr std::enable_if_t<BasicMdspan::is_always_contiguous(),
77 typename BasicMdspan::pointer>
78 end(const BasicMdspan &basicMdspan)
80 return basicMdspan.data() + basicMdspan.mapping().required_span_size();
83 //! Convenience type for often-used three dimensional extents
84 using dynamicExtents3D = extents<dynamic_extent, dynamic_extent, dynamic_extent>;
86 //! Elementwise addition
87 template <class BasicMdspan>
88 constexpr BasicMdspan addElementwise(const BasicMdspan &span1, const BasicMdspan &span2)
90 BasicMdspan result(span1);
91 std::transform(begin(span1), end(span1), begin(span2),
92 begin(result), std::plus<typename BasicMdspan::element_type>());
93 return result;
96 //! Elementwise subtraction - left minus right
97 template <class BasicMdspan>
98 constexpr BasicMdspan subtractElementwise(const BasicMdspan &span1, const BasicMdspan &span2)
100 BasicMdspan result(span1);
101 std::transform(begin(span1), end(span1), begin(span2),
102 begin(result), std::minus<typename BasicMdspan::element_type>());
103 return result;
106 //! Elementwise multiplication
107 template <class BasicMdspan>
108 constexpr BasicMdspan multiplyElementwise(const BasicMdspan &span1, const BasicMdspan &span2)
110 BasicMdspan result(span1);
111 std::transform(begin(span1), end(span1), begin(span2),
112 begin(result), std::multiplies<typename BasicMdspan::element_type>());
113 return result;
116 //! Elementwise division - left / right
117 template <class BasicMdspan>
118 constexpr BasicMdspan divideElementwise(const BasicMdspan &span1, const BasicMdspan &span2)
120 BasicMdspan result(span1);
121 std::transform(begin(span1), end(span1), begin(span2),
122 begin(result), std::divides<typename BasicMdspan::element_type>());
123 return result;
126 } // namespace gmx
128 #endif // GMX_MDSPAN_EXTENSIONS_H_