Chemical Data Processing Library C++ API - Version 1.2.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 
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
Definition of exception classes.
Definition of various preprocessor macros for error checking.
#define CDPL_MATH_CHECK(expr, msg, e)
Definition: Check.hpp:36
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
Definition: Slice.hpp:45
SizeType operator()(SizeType i) const
Definition: Slice.hpp:62
friend void swap(Slice &s1, Slice &s2)
Definition: Slice.hpp:108
S SizeType
Definition: Slice.hpp:50
bool operator!=(const Slice &s) const
Definition: Slice.hpp:93
Slice()
Definition: Slice.hpp:53
SizeType getStart() const
Definition: Slice.hpp:68
bool operator==(const Slice &s) const
Definition: Slice.hpp:88
SizeType getSize() const
Definition: Slice.hpp:78
DifferenceType getStride() const
Definition: Slice.hpp:73
Slice(SizeType start, DifferenceType stride, SizeType size)
Definition: Slice.hpp:56
D DifferenceType
Definition: Slice.hpp:51
void swap(Slice &s)
Definition: Slice.hpp:98
bool isEmpty() const
Definition: Slice.hpp:83
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)
Definition: MatrixProxy.hpp:788
The namespace of the Chemical Data Processing Library.