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:
55  typedef S SizeType;
56 
60  Range():
61  start(0), stop(0) {}
62 
69  Range(SizeType start, SizeType stop):
70  start(start), stop(stop)
71  {
72  CDPL_MATH_CHECK(start <= stop, "Invalid range specification", Base::RangeError);
73  }
74 
82  {
83  CDPL_MATH_CHECK(i < getSize(), "Index out of range", Base::IndexError);
84  return (start + i);
85  }
86 
92  {
93  return start;
94  }
95 
101  {
102  return stop;
103  }
104 
110  {
111  return (stop - start);
112  }
113 
118  bool isEmpty() const
119  {
120  return (stop == start);
121  }
122 
128  bool operator==(const Range& r) const
129  {
130  return (start == r.start && stop == r.stop);
131  }
132 
138  bool operator!=(const Range& r) const
139  {
140  return !this->operator==(r);
141  }
142 
147  void swap(Range& r)
148  {
149  if (this == &r)
150  return;
151 
152  std::swap(start, r.start);
153  std::swap(stop, r.stop);
154  }
155 
161  friend void swap(Range& r1, Range& r2)
162  {
163  r1.swap(r2);
164  }
165 
166  private:
167  SizeType start;
168  SizeType stop;
169  };
170 
177  inline Range<std::size_t>
178  range(std::size_t start, std::size_t stop)
179  {
180  return Range<std::size_t>(start, stop);
181  }
182  } // namespace Math
183 } // namespace CDPL
184 
185 #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 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:161
bool operator!=(const Range &r) const
Inequality comparison.
Definition: Range.hpp:138
Range(SizeType start, SizeType stop)
Constructs the range .
Definition: Range.hpp:69
Range()
Constructs an empty range .
Definition: Range.hpp:60
SizeType operator()(SizeType i) const
Maps the local position i to the global index start + i.
Definition: Range.hpp:81
SizeType getStop() const
Returns the upper (exclusive) bound.
Definition: Range.hpp:100
SizeType getStart() const
Returns the lower (inclusive) bound.
Definition: Range.hpp:91
bool isEmpty() const
Tells whether the range is empty.
Definition: Range.hpp:118
void swap(Range &r)
Swaps the contents of *this and r.
Definition: Range.hpp:147
SizeType getSize() const
Returns the size of the range, .
Definition: Range.hpp:109
S SizeType
The integral size/index type.
Definition: Range.hpp:55
bool operator==(const Range &r) const
Equality comparison.
Definition: Range.hpp:128
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:1288
The namespace of the Chemical Data Processing Library.