Chemical Data Processing Library C++ API - Version 1.4.0
Range.hpp
Go to the documentation of this file.
1 /*
2  * Range.hpp
3  *
4  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 #ifndef CDPL_MATH_RANGE_HPP
28 #define CDPL_MATH_RANGE_HPP
29 
30 #include <cstddef>
31 #include <utility>
32 
33 #include "CDPL/Math/Check.hpp"
34 #include "CDPL/Base/Exceptions.hpp"
35 
36 
37 namespace CDPL
38 {
39 
40  namespace Math
41  {
42 
47  template <typename S>
48  class Range
49  {
50 
51  typedef Range<S> SelfType;
52 
53  public:
57  typedef S SizeType;
58 
62  Range():
63  start(0), stop(0) {}
64 
71  Range(SizeType start, SizeType stop):
72  start(start), stop(stop)
73  {
74  CDPL_MATH_CHECK(start <= stop, "Invalid range specification", Base::RangeError);
75  }
76 
84  {
85  CDPL_MATH_CHECK(i < getSize(), "Index out of range", Base::IndexError);
86  return (start + i);
87  }
88 
94  {
95  return start;
96  }
97 
103  {
104  return stop;
105  }
106 
112  {
113  return (stop - start);
114  }
115 
120  bool isEmpty() const
121  {
122  return (stop == start);
123  }
124 
130  bool operator==(const Range& r) const
131  {
132  return (start == r.start && stop == r.stop);
133  }
134 
140  bool operator!=(const Range& r) const
141  {
142  return !this->operator==(r);
143  }
144 
149  void swap(Range& r)
150  {
151  if (this == &r)
152  return;
153 
154  std::swap(start, r.start);
155  std::swap(stop, r.stop);
156  }
157 
163  friend void swap(Range& r1, Range& r2)
164  {
165  r1.swap(r2);
166  }
167 
168  private:
169  SizeType start;
170  SizeType stop;
171  };
172 
179  inline Range<std::size_t>
180  range(std::size_t start, std::size_t stop)
181  {
182  return Range<std::size_t>(start, stop);
183  }
184  } // namespace Math
185 } // namespace CDPL
186 
187 #endif // CDPL_MATH_RANGE_HPP
Definition of exception classes.
Definition of various preprocessor macros for error checking.
#define CDPL_MATH_CHECK(expr, msg, e)
Throws the exception e with message msg when the boolean expression expr evaluates to false.
Definition: Check.hpp:47
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
Thrown to indicate that a value is out of range.
Definition: Base/Exceptions.hpp:114
Half-open index range [start, stop) used for slicing vector and matrix expressions.
Definition: Range.hpp:49
friend void swap(Range &r1, Range &r2)
Free-function swap overload.
Definition: Range.hpp:163
bool operator!=(const Range &r) const
Inequality comparison.
Definition: Range.hpp:140
Range(SizeType start, SizeType stop)
Constructs the range [start, stop).
Definition: Range.hpp:71
Range()
Constructs an empty range [0, 0).
Definition: Range.hpp:62
SizeType operator()(SizeType i) const
Maps the local position i to the global index .
Definition: Range.hpp:83
SizeType getStop() const
Returns the upper (exclusive) bound.
Definition: Range.hpp:102
SizeType getStart() const
Returns the lower (inclusive) bound.
Definition: Range.hpp:93
bool isEmpty() const
Tells whether the range is empty.
Definition: Range.hpp:120
void swap(Range &r)
Swaps the contents of *this and r.
Definition: Range.hpp:149
SizeType getSize() const
Returns the size of the range.
Definition: Range.hpp:111
S SizeType
The integral size/index type.
Definition: Range.hpp:57
bool operator==(const Range &r) const
Equality comparison.
Definition: Range.hpp:130
constexpr unsigned int S
Specifies that the atom has S configuration.
Definition: AtomConfiguration.hpp:63
constexpr unsigned int r
Specifies that the stereocenter has r configuration.
Definition: CIPDescriptor.hpp:76
MatrixRange< E > range(MatrixExpression< E > &e, const typename MatrixRange< E >::RangeType &r1, const typename MatrixRange< E >::RangeType &r2)
Returns a mutable matrix range proxy viewing rows in r1 and columns in r2 of e.
Definition: MatrixProxy.hpp:1429
The namespace of the Chemical Data Processing Library.