ROL
ROL_SampledVector.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_SAMPLEDVECTOR_H
11 #define ROL_SAMPLEDVECTOR_H
12 
13 namespace ROL {
14 
15 template <class Real, class Key=std::vector<Real>>
17 private:
18  // Storage
19  std::map<Key, int> indices_;
20  std::vector<bool> flags_;
21  std::vector<ROL::Ptr<Vector<Real>>> vectors_;
22  int maxIndex_;
23 
24  // Update flags
25  bool updated_;
26 
27  void reset(const bool flag = true) {
28  if ( flag ) {
29  flags_.assign(flags_.size(),false);
30 // typename std::map<Key, int>::iterator it;
31 // for (it = indices_.begin(); it != indices_.end(); ++it) {
32 // flags_[it->second] = false;
33 // }
34  }
35  }
36 
37 public:
41  : maxIndex_(0), updated_(false) {
42  indices_.clear();
43  flags_.clear();
44  vectors_.clear();
45  }
46 
49  void update(const bool flag = true) {
50  updated_ = flag;
51  reset(flag);
52  }
53 
56  bool get(Vector<Real> &x, const Key &param) {
57  int count = indices_.count(param);
58  bool flag = false;
59  int index = maxIndex_;
60  if (count) {
61  typename std::map<Key, int>::iterator it = indices_.find(param);
62  index = it->second;
63  flag = flags_[index];
64  if (flag) {
65  x.set(*vectors_[index]);
66  }
67  }
68  else {
69  indices_.insert(std::pair<Key, int>(param, index));
70  flags_.push_back(false);
71  vectors_.push_back(x.clone());
72  maxIndex_++;
73  }
74  return flag;
75  }
76 
79  void set(const Vector<Real> &x, const Key &param) {
80  int count = indices_.count(param);
81  int index = maxIndex_;
82  if (count) {
83  typename std::map<Key, int>::iterator it = indices_.find(param);
84  index = it->second;
85  flags_[index] = true;
86  vectors_[index]->set(x);
87  }
88  else {
89  indices_.insert(std::pair<Key, int>(param, index));
90  flags_.push_back(true);
91  vectors_.push_back(x.clone());
92  vectors_[index]->set(x);
93  maxIndex_++;
94  }
95  }
96 }; // class SampledVector
97 
98 } // namespace ROL
99 
100 #endif
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
void reset(const bool flag=true)
SampledVector(void)
Constructor.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
std::vector< ROL::Ptr< Vector< Real > > > vectors_
std::vector< bool > flags_
void update(const bool flag=true)
Update for SampledVector storage.
std::map< Key, int > indices_
void set(const Vector< Real > &x, const Key &param)
Set vector corresponding to input parameter.