Improve MessageStringCollector
[alexxy/gromacs.git] / src / gromacs / utility / range.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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 /*! \file
36  * \brief
37  * Declares gmx::Range
38  *
39  * \author Berk Hess <hess@kth.se>
40  * \inpublicapi
41  * \ingroup module_utility
42  */
43 #ifndef GMX_UTILITY_RANGE_H
44 #define GMX_UTILITY_RANGE_H
45
46 #include <type_traits>
47
48 #include "gromacs/utility/basedefinitions.h"
49 #include "gromacs/utility/gmxassert.h"
50
51 namespace gmx
52 {
53
54 /*! \brief Defines a range of integer numbers and accompanying operations
55  *
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:
58  *
59  * Range<int> range(2,5);
60  * for (int i : range) { printf(" %d", i); }
61  *
62  * This will print: 2 3 4
63  */
64 template<typename T>
65 class Range
66 {
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");
69
70     // Note: This class has as invariant: begin_ <= end_
71
72 public:
73     //! An iterator that loops over a range of integers
74     struct iterator
75     {
76         //! Constructor
77         iterator(T value) : value_(value) {}
78         //! Value
79         operator T() const { return value_; }
80         //! Reference
81         operator T&() { return value_; }
82         //! Pointer
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++()
88         {
89             ++value_;
90             return *this;
91         }
92         //! Increment operator
93         iterator operator++(int gmx_unused dummy)
94         {
95             iterator tmp(*this);
96             ++value_;
97             return tmp;
98         }
99         //! The actual value
100         T value_;
101     };
102
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)
105     {
106         GMX_RELEASE_ASSERT(begin_ <= end_, "A range should have begin<=end");
107     }
108
109     //! Default constructor, produces an empty range
110     Range() = default;
111
112     //! Begin iterator/value
113     iterator begin() const { return begin_; }
114     //! End iterator/value
115     iterator end() const { return end_; }
116
117     //! Returns the length of the range
118     T size() const { return end_ - begin_; }
119
120     //! Returns whether the range is empty
121     bool empty() const { return size() == 0; }
122
123     //! Returns whether \p value is in range
124     bool isInRange(const T value) const { return (begin_ <= value && value < end_); }
125
126 private:
127     //! The start of the range
128     T begin_ = 0;
129     //! The end of the range
130     T end_ = 0;
131 };
132
133 } // namespace gmx
134
135 #endif