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 template<typename Real>
34 class VectorClone {
35 private:
36 
37  Ptr<Vector<Real>> vec_;
39 
40 public:
41 
42  VectorClone() : vec_(nullPtr), is_allocated_(false) {}
43 
44  Ptr<Vector<Real>> operator() ( const Vector<Real>& x ) {
45  if( is_allocated_ ) {
46  if( typeid(x) != typeid(*vec_) )
47  throw std::logic_error("Argument and member vector types are different!");
48  if( x.dimension() != vec_->dimension() )
49  throw std::logic_error("Argument and member vector types have different dimensions!");
50  }
51  else {
52  vec_ = x.clone();
53  is_allocated_ = true;
54  }
55  return vec_;
56  }
57 
58  Ptr<Vector<Real>> operator() ( const Ptr<const Vector<Real>>& x ) {
59  if( is_allocated_ ) {
60  if( typeid(*x) != typeid(*vec_) )
61  throw std::logic_error("Argument and member vector types are different!");
62  if( x->dimension() != vec_->dimension() )
63  throw std::logic_error("Argument and member vector types have different dimensions!");
64  }
65  else {
66  vec_ = x->clone();
67  is_allocated_ = true;
68  }
69  return vec_;
70  }
71 }; // VectorClone
72 
73 
74 
82 template<typename Real, typename KeyType=const char*>
84 private:
85  std::map<KeyType, VectorClone<Real>> clones_;
86 
87  template<typename First, typename...Rest>
88  void Constructor_Impl( First first, Rest... rest ) {
89  clones_[static_cast<KeyType>(first)] = VectorClone<Real>();
90  Constructor_Impl( rest... );
91  }
92 
93  template<typename First>
94  void Constructor_Impl( First first ) {
95  clones_[static_cast<KeyType>(first)] = VectorClone<Real>();
96  }
97 
98 public:
99 
101  template<typename... Keys>
102  VectorCloneMap( Keys&&...keys ) {
103  Constructor_Impl( keys... );
104  }
105 
106  Ptr<Vector<Real>> operator() ( const Vector<Real>& x, KeyType key ) {
107  return clones_[key](x);
108  }
109 
110  Ptr<Vector<Real>> operator() ( const Ptr<const Vector<Real>>& x, KeyType key ) {
111  return clones_[key](x);
112  }
113 }; // VectorCloneMap
114 
115 
116 
117 
118 
119 } // namespace details
120 
123 
124 } // namespace ROL
125 
126 
127 #endif // ROL_VECTORCLONE_HPP
128 
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
Ptr< Vector< Real > > operator()(const Vector< Real > &x)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Ptr< Vector< Real > > vec_
void Constructor_Impl(First first)
std::map< KeyType, VectorClone< Real > > clones_
void Constructor_Impl(First first, Rest...rest)
Ptr< Vector< Real > > operator()(const Vector< Real > &x, KeyType key)
VectorCloneMap(Keys &&...keys)
Preallocate keys if desired.