Chemical Data Processing Library C++ API - Version 1.4.0
VectorProxy.hpp
Go to the documentation of this file.
1 /*
2  * VectorProxy.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_VECTORPROXY_HPP
28 #define CDPL_MATH_VECTORPROXY_HPP
29 
30 #include <type_traits>
31 
32 #include "CDPL/Math/Expression.hpp"
34 #include "CDPL/Math/TypeTraits.hpp"
35 #include "CDPL/Math/Functional.hpp"
36 #include "CDPL/Math/Range.hpp"
37 #include "CDPL/Math/Slice.hpp"
38 
39 
40 namespace CDPL
41 {
42 
43  namespace Math
44  {
45 
50  template <typename V>
51  class VectorRange : public VectorExpression<VectorRange<V> >
52  {
53 
54  typedef VectorRange<V> SelfType;
55 
56  public:
58  typedef V VectorType;
60  typedef typename V::SizeType SizeType;
62  typedef typename V::DifferenceType DifferenceType;
64  typedef typename V::ValueType ValueType;
66  typedef typename V::ConstReference ConstReference;
68  typedef typename std::conditional<std::is_const<V>::value,
69  typename V::ConstReference,
70  typename V::Reference>::type Reference;
72  typedef typename std::conditional<std::is_const<V>::value,
73  typename V::ConstClosureType,
74  typename V::ClosureType>::type VectorClosureType;
76  typedef const SelfType ConstClosureType;
81 
88  data(v), range(r) {}
89 
96  {
97  return data(range(i));
98  }
99 
106  {
107  return data(range(i));
108  }
109 
116  {
117  return data[range(i)];
118  }
119 
126  {
127  return data[range(i)];
128  }
129 
135  {
136  return range.getStart();
137  }
138 
144  {
145  return range.getSize();
146  }
147 
152  bool isEmpty() const
153  {
154  return range.isEmpty();
155  }
156 
162  {
163  return data;
164  }
165 
170  const VectorClosureType& getData() const
171  {
172  return data;
173  }
174 
181  {
182  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(r));
183  return *this;
184  }
185 
192  template <typename E>
194  {
195  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(e));
196  return *this;
197  }
198 
205  template <typename E>
207  {
208  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this + e));
209  return *this;
210  }
211 
218  template <typename E>
220  {
221  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this - e));
222  return *this;
223  }
224 
231  template <typename T>
232  typename std::enable_if<IsScalar<T>::value, VectorRange>::type& operator*=(const T& t)
233  {
234  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
235  return *this;
236  }
237 
244  template <typename T>
245  typename std::enable_if<IsScalar<T>::value, VectorRange>::type& operator/=(const T& t)
246  {
247  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
248  return *this;
249  }
250 
258  template <typename E>
260  {
261  vectorAssignVector<ScalarAssignment>(*this, e);
262  return *this;
263  }
264 
271  template <typename E>
273  {
274  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
275  return *this;
276  }
277 
284  template <typename E>
286  {
287  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
288  return *this;
289  }
290 
296  {
297  if (this != &r)
298  vectorSwap(*this, r);
299  }
300 
306  friend void swap(VectorRange& r1, VectorRange& r2)
307  {
308  r1.swap(r2);
309  }
310 
311  private:
312  VectorClosureType data;
313  RangeType range;
314  };
315 
320  template <typename V>
321  class VectorSlice : public VectorExpression<VectorSlice<V> >
322  {
323 
324  typedef VectorSlice<V> SelfType;
325 
326  public:
328  typedef V VectorType;
330  typedef typename V::SizeType SizeType;
332  typedef typename V::DifferenceType DifferenceType;
334  typedef typename V::ValueType ValueType;
336  typedef typename V::ConstReference ConstReference;
338  typedef typename std::conditional<std::is_const<V>::value,
339  typename V::ConstReference,
340  typename V::Reference>::type Reference;
342  typedef typename std::conditional<std::is_const<V>::value,
343  typename V::ConstClosureType,
344  typename V::ClosureType>::type VectorClosureType;
346  typedef const SelfType ConstClosureType;
351 
358  data(v), slice(s) {}
359 
366  {
367  return data(slice(i));
368  }
369 
376  {
377  return data(slice(i));
378  }
379 
386  {
387  return data[slice(i)];
388  }
389 
396  {
397  return data[slice(i)];
398  }
399 
405  {
406  return slice.getStart();
407  }
408 
414  {
415  return slice.getStride();
416  }
417 
423  {
424  return slice.getSize();
425  }
426 
431  bool isEmpty() const
432  {
433  return slice.isEmpty();
434  }
435 
441  {
442  return data;
443  }
444 
449  const VectorClosureType& getData() const
450  {
451  return data;
452  }
453 
460  {
461  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(s));
462  return *this;
463  }
464 
471  template <typename E>
473  {
474  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(e));
475  return *this;
476  }
477 
484  template <typename E>
486  {
487  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this + e));
488  return *this;
489  }
490 
497  template <typename E>
499  {
500  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this - e));
501  return *this;
502  }
503 
510  template <typename T>
511  typename std::enable_if<IsScalar<T>::value, VectorSlice>::type& operator*=(const T& t)
512  {
513  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
514  return *this;
515  }
516 
523  template <typename T>
524  typename std::enable_if<IsScalar<T>::value, VectorSlice>::type& operator/=(const T& t)
525  {
526  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
527  return *this;
528  }
529 
537  template <typename E>
539  {
540  vectorAssignVector<ScalarAssignment>(*this, e);
541  return *this;
542  }
543 
550  template <typename E>
552  {
553  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
554  return *this;
555  }
556 
563  template <typename E>
565  {
566  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
567  return *this;
568  }
569 
575  {
576  if (this != &s)
577  vectorSwap(*this, s);
578  }
579 
585  friend void swap(VectorSlice& s1, VectorSlice& s2)
586  {
587  s1.swap(s2);
588  }
589 
590  private:
591  VectorClosureType data;
592  SliceType slice;
593  };
594 
599  template <typename V>
601  {};
602 
607  template <typename V>
609  {};
610 
615  template <typename V>
617  {};
618 
623  template <typename V>
625  {};
626 
634  template <typename E>
637  {
638  return VectorRange<E>(e(), r);
639  }
640 
648  template <typename E>
649  VectorRange<const E>
651  {
652  return VectorRange<const E>(e(), r);
653  }
654 
663  template <typename E>
664  VectorRange<E>
668  {
669  typedef typename VectorRange<E>::RangeType RangeType;
670 
671  return VectorRange<E>(e(), RangeType(start, stop));
672  }
673 
682  template <typename E>
683  VectorRange<const E>
687  {
688  typedef typename VectorRange<const E>::RangeType RangeType;
689 
690  return VectorRange<const E>(e(), RangeType(start, stop));
691  }
692 
700  template <typename E>
701  VectorSlice<E>
703  {
704  return VectorSlice<E>(e(), s);
705  }
706 
714  template <typename E>
715  VectorSlice<const E>
717  {
718  return VectorSlice<const E>(e(), s);
719  }
720 
730  template <typename E>
731  VectorSlice<E>
736  {
737  typedef typename VectorSlice<E>::SliceType SliceType;
738 
739  return VectorSlice<E>(e(), SliceType(start, stride, size));
740  }
741 
751  template <typename E>
752  VectorSlice<const E>
757  {
758  typedef typename VectorSlice<const E>::SliceType SliceType;
759 
760  return VectorSlice<const E>(e(), SliceType(start, stride, size));
761  }
762  } // namespace Math
763 } // namespace CDPL
764 
765 #endif // CDPL_MATH_VECTORPROXY_HPP
Definition of basic expression types.
Definition of various functors.
Definition of a data type for describing index ranges.
Definition of a data type for describing index slices.
Definition of type traits.
Implementation of vector assignment routines.
SizeType getStart() const
Returns the lower (inclusive) bound.
Definition: Range.hpp:91
bool isEmpty() const
Tells whether the range is empty.
Definition: Range.hpp:118
SizeType getSize() const
Returns the size of the range, .
Definition: Range.hpp:109
SizeType SizeType
The integral size/index type.
Definition: Range.hpp:55
SizeType SizeType
The integral size/index type.
Definition: Slice.hpp:60
SizeType getStart() const
Returns the starting global index.
Definition: Slice.hpp:99
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
DifferenceType DifferenceType
The signed difference type used for the stride.
Definition: Slice.hpp:62
bool isEmpty() const
Tells whether the slice is empty.
Definition: Slice.hpp:126
CRTP base class for all vector expression types.
Definition: Expression.hpp:66
Vector-expression proxy that views a contiguous half-open subrange of an underlying vector.
Definition: VectorProxy.hpp:52
bool isEmpty() const
Tells whether the view is empty (zero-length range).
Definition: VectorProxy.hpp:152
SizeType getStart() const
Returns the start index of the viewed range within the wrapped vector.
Definition: VectorProxy.hpp:134
Reference operator[](SizeType i)
Returns a mutable reference to the element at index i of the view (alias for operator()).
Definition: VectorProxy.hpp:115
VectorRange & assign(const VectorExpression< E > &e)
Assigns the vector expression e to this view without intermediate temporary (use only when e does not...
Definition: VectorProxy.hpp:259
VectorClosureType & getData()
Returns a reference to the wrapped vector (via its stored closure).
Definition: VectorProxy.hpp:161
Reference operator()(SizeType i)
Returns a mutable reference to the element at index i of the view.
Definition: VectorProxy.hpp:95
friend void swap(VectorRange &r1, VectorRange &r2)
ADL-enabled free-function form of swap().
Definition: VectorProxy.hpp:306
ConstReference operator[](SizeType i) const
Returns a const reference to the element at index i of the view (alias for operator()).
Definition: VectorProxy.hpp:125
VectorRange & operator-=(const VectorExpression< E > &e)
Subtracts the vector expression e componentwise from this view.
Definition: VectorProxy.hpp:219
V VectorType
The wrapped vector type.
Definition: VectorProxy.hpp:58
std::enable_if< IsScalar< T >::value, VectorRange >::type & operator/=(const T &t)
Divides every element of this view by the scalar t.
Definition: VectorProxy.hpp:245
SizeType getSize() const
Returns the size of the view (number of elements covered by the range).
Definition: VectorProxy.hpp:143
VectorRange(VectorType &v, const RangeType &r)
Constructs the proxy viewing the subrange r of v.
Definition: VectorProxy.hpp:87
SelfType ClosureType
Closure type used when this proxy appears inside another expression.
Definition: VectorProxy.hpp:78
std::conditional< std::is_const< V >::value, typename V::ConstClosureType, typename V::ClosureType >::type VectorClosureType
Closure type used to store the wrapped vector internally (mutable or const flavor).
Definition: VectorProxy.hpp:74
const VectorClosureType & getData() const
Returns a const reference to the wrapped vector (via its stored closure).
Definition: VectorProxy.hpp:170
VectorRange & operator=(const VectorRange &r)
Copies the elements of r into this view (writing through to the wrapped vector).
Definition: VectorProxy.hpp:180
V::ConstReference ConstReference
Constant reference type to an element.
Definition: VectorProxy.hpp:66
V::ValueType ValueType
The element value type of the wrapped vector.
Definition: VectorProxy.hpp:64
VectorRange & operator=(const VectorExpression< E > &e)
Assigns the vector expression e to this view (writing through to the wrapped vector).
Definition: VectorProxy.hpp:193
VectorRange & plusAssign(const VectorExpression< E > &e)
Adds the vector expression e to this view without intermediate temporary.
Definition: VectorProxy.hpp:272
V::DifferenceType DifferenceType
The signed difference type used by the wrapped vector.
Definition: VectorProxy.hpp:62
VectorRange & minusAssign(const VectorExpression< E > &e)
Subtracts the vector expression e from this view without intermediate temporary.
Definition: VectorProxy.hpp:285
void swap(VectorRange &r)
Swaps the elements of this view with those of r.
Definition: VectorProxy.hpp:295
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Mutable reference type (degrades to ConstReference when the wrapped vector is const).
Definition: VectorProxy.hpp:70
Range< SizeType > RangeType
The Math::Range type defining the half-open index subrange.
Definition: VectorProxy.hpp:80
const SelfType ConstClosureType
Constant closure type used when this proxy appears inside another expression.
Definition: VectorProxy.hpp:76
V::SizeType SizeType
The size type used by the wrapped vector.
Definition: VectorProxy.hpp:60
VectorRange & operator+=(const VectorExpression< E > &e)
Adds the vector expression e componentwise to this view.
Definition: VectorProxy.hpp:206
std::enable_if< IsScalar< T >::value, VectorRange >::type & operator*=(const T &t)
Multiplies every element of this view by the scalar t.
Definition: VectorProxy.hpp:232
ConstReference operator()(SizeType i) const
Returns a const reference to the element at index i of the view.
Definition: VectorProxy.hpp:105
Vector-expression proxy that views a strided slice of an underlying vector.
Definition: VectorProxy.hpp:322
VectorSlice & operator=(const VectorExpression< E > &e)
Assigns the vector expression e to this view (writing through to the wrapped vector).
Definition: VectorProxy.hpp:472
ConstReference operator()(SizeType i) const
Returns a const reference to the element at index i of the view.
Definition: VectorProxy.hpp:375
V::DifferenceType DifferenceType
The signed difference type used by the wrapped vector.
Definition: VectorProxy.hpp:332
Slice< SizeType, DifferenceType > SliceType
The Math::Slice type defining the (start, stride, size) selection.
Definition: VectorProxy.hpp:350
ConstReference operator[](SizeType i) const
Returns a const reference to the element at index i of the view (alias for operator()).
Definition: VectorProxy.hpp:395
const VectorClosureType & getData() const
Returns a const reference to the wrapped vector (via its stored closure).
Definition: VectorProxy.hpp:449
Reference operator[](SizeType i)
Returns a mutable reference to the element at index i of the view (alias for operator()).
Definition: VectorProxy.hpp:385
V::ValueType ValueType
The element value type of the wrapped vector.
Definition: VectorProxy.hpp:334
VectorSlice & plusAssign(const VectorExpression< E > &e)
Adds the vector expression e to this view without intermediate temporary.
Definition: VectorProxy.hpp:551
const SelfType ConstClosureType
Constant closure type used when this proxy appears inside another expression.
Definition: VectorProxy.hpp:346
VectorSlice & operator+=(const VectorExpression< E > &e)
Adds the vector expression e componentwise to this view.
Definition: VectorProxy.hpp:485
std::enable_if< IsScalar< T >::value, VectorSlice >::type & operator*=(const T &t)
Multiplies every element of this view by the scalar t.
Definition: VectorProxy.hpp:511
SizeType getSize() const
Returns the size of the view (number of elements covered by the slice).
Definition: VectorProxy.hpp:422
Reference operator()(SizeType i)
Returns a mutable reference to the element at index i of the view.
Definition: VectorProxy.hpp:365
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Mutable reference type (degrades to ConstReference when the wrapped vector is const).
Definition: VectorProxy.hpp:340
bool isEmpty() const
Tells whether the view is empty (zero-length slice).
Definition: VectorProxy.hpp:431
SizeType getStart() const
Returns the start index of the slice within the wrapped vector.
Definition: VectorProxy.hpp:404
DifferenceType getStride() const
Returns the stride of the slice (step between successive viewed elements).
Definition: VectorProxy.hpp:413
V VectorType
The wrapped vector type.
Definition: VectorProxy.hpp:328
std::enable_if< IsScalar< T >::value, VectorSlice >::type & operator/=(const T &t)
Divides every element of this view by the scalar t.
Definition: VectorProxy.hpp:524
friend void swap(VectorSlice &s1, VectorSlice &s2)
ADL-enabled free-function form of swap().
Definition: VectorProxy.hpp:585
void swap(VectorSlice &s)
Swaps the elements of this view with those of s.
Definition: VectorProxy.hpp:574
VectorClosureType & getData()
Returns a reference to the wrapped vector (via its stored closure).
Definition: VectorProxy.hpp:440
V::SizeType SizeType
The size type used by the wrapped vector.
Definition: VectorProxy.hpp:330
V::ConstReference ConstReference
Constant reference type to an element.
Definition: VectorProxy.hpp:336
VectorSlice & minusAssign(const VectorExpression< E > &e)
Subtracts the vector expression e from this view without intermediate temporary.
Definition: VectorProxy.hpp:564
VectorSlice(VectorType &v, const SliceType &s)
Constructs the proxy viewing the slice s of v.
Definition: VectorProxy.hpp:357
VectorSlice & operator=(const VectorSlice &s)
Copies the elements of s into this view (writing through to the wrapped vector).
Definition: VectorProxy.hpp:459
std::conditional< std::is_const< V >::value, typename V::ConstClosureType, typename V::ClosureType >::type VectorClosureType
Closure type used to store the wrapped vector internally (mutable or const flavor).
Definition: VectorProxy.hpp:344
VectorSlice & assign(const VectorExpression< E > &e)
Assigns the vector expression e to this view without intermediate temporary (use only when e does not...
Definition: VectorProxy.hpp:538
VectorSlice & operator-=(const VectorExpression< E > &e)
Subtracts the vector expression e componentwise from this view.
Definition: VectorProxy.hpp:498
SelfType ClosureType
Closure type used when this proxy appears inside another expression.
Definition: VectorProxy.hpp:348
constexpr unsigned int V
Specifies Vanadium.
Definition: AtomType.hpp:177
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
constexpr unsigned int r
Specifies that the stereocenter has r configuration.
Definition: CIPDescriptor.hpp:76
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
void vectorSwap(V &v, VectorExpression< E > &e)
Swaps the elements of two equally sized vector expressions element by element.
Definition: VectorAssignment.hpp:97
MatrixRange< E > range(MatrixExpression< E > &e, const typename MatrixRange< E >::RangeType &r1, const typename MatrixRange< E >::RangeType &r2)
Returns a mutable matrix range proxy viewing rows in r1 and columns in r2 of e.
Definition: MatrixProxy.hpp:1288
The namespace of the Chemical Data Processing Library.
Selects a concrete temporary vector type compatible with the vector expression V.
Definition: TypeTraits.hpp:301
V::VectorTemporaryType Type
The concrete temporary vector type compatible with the vector expression V.
Definition: TypeTraits.hpp:304