2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019,2020,2021, 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.
39 * \author Berk Hess <hess@kth.se>
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_RANGE_H
44 #define GMX_UTILITY_RANGE_H
46 #include <type_traits>
48 #include "gromacs/utility/basedefinitions.h"
49 #include "gromacs/utility/gmxassert.h"
54 /*! \brief Defines a range of integer numbers and accompanying operations
56 * Defines a range of integer type with begin and end.
57 * Can be used in a range loop over all integers in the range as in:
59 * Range<int> range(2,5);
60 * for (int i : range) { printf(" %d", i); }
62 * This will print: 2 3 4
67 // TODO: Use std::is_integral_v when CUDA 11 is a requirement.
68 static_assert(std::is_integral<T>::value, "Range can only be used with integral types");
70 // Note: This class has as invariant: begin_ <= end_
73 //! An iterator that loops over a range of integers
77 iterator(T value) : value_(value) {}
79 operator T() const { return value_; }
81 operator T&() { return value_; }
83 T operator*() const { return value_; }
84 //! Inequality comparison
85 bool operator!=(const iterator other) { return value_ != other.value_; }
86 //! Increment operator
87 iterator& operator++()
92 //! Increment operator
93 iterator operator++(int gmx_unused dummy)
103 //! Constructor, has to be called with \p begin <= \p end (is checked)
104 Range(const T begin, const T end) : begin_(begin), end_(end)
106 GMX_RELEASE_ASSERT(begin_ <= end_, "A range should have begin<=end");
109 //! Default constructor, produces an empty range
112 //! Begin iterator/value
113 iterator begin() const { return begin_; }
114 //! End iterator/value
115 iterator end() const { return end_; }
117 //! Returns the length of the range
118 T size() const { return end_ - begin_; }
120 //! Returns whether the range is empty
121 bool empty() const { return size() == 0; }
123 //! Returns whether \p value is in range
124 bool isInRange(const T value) const { return (begin_ <= value && value < end_); }
127 //! The start of the range
129 //! The end of the range