Chemical Data Processing Library C++ API - Version 1.1.1
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 
43  template <typename S>
44  class Range
45  {
46 
47  typedef Range<S> SelfType;
48 
49  public:
50  typedef S SizeType;
51 
52  Range():
53  start(0), stop(0) {}
54 
55  Range(SizeType start, SizeType stop):
56  start(start), stop(stop)
57  {
58  CDPL_MATH_CHECK(start <= stop, "Invalid range specification", Base::RangeError);
59  }
60 
62  {
63  CDPL_MATH_CHECK(i < getSize(), "Index out of range", Base::IndexError);
64  return (start + i);
65  }
66 
68  {
69  return start;
70  }
71 
72  SizeType getStop() const
73  {
74  return stop;
75  }
76 
77  SizeType getSize() const
78  {
79  return (stop - start);
80  }
81 
82  bool isEmpty() const
83  {
84  return (stop == start);
85  }
86 
87  bool operator==(const Range& r) const
88  {
89  return (start == r.start && stop == r.stop);
90  }
91 
92  bool operator!=(const Range& r) const
93  {
94  return !this->operator==(r);
95  }
96 
97  void swap(Range& r)
98  {
99  if (this == &r)
100  return;
101 
102  std::swap(start, r.start);
103  std::swap(stop, r.stop);
104  }
105 
106  friend void swap(Range& r1, Range& r2)
107  {
108  r1.swap(r2);
109  }
110 
111  private:
112  SizeType start;
113  SizeType stop;
114  };
115 
116  inline Range<std::size_t>
117  range(std::size_t start, std::size_t stop)
118  {
119  return Range<std::size_t>(start, stop);
120  }
121  } // namespace Math
122 } // namespace CDPL
123 
124 #endif // CDPL_MATH_RANGE_HPP
CDPL::Math::Range::operator==
bool operator==(const Range &r) const
Definition: Range.hpp:87
CDPL::Base::RangeError
Thrown to indicate that a value is out of range.
Definition: Base/Exceptions.hpp:114
CDPL::Math::Range::operator!=
bool operator!=(const Range &r) const
Definition: Range.hpp:92
CDPL::Math::Range
Definition: Range.hpp:45
CDPL_MATH_CHECK
#define CDPL_MATH_CHECK(expr, msg, e)
Definition: Check.hpp:36
CDPL::Base::IndexError
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
CDPL::Math::Range::swap
friend void swap(Range &r1, Range &r2)
Definition: Range.hpp:106
CDPL::Math::Range::SizeType
S SizeType
Definition: Range.hpp:50
CDPL::Math::Range::isEmpty
bool isEmpty() const
Definition: Range.hpp:82
CDPL::Chem::AtomConfiguration::S
const unsigned int S
Specifies that the atom has S configuration.
Definition: AtomConfiguration.hpp:63
Exceptions.hpp
Definition of exception classes.
CDPL::Chem::CIPDescriptor::r
const unsigned int r
Specifies that the stereocenter has r configuration.
Definition: CIPDescriptor.hpp:76
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Math::Range::Range
Range(SizeType start, SizeType stop)
Definition: Range.hpp:55
CDPL::Math::Range::swap
void swap(Range &r)
Definition: Range.hpp:97
CDPL::Math::Range::operator()
SizeType operator()(SizeType i) const
Definition: Range.hpp:61
Check.hpp
Definition of various preprocessor macros for error checking.
CDPL::Math::Range::getStop
SizeType getStop() const
Definition: Range.hpp:72
CDPL::Math::Range::getSize
SizeType getSize() const
Definition: Range.hpp:77
CDPL::Math::Range::Range
Range()
Definition: Range.hpp:52
CDPL::Math::range
MatrixRange< E > range(MatrixExpression< E > &e, const typename MatrixRange< E >::RangeType &r1, const typename MatrixRange< E >::RangeType &r2)
Definition: MatrixProxy.hpp:744
CDPL::Math::Range::getStart
SizeType getStart() const
Definition: Range.hpp:67