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
29namespace ROL {
30
31namespace details {
32
33template<typename Real>
35private:
36
37 Ptr<Vector<Real>> vec_;
39
40public:
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
82template<typename Real, typename KeyType=const char*>
84private:
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
98public:
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
Defines the linear algebra or vector space interface.
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual int dimension() const
Return dimension of the vector space.
void Constructor_Impl(First first, Rest... rest)
Ptr< Vector< Real > > operator()(const Vector< Real > &x, KeyType key)
std::map< KeyType, VectorClone< Real > > clones_
VectorCloneMap(Keys &&...keys)
Preallocate keys if desired.
Ptr< Vector< Real > > operator()(const Vector< Real > &x)
Ptr< Vector< Real > > vec_