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 //
8 // Rapid Optimization Library (ROL) Package
9 // Copyright (2014) Sandia Corporation
10 //
11 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
12 // license for use of this work by or on behalf of the U.S. Government.
13 //
14 // Redistribution and use in source and binary forms, with or without
15 // modification, are permitted provided that the following conditions are
16 // met:
17 //
18 // 1. Redistributions of source code must retain the above copyright
19 // notice, this list of conditions and the following disclaimer.
20 //
21 // 2. Redistributions in binary form must reproduce the above copyright
22 // notice, this list of conditions and the following disclaimer in the
23 // documentation and/or other materials provided with the distribution.
24 //
25 // 3. Neither the name of the Corporation nor the names of the
26 // contributors may be used to endorse or promote products derived from
27 // this software without specific prior written permission.
28 //
29 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
30 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
33 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 //
41 // Questions? Contact lead developers:
42 // Drew Kouri (dpkouri@sandia.gov) and
43 // Denis Ridzal (dridzal@sandia.gov)
44 //
45 // ************************************************************************
46 // @HEADER
47 
48 #include "ROL_Vector.hpp"
49 #include <exception>
50 #include <typeinfo>
51 #include <utility>
52 #include <map>
53 
63 namespace ROL {
64 
65 namespace details {
66 
67 using namespace std;
68 
69 template<typename Real>
70 class VectorClone {
71 private:
72 
73  Ptr<Vector<Real>> vec_;
75 
76 public:
77 
78  VectorClone() : vec_(nullPtr), is_allocated_(false) {}
79 
80  Ptr<Vector<Real>> operator() ( const Vector<Real>& x ) {
81  if( is_allocated_ ) {
82  if( typeid(x) != typeid(*vec_) )
83  throw logic_error("Argument and member vector types are different!");
84  if( x.dimension() != vec_->dimension() )
85  throw logic_error("Argument and member vector types have different dimensions!");
86  }
87  else {
88  vec_ = x.clone();
89  is_allocated_ = true;
90  }
91  return vec_;
92  }
93 
94  Ptr<Vector<Real>> operator() ( const Ptr<const Vector<Real>>& x ) {
95  if( is_allocated_ ) {
96  if( typeid(*x) != typeid(*vec_) )
97  throw logic_error("Argument and member vector types are different!");
98  if( x->dimension() != vec_->dimension() )
99  throw logic_error("Argument and member vector types have different dimensions!");
100  }
101  else {
102  vec_ = x->clone();
103  is_allocated_ = true;
104  }
105  return vec_;
106  }
107 }; // VectorClone
108 
109 
110 
118 template<typename Real, typename KeyType=const char*>
120 private:
121  map<KeyType, VectorClone<Real>> clones_;
122 
123  template<typename First, typename...Rest>
124  void Constructor_Impl( First first, Rest... rest ) {
125  clones_[static_cast<KeyType>(first)] = VectorClone<Real>();
126  Constructor_Impl( rest... );
127  }
128 
129  template<typename First>
130  void Constructor_Impl( First first ) {
131  clones_[static_cast<KeyType>(first)] = VectorClone<Real>();
132  }
133 
134 public:
135 
137  template<typename... Keys>
138  VectorCloneMap( Keys&&...keys ) {
139  Constructor_Impl( forward<Keys>(keys)... );
140  }
141 
142  Ptr<Vector<Real>> operator() ( const Vector<Real>& x, KeyType key ) {
143  return clones_[key](x);
144  }
145 
146  Ptr<Vector<Real>> operator() ( const Ptr<const Vector<Real>>& x, KeyType key ) {
147  return clones_[key](x);
148  }
149 }; // VectorCloneMap
150 
151 
152 
153 
154 
155 } // namespace details
156 
159 
160 } // namespace ROL
161 
162 
163 #endif // ROL_VECTORCLONE_HPP
164 
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:196
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
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.