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:
60  typedef V VectorType;
61 
65  typedef typename V::SizeType SizeType;
66 
70  typedef typename V::DifferenceType DifferenceType;
71 
75  typedef typename V::ValueType ValueType;
76 
80  typedef typename V::ConstReference ConstReference;
81 
85  typedef typename std::conditional<std::is_const<V>::value,
86  typename V::ConstReference,
87  typename V::Reference>::type Reference;
88 
92  typedef typename std::conditional<std::is_const<V>::value,
93  typename V::ConstClosureType,
94  typename V::ClosureType>::type VectorClosureType;
95 
99  typedef const SelfType ConstClosureType;
100 
105 
110 
117  data(v), range(r) {}
118 
125  {
126  return data(range(i));
127  }
128 
135  {
136  return data(range(i));
137  }
138 
145  {
146  return data[range(i)];
147  }
148 
155  {
156  return data[range(i)];
157  }
158 
164  {
165  return range.getStart();
166  }
167 
173  {
174  return range.getSize();
175  }
176 
181  bool isEmpty() const
182  {
183  return range.isEmpty();
184  }
185 
191  {
192  return data;
193  }
194 
199  const VectorClosureType& getData() const
200  {
201  return data;
202  }
203 
210  {
211  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(r));
212  return *this;
213  }
214 
221  template <typename E>
223  {
224  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(e));
225  return *this;
226  }
227 
234  template <typename E>
236  {
237  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this + e));
238  return *this;
239  }
240 
247  template <typename E>
249  {
250  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this - e));
251  return *this;
252  }
253 
260  template <typename T>
261  typename std::enable_if<IsScalar<T>::value, VectorRange>::type& operator*=(const T& t)
262  {
263  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
264  return *this;
265  }
266 
273  template <typename T>
274  typename std::enable_if<IsScalar<T>::value, VectorRange>::type& operator/=(const T& t)
275  {
276  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
277  return *this;
278  }
279 
287  template <typename E>
289  {
290  vectorAssignVector<ScalarAssignment>(*this, e);
291  return *this;
292  }
293 
300  template <typename E>
302  {
303  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
304  return *this;
305  }
306 
313  template <typename E>
315  {
316  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
317  return *this;
318  }
319 
325  {
326  if (this != &r)
327  vectorSwap(*this, r);
328  }
329 
335  friend void swap(VectorRange& r1, VectorRange& r2)
336  {
337  r1.swap(r2);
338  }
339 
340  private:
341  VectorClosureType data;
342  RangeType range;
343  };
344 
349  template <typename V>
350  class VectorSlice : public VectorExpression<VectorSlice<V> >
351  {
352 
353  typedef VectorSlice<V> SelfType;
354 
355  public:
359  typedef V VectorType;
360 
364  typedef typename V::SizeType SizeType;
365 
369  typedef typename V::DifferenceType DifferenceType;
370 
374  typedef typename V::ValueType ValueType;
375 
379  typedef typename V::ConstReference ConstReference;
380 
384  typedef typename std::conditional<std::is_const<V>::value,
385  typename V::ConstReference,
386  typename V::Reference>::type Reference;
387 
391  typedef typename std::conditional<std::is_const<V>::value,
392  typename V::ConstClosureType,
393  typename V::ClosureType>::type VectorClosureType;
394 
398  typedef const SelfType ConstClosureType;
399 
404 
409 
416  data(v), slice(s) {}
417 
424  {
425  return data(slice(i));
426  }
427 
434  {
435  return data(slice(i));
436  }
437 
444  {
445  return data[slice(i)];
446  }
447 
454  {
455  return data[slice(i)];
456  }
457 
463  {
464  return slice.getStart();
465  }
466 
472  {
473  return slice.getStride();
474  }
475 
481  {
482  return slice.getSize();
483  }
484 
489  bool isEmpty() const
490  {
491  return slice.isEmpty();
492  }
493 
499  {
500  return data;
501  }
502 
507  const VectorClosureType& getData() const
508  {
509  return data;
510  }
511 
518  {
519  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(s));
520  return *this;
521  }
522 
529  template <typename E>
531  {
532  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(e));
533  return *this;
534  }
535 
542  template <typename E>
544  {
545  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this + e));
546  return *this;
547  }
548 
555  template <typename E>
557  {
558  vectorAssignVector<ScalarAssignment>(*this, typename VectorTemporaryTraits<V>::Type(*this - e));
559  return *this;
560  }
561 
568  template <typename T>
569  typename std::enable_if<IsScalar<T>::value, VectorSlice>::type& operator*=(const T& t)
570  {
571  vectorAssignScalar<ScalarMultiplicationAssignment>(*this, t);
572  return *this;
573  }
574 
581  template <typename T>
582  typename std::enable_if<IsScalar<T>::value, VectorSlice>::type& operator/=(const T& t)
583  {
584  vectorAssignScalar<ScalarDivisionAssignment>(*this, t);
585  return *this;
586  }
587 
595  template <typename E>
597  {
598  vectorAssignVector<ScalarAssignment>(*this, e);
599  return *this;
600  }
601 
608  template <typename E>
610  {
611  vectorAssignVector<ScalarAdditionAssignment>(*this, e);
612  return *this;
613  }
614 
621  template <typename E>
623  {
624  vectorAssignVector<ScalarSubtractionAssignment>(*this, e);
625  return *this;
626  }
627 
633  {
634  if (this != &s)
635  vectorSwap(*this, s);
636  }
637 
643  friend void swap(VectorSlice& s1, VectorSlice& s2)
644  {
645  s1.swap(s2);
646  }
647 
648  private:
649  VectorClosureType data;
650  SliceType slice;
651  };
652 
657  template <typename V>
659  {};
660 
665  template <typename V>
667  {};
668 
673  template <typename V>
675  {};
676 
681  template <typename V>
683  {};
684 
692  template <typename E>
695  {
696  return VectorRange<E>(e(), r);
697  }
698 
706  template <typename E>
707  VectorRange<const E>
709  {
710  return VectorRange<const E>(e(), r);
711  }
712 
721  template <typename E>
722  VectorRange<E>
726  {
727  typedef typename VectorRange<E>::RangeType RangeType;
728 
729  return VectorRange<E>(e(), RangeType(start, stop));
730  }
731 
740  template <typename E>
741  VectorRange<const E>
745  {
746  typedef typename VectorRange<const E>::RangeType RangeType;
747 
748  return VectorRange<const E>(e(), RangeType(start, stop));
749  }
750 
758  template <typename E>
759  VectorSlice<E>
761  {
762  return VectorSlice<E>(e(), s);
763  }
764 
772  template <typename E>
773  VectorSlice<const E>
775  {
776  return VectorSlice<const E>(e(), s);
777  }
778 
788  template <typename E>
789  VectorSlice<E>
794  {
795  typedef typename VectorSlice<E>::SliceType SliceType;
796 
797  return VectorSlice<E>(e(), SliceType(start, stride, size));
798  }
799 
809  template <typename E>
810  VectorSlice<const E>
815  {
816  typedef typename VectorSlice<const E>::SliceType SliceType;
817 
818  return VectorSlice<const E>(e(), SliceType(start, stride, size));
819  }
820  } // namespace Math
821 } // namespace CDPL
822 
823 #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:93
bool isEmpty() const
Tells whether the range is empty.
Definition: Range.hpp:120
SizeType getSize() const
Returns the size of the range.
Definition: Range.hpp:111
SizeType SizeType
The integral size/index type.
Definition: Range.hpp:57
SizeType SizeType
The integral size/index type.
Definition: Slice.hpp:62
SizeType getStart() const
Returns the starting global index.
Definition: Slice.hpp:104
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
DifferenceType DifferenceType
The signed difference type used for the stride.
Definition: Slice.hpp:67
bool isEmpty() const
Tells whether the slice is empty.
Definition: Slice.hpp:131
CRTP base class of all vector expression types.
Definition: Expression.hpp:68
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:181
SizeType getStart() const
Returns the start index of the viewed range within the wrapped vector.
Definition: VectorProxy.hpp:163
Reference operator[](SizeType i)
Returns a mutable reference to the element at index i of the view.
Definition: VectorProxy.hpp:144
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:288
VectorClosureType & getData()
Returns a reference to the wrapped vector (via its stored closure).
Definition: VectorProxy.hpp:190
Reference operator()(SizeType i)
Returns a mutable reference to the element at index i of the view.
Definition: VectorProxy.hpp:124
friend void swap(VectorRange &r1, VectorRange &r2)
ADL-enabled free-function form of swap().
Definition: VectorProxy.hpp:335
ConstReference operator[](SizeType i) const
Returns a const reference to the element at index i of the view.
Definition: VectorProxy.hpp:154
VectorRange & operator-=(const VectorExpression< E > &e)
Subtracts the vector expression e componentwise from this view.
Definition: VectorProxy.hpp:248
V VectorType
The wrapped vector type.
Definition: VectorProxy.hpp:60
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:274
SizeType getSize() const
Returns the size of the view (number of elements covered by the range).
Definition: VectorProxy.hpp:172
VectorRange(VectorType &v, const RangeType &r)
Constructs the proxy viewing the subrange r of v.
Definition: VectorProxy.hpp:116
SelfType ClosureType
Closure type used when this proxy appears inside another expression.
Definition: VectorProxy.hpp:104
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:94
const VectorClosureType & getData() const
Returns a const reference to the wrapped vector (via its stored closure).
Definition: VectorProxy.hpp:199
VectorRange & operator=(const VectorRange &r)
Copies the elements of r into this view (writing through to the wrapped vector).
Definition: VectorProxy.hpp:209
V::ConstReference ConstReference
Constant element reference type.
Definition: VectorProxy.hpp:80
V::ValueType ValueType
The element value type of the wrapped vector.
Definition: VectorProxy.hpp:75
VectorRange & operator=(const VectorExpression< E > &e)
Assigns the vector expression e to this view (writing through to the wrapped vector).
Definition: VectorProxy.hpp:222
VectorRange & plusAssign(const VectorExpression< E > &e)
Adds the vector expression e to this view without intermediate temporary.
Definition: VectorProxy.hpp:301
V::DifferenceType DifferenceType
The signed difference type used by the wrapped vector.
Definition: VectorProxy.hpp:70
VectorRange & minusAssign(const VectorExpression< E > &e)
Subtracts the vector expression e from this view without intermediate temporary.
Definition: VectorProxy.hpp:314
void swap(VectorRange &r)
Swaps the elements of this view with those of r.
Definition: VectorProxy.hpp:324
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Mutable element reference type (degrades to ConstReference when the wrapped vector is const).
Definition: VectorProxy.hpp:87
Range< SizeType > RangeType
The Math::Range type defining the half-open index subrange.
Definition: VectorProxy.hpp:109
const SelfType ConstClosureType
Constant closure type used when this proxy appears inside another expression.
Definition: VectorProxy.hpp:99
V::SizeType SizeType
The size type used by the wrapped vector.
Definition: VectorProxy.hpp:65
VectorRange & operator+=(const VectorExpression< E > &e)
Adds the vector expression e componentwise to this view.
Definition: VectorProxy.hpp:235
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:261
ConstReference operator()(SizeType i) const
Returns a const reference to the element at index i of the view.
Definition: VectorProxy.hpp:134
Vector expression proxy that views a strided slice of an underlying vector.
Definition: VectorProxy.hpp:351
VectorSlice & operator=(const VectorExpression< E > &e)
Assigns the vector expression e to this view (writing through to the wrapped vector).
Definition: VectorProxy.hpp:530
ConstReference operator()(SizeType i) const
Returns a const reference to the element at index i of the view.
Definition: VectorProxy.hpp:433
V::DifferenceType DifferenceType
The signed difference type used by the wrapped vector.
Definition: VectorProxy.hpp:369
Slice< SizeType, DifferenceType > SliceType
The Math::Slice type defining the (start, stride, size) selection.
Definition: VectorProxy.hpp:408
ConstReference operator[](SizeType i) const
Returns a const reference to the element at index i of the view.
Definition: VectorProxy.hpp:453
const VectorClosureType & getData() const
Returns a const reference to the wrapped vector (via its stored closure).
Definition: VectorProxy.hpp:507
Reference operator[](SizeType i)
Returns a mutable reference to the element at index i of the view.
Definition: VectorProxy.hpp:443
V::ValueType ValueType
The element value type of the wrapped vector.
Definition: VectorProxy.hpp:374
VectorSlice & plusAssign(const VectorExpression< E > &e)
Adds the vector expression e to this view without intermediate temporary.
Definition: VectorProxy.hpp:609
const SelfType ConstClosureType
Constant closure type used when this proxy appears inside another expression.
Definition: VectorProxy.hpp:398
VectorSlice & operator+=(const VectorExpression< E > &e)
Adds the vector expression e componentwise to this view.
Definition: VectorProxy.hpp:543
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:569
SizeType getSize() const
Returns the size of the view (number of elements covered by the slice).
Definition: VectorProxy.hpp:480
Reference operator()(SizeType i)
Returns a mutable reference to the element at index i of the view.
Definition: VectorProxy.hpp:423
std::conditional< std::is_const< V >::value, typename V::ConstReference, typename V::Reference >::type Reference
Mutable element reference type (degrades to ConstReference when the wrapped vector is const).
Definition: VectorProxy.hpp:386
bool isEmpty() const
Tells whether the view is empty (zero-length slice).
Definition: VectorProxy.hpp:489
SizeType getStart() const
Returns the start index of the slice within the wrapped vector.
Definition: VectorProxy.hpp:462
DifferenceType getStride() const
Returns the stride of the slice (step between successive viewed elements).
Definition: VectorProxy.hpp:471
V VectorType
The wrapped vector type.
Definition: VectorProxy.hpp:359
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:582
friend void swap(VectorSlice &s1, VectorSlice &s2)
ADL-enabled free-function form of swap().
Definition: VectorProxy.hpp:643
void swap(VectorSlice &s)
Swaps the elements of this view with those of s.
Definition: VectorProxy.hpp:632
VectorClosureType & getData()
Returns a reference to the wrapped vector (via its stored closure).
Definition: VectorProxy.hpp:498
V::SizeType SizeType
The size type used by the wrapped vector.
Definition: VectorProxy.hpp:364
V::ConstReference ConstReference
Constant element reference type.
Definition: VectorProxy.hpp:379
VectorSlice & minusAssign(const VectorExpression< E > &e)
Subtracts the vector expression e from this view without intermediate temporary.
Definition: VectorProxy.hpp:622
VectorSlice(VectorType &v, const SliceType &s)
Constructs the proxy viewing the slice s of v.
Definition: VectorProxy.hpp:415
VectorSlice & operator=(const VectorSlice &s)
Copies the elements of s into this view (writing through to the wrapped vector).
Definition: VectorProxy.hpp:517
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:393
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:596
VectorSlice & operator-=(const VectorExpression< E > &e)
Subtracts the vector expression e componentwise from this view.
Definition: VectorProxy.hpp:556
SelfType ClosureType
Closure type used when this proxy appears inside another expression.
Definition: VectorProxy.hpp:403
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:1509
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:1429
The namespace of the Chemical Data Processing Library.
Selects a concrete temporary vector type compatible with the vector expression V.
Definition: TypeTraits.hpp:323
V::VectorTemporaryType Type
The concrete temporary vector type compatible with the vector expression V.
Definition: TypeTraits.hpp:328