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