ROL
ROL_VectorClone.hpp
Go to the documentation of this file.
1 #pragma once
2 #ifndef ROL_VECTORCLONE_HPP
3 #define ROL_VECTORCLONE_HPP
4 
5 // @HEADER
6 // *****************************************************************************
7 // Rapid Optimization Library (ROL) Package
8 //
9 // Copyright 2014 NTESS and the ROL contributors.
10 // SPDX-License-Identifier: BSD-3-Clause
11 // *****************************************************************************
12 // @HEADER
13 
14 #include "ROL_Vector.hpp"
15 #include <exception>
16 #include <typeinfo>
17 #include <utility>
18 #include <map>
19 
29 namespace ROL {
30 
31 namespace details {
32 
33 using namespace std;
34 
35 template<typename Real>
36 class VectorClone {
37 private:
38 
39  Ptr<Vector<Real>> vec_;
41 
42 public:
43 
44  VectorClone() : vec_(nullPtr), is_allocated_(false) {}
45 
46  Ptr<Vector<Real>> operator() ( const Vector<Real>& x ) {
47  if( is_allocated_ ) {
48  if( typeid(x) != typeid(*vec_) )
49  throw logic_error("Argument and member vector types are different!");
50  if( x.dimension() != vec_->dimension() )
51  throw logic_error("Argument and member vector types have different dimensions!");
52  }
53  else {
54  vec_ = x.clone();
55  is_allocated_ = true;
56  }
57  return vec_;
58  }
59 
60  Ptr<Vector<Real>> operator() ( const Ptr<const Vector<Real>>& x ) {
61  if( is_allocated_ ) {
62  if( typeid(*x) != typeid(*vec_) )
63  throw logic_error("Argument and member vector types are different!");
64  if( x->dimension() != vec_->dimension() )
65  throw logic_error("Argument and member vector types have different dimensions!");
66  }
67  else {
68  vec_ = x->clone();
69  is_allocated_ = true;
70  }
71  return vec_;
72  }
73 }; // VectorClone
74 
75 
76 
84 template<typename Real, typename KeyType=const char*>
86 private:
87  map<KeyType, VectorClone<Real>> clones_;
88 
89  template<typename First, typename...Rest>
90  void Constructor_Impl( First first, Rest... rest ) {
91  clones_[static_cast<KeyType>(first)] = VectorClone<Real>();
92  Constructor_Impl( rest... );
93  }
94 
95  template<typename First>
96  void Constructor_Impl( First first ) {
97  clones_[static_cast<KeyType>(first)] = VectorClone<Real>();
98  }
99 
100 public:
101 
103  template<typename... Keys>
104  VectorCloneMap( Keys&&...keys ) {
105  Constructor_Impl( forward<Keys>(keys)... );
106  }
107 
108  Ptr<Vector<Real>> operator() ( const Vector<Real>& x, KeyType key ) {
109  return clones_[key](x);
110  }
111 
112  Ptr<Vector<Real>> operator() ( const Ptr<const Vector<Real>>& x, KeyType key ) {
113  return clones_[key](x);
114  }
115 }; // VectorCloneMap
116 
117 
118 
119 
120 
121 } // namespace details
122 
125 
126 } // namespace ROL
127 
128 
129 #endif // ROL_VECTORCLONE_HPP
130 
Container for wrapping a collection of uniquely-named reusable cloned vectors, which in are stored in...
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual int dimension() const
Return dimension of the vector space.
Definition: ROL_Vector.hpp:162
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Ptr< Vector< Real > > vec_
void Constructor_Impl(First first)
map< KeyType, VectorClone< Real > > clones_
void Constructor_Impl(First first, Rest...rest)
VectorCloneMap(Keys &&...keys)
Preallocate keys if desired.