Chemical Data Processing Library C++ API - Version 1.4.0
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 
52  template <typename S, typename D>
53  class Slice
54  {
55 
56  typedef Slice<S, D> SelfType;
57 
58  public:
60  typedef S SizeType;
62  typedef D DifferenceType;
63 
67  Slice():
68  start(0), stride(0), size(0) {}
69 
77  Slice(SizeType start, DifferenceType stride, SizeType size):
78  start(start), stride(stride), size(size)
79  {
80  CDPL_MATH_CHECK(stride >= 0 || size == 0 || start >= -stride * (size - 1), "Invalid slice specification", Base::RangeError);
81  }
82 
90  {
91  CDPL_MATH_CHECK(i < getSize(), "Index out of range", Base::IndexError);
92  return (start + i * stride);
93  }
94 
100  {
101  return start;
102  }
103 
109  {
110  return stride;
111  }
112 
118  {
119  return size;
120  }
121 
126  bool isEmpty() const
127  {
128  return (size == 0);
129  }
130 
136  bool operator==(const Slice& s) const
137  {
138  return (start == s.start && stride == s.stride && size == s.size);
139  }
140 
146  bool operator!=(const Slice& s) const
147  {
148  return !this->operator==(s);
149  }
150 
155  void swap(Slice& s)
156  {
157  if (this == &s)
158  return;
159 
160  std::swap(start, s.start);
161  std::swap(stride, s.stride);
162  std::swap(size, s.size);
163  }
164 
170  friend void swap(Slice& s1, Slice& s2)
171  {
172  s1.swap(s2);
173  }
174 
175  private:
176  SizeType start;
177  DifferenceType stride;
178  SizeType size;
179  };
180 
188  inline Slice<std::size_t, std::ptrdiff_t>
189  slice(std::size_t start, std::ptrdiff_t stride, std::size_t size)
190  {
191  return Slice<std::size_t, std::ptrdiff_t>(start, stride, size);
192  }
193  } // namespace Math
194 } // namespace CDPL
195 
196 #endif // CDPL_MATH_SLICE_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
Index slice ( ) used for strided slicing of vector and matrix expressions.
Definition: Slice.hpp:54
SizeType operator()(SizeType i) const
Maps the local position i to the global index .
Definition: Slice.hpp:89
friend void swap(Slice &s1, Slice &s2)
Free-function swap overload.
Definition: Slice.hpp:170
S SizeType
The integral size/index type.
Definition: Slice.hpp:60
bool operator!=(const Slice &s) const
Inequality comparison.
Definition: Slice.hpp:146
Slice()
Constructs an empty slice ( ).
Definition: Slice.hpp:67
SizeType getStart() const
Returns the starting global index.
Definition: Slice.hpp:99
bool operator==(const Slice &s) const
Equality comparison.
Definition: Slice.hpp:136
SizeType getSize() const
Returns the number of entries in the slice.
Definition: Slice.hpp:117
DifferenceType getStride() const
Returns the signed step size between consecutive entries.
Definition: Slice.hpp:108
Slice(SizeType start, DifferenceType stride, SizeType size)
Constructs the slice (start, stride, size).
Definition: Slice.hpp:77
D DifferenceType
The signed difference type used for the stride.
Definition: Slice.hpp:62
void swap(Slice &s)
Swaps the contents of *this and s.
Definition: Slice.hpp:155
bool isEmpty() const
Tells whether the slice is empty.
Definition: Slice.hpp:126
constexpr unsigned int S
Specifies that the atom has S configuration.
Definition: AtomConfiguration.hpp:63
constexpr unsigned int D
Specifies Hydrogen (Deuterium).
Definition: AtomType.hpp:62
constexpr unsigned int s
Specifies that the stereocenter has s configuration.
Definition: CIPDescriptor.hpp:81
MatrixSlice< E > slice(MatrixExpression< E > &e, const typename MatrixSlice< E >::SliceType &s1, const typename MatrixSlice< E >::SliceType &s2)
Returns a mutable matrix slice proxy viewing the strided rectangular slice (s1, s2) of e.
Definition: MatrixProxy.hpp:1368
The namespace of the Chemical Data Processing Library.