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.
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 "gromacs/utility/gmxassert.h"
51 /*! \brief Defines a range of integer numbers and accompanying operations
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:
56 * Range<int> range(2,5);
57 * for (int i : range) { printf(" %d", i); }
59 * This will print: 2 3 4
64 static_assert(std::is_integral<T>::value, "Range can only be used with integral types");
66 // Note: This class has as invariant: begin_ <= end_
69 //! An iterator that loops over a range of integers
73 iterator(T value) : value_(value) {}
75 operator T() const { return value_; }
77 operator T&() { return value_; }
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++()
88 //! Increment operator
89 iterator operator++(T gmx_unused dummy)
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)
102 GMX_RELEASE_ASSERT(begin_ <= end_, "A range should have begin<=end");
105 //! Default constructor, produces an empty range
108 //! Begin iterator/value
109 iterator begin() const { return begin_; }
110 //! End iterator/value
111 iterator end() const { return end_; }
113 //! Returns the length of the range
114 T size() const { return end_ - begin_; }
116 //! Returns whether the range is empty
117 bool empty() const { return size() == 0; }
119 //! Returns whether \p value is in range
120 bool isInRange(const T value) const { return (begin_ <= value && value < end_); }
123 //! The start of the range
125 //! The end of the range