Apply clang-format to source tree
[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, 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 "gromacs/utility/gmxassert.h"
47
48 namespace gmx
49 {
50
51 /*! \brief Defines a range of integer numbers and accompanying operations
52  *
53  * Defines a range of integer type with begin and end.
54  * Can be used in a range loop over all integers in the range as in:
55  *
56  * Range<int> range(2,5);
57  * for (int i : range) { printf(" %d", i); }
58  *
59  * This will print: 2 3 4
60  */
61 template<typename T>
62 class Range
63 {
64     static_assert(std::is_integral<T>::value, "Range can only be used with integral types");
65
66     // Note: This class has as invariant: begin_ <= end_
67
68 public:
69     //! An iterator that loops over a range of integers
70     struct iterator
71     {
72         //! Constructor
73         iterator(T value) : value_(value) {}
74         //! Value
75         operator T() const { return value_; }
76         //! Reference
77         operator T&() { return value_; }
78         //! Pointer
79         int operator*() const { return value_; }
80         //! Inequality comparison
81         bool operator!=(const iterator other) { return value_ != other.value_; }
82         //! Increment operator
83         iterator& operator++()
84         {
85             ++value_;
86             return *this;
87         }
88         //! Increment operator
89         iterator operator++(T gmx_unused dummy)
90         {
91             iterator tmp(*this);
92             ++value_;
93             return tmp;
94         }
95         //! The actual value
96         T value_;
97     };
98
99     //! Constructor, has to be called with \p begin <= \p end (is checked)
100     Range(const T begin, const T end) : begin_(begin), end_(end)
101     {
102         GMX_RELEASE_ASSERT(begin_ <= end_, "A range should have begin<=end");
103     }
104
105     //! Default constructor, produces an empty range
106     Range() = default;
107
108     //! Begin iterator/value
109     iterator begin() const { return begin_; }
110     //! End iterator/value
111     iterator end() const { return end_; }
112
113     //! Returns the length of the range
114     T size() const { return end_ - begin_; }
115
116     //! Returns whether the range is empty
117     bool empty() const { return size() == 0; }
118
119     //! Returns whether \p value is in range
120     bool isInRange(const T value) const { return (begin_ <= value && value < end_); }
121
122 private:
123     //! The start of the range
124     T begin_ = 0;
125     //! The end of the range
126     T end_ = 0;
127 };
128
129 } // namespace gmx
130
131 #endif