ROL
Toggle main menu visibility
Loading...
Searching...
No Matches
src
vector
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
28
29
namespace
ROL
{
30
31
namespace
details
{
32
33
template
<
typename
Real>
34
class
VectorClone
{
35
private
:
36
37
Ptr<Vector<Real>>
vec_
;
38
bool
is_allocated_
;
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
80
81
82
template
<
typename
Real,
typename
KeyType=const
char
*>
83
class
VectorCloneMap
{
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
121
using
details::VectorClone
;
122
using
details::VectorCloneMap
;
123
124
}
// namespace ROL
125
126
127
#endif
// ROL_VECTORCLONE_HPP
128
ROL_Vector.hpp
ROL::Vector
Defines the linear algebra or vector space interface.
Definition
ROL_Vector.hpp:51
ROL::Vector::clone
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
ROL::Vector::dimension
virtual int dimension() const
Return dimension of the vector space.
Definition
ROL_Vector.hpp:163
ROL::details::VectorCloneMap
Definition
ROL_VectorClone.hpp:83
ROL::details::VectorCloneMap::Constructor_Impl
void Constructor_Impl(First first, Rest... rest)
Definition
ROL_VectorClone.hpp:88
ROL::details::VectorCloneMap::operator()
Ptr< Vector< Real > > operator()(const Vector< Real > &x, KeyType key)
Definition
ROL_VectorClone.hpp:106
ROL::details::VectorCloneMap::clones_
std::map< KeyType, VectorClone< Real > > clones_
Definition
ROL_VectorClone.hpp:85
ROL::details::VectorCloneMap::VectorCloneMap
VectorCloneMap(Keys &&...keys)
Preallocate keys if desired.
Definition
ROL_VectorClone.hpp:102
ROL::details::VectorCloneMap::Constructor_Impl
void Constructor_Impl(First first)
Definition
ROL_VectorClone.hpp:94
ROL::details::VectorClone
Definition
ROL_VectorClone.hpp:34
ROL::details::VectorClone::VectorClone
VectorClone()
Definition
ROL_VectorClone.hpp:42
ROL::details::VectorClone::operator()
Ptr< Vector< Real > > operator()(const Vector< Real > &x)
Definition
ROL_VectorClone.hpp:44
ROL::details::VectorClone::is_allocated_
bool is_allocated_
Definition
ROL_VectorClone.hpp:38
ROL::details::VectorClone::vec_
Ptr< Vector< Real > > vec_
Definition
ROL_VectorClone.hpp:37
ROL
Definition
ROL_ElementwiseVector.hpp:27
details
Definition
vector/test_10.cpp:25
Generated by
1.17.0