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:
62  typedef S SizeType;
63 
67  typedef D DifferenceType;
68 
72  Slice():
73  start(0), stride(0), size(0) {}
74 
82  Slice(SizeType start, DifferenceType stride, SizeType size):
83  start(start), stride(stride), size(size)
84  {
85  CDPL_MATH_CHECK(stride >= 0 || size == 0 || start >= -stride * (size - 1), "Invalid slice specification", Base::RangeError);
86  }
87 
95  {
96  CDPL_MATH_CHECK(i < getSize(), "Index out of range", Base::IndexError);
97  return (start + i * stride);
98  }
99 
105  {
106  return start;
107  }
108 
114  {
115  return stride;
116  }
117 
123  {
124  return size;
125  }
126 
131  bool isEmpty() const
132  {
133  return (size == 0);
134  }
135 
141  bool operator==(const Slice& s) const
142  {
143  return (start == s.start && stride == s.stride && size == s.size);
144  }
145 
151  bool operator!=(const Slice& s) const
152  {
153  return !this->operator==(s);
154  }
155 
160  void swap(Slice& s)
161  {
162  if (this == &s)
163  return;
164 
165  std::swap(start, s.start);
166  std::swap(stride, s.stride);
167  std::swap(size, s.size);
168  }
169 
175  friend void swap(Slice& s1, Slice& s2)
176  {
177  s1.swap(s2);
178  }
179 
180  private:
181  SizeType start;
182  DifferenceType stride;
183  SizeType size;
184  };
185 
193  inline Slice<std::size_t, std::ptrdiff_t>
194  slice(std::size_t start, std::ptrdiff_t stride, std::size_t size)
195  {
196  return Slice<std::size_t, std::ptrdiff_t>(start, stride, size);
197  }
198  } // namespace Math
199 } // namespace CDPL
200 
201 #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 (start, stride, size ) 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:94
friend void swap(Slice &s1, Slice &s2)
Free-function swap overload.
Definition: Slice.hpp:175
S SizeType
The integral size/index type.
Definition: Slice.hpp:62
bool operator!=(const Slice &s) const
Inequality comparison.
Definition: Slice.hpp:151
Slice()
Constructs an empty slice.
Definition: Slice.hpp:72
SizeType getStart() const
Returns the starting global index.
Definition: Slice.hpp:104
bool operator==(const Slice &s) const
Equality comparison.
Definition: Slice.hpp:141
SizeType getSize() const
Returns the number of entries in the slice.
Definition: Slice.hpp:122
DifferenceType getStride() const
Returns the signed step size between consecutive entries.
Definition: Slice.hpp:113
Slice(SizeType start, DifferenceType stride, SizeType size)
Constructs the slice (start, stride, size).
Definition: Slice.hpp:82
D DifferenceType
The signed difference type used for the stride.
Definition: Slice.hpp:67
void swap(Slice &s)
Swaps the contents of *this and s.
Definition: Slice.hpp:160
bool isEmpty() const
Tells whether the slice is empty.
Definition: Slice.hpp:131
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:1509
The namespace of the Chemical Data Processing Library.