ROL
ROL_SampledScalar.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_SAMPLEDSCALAR_H
11#define ROL_SAMPLEDSCALAR_H
12
13namespace ROL {
14
15template <class Real, class Key=std::vector<Real>>
17private:
18 // Storage
19 std::map<Key, int> indices_;
20 std::vector<bool> flags_;
21 std::vector<Real> scalars_;
23
24 // Update flags
26
27 void reset(const bool flag = true) {
28 if ( flag ) {
29 typename std::map<Key, int>::iterator it;
30 for (it = indices_.begin(); it != indices_.end(); ++it) {
31 flags_[it->second] = false;
32 }
33 }
34 }
35
36public:
40 : maxIndex_(0), updated_(false) {
41 indices_.clear();
42 flags_.clear();
43 scalars_.clear();
44 }
45
48 void update(const bool flag = true) {
49 updated_ = flag;
50 reset(flag);
51 }
52
55 bool get(Real &x, const Key &param) {
56 int count = indices_.count(param);
57 bool flag = false;
58 int index = maxIndex_;
59 if (count) {
60 typename std::map<Key, int>::iterator it = indices_.find(param);
61 index = it->second;
62 flag = flags_[index];
63 if (flag) {
64 x = scalars_[index];
65 }
66 }
67 else {
68 indices_.insert(std::pair<Key, int>(param, index));
69 flags_.push_back(false);
70 scalars_.push_back(static_cast<Real>(0));
71 maxIndex_++;
72 }
73 return flag;
74 }
75
78 void set(const Real &x, const Key &param) {
79 int count = indices_.count(param);
80 int index = maxIndex_;
81 if (count) {
82 typename std::map<Key, int>::iterator it = indices_.find(param);
83 index = it->second;
84 flags_[index] = true;
85 scalars_[index] = x;
86 }
87 else {
88 indices_.insert(std::pair<Key, int>(param, index));
89 flags_.push_back(true);
90 scalars_.push_back(x);
91 maxIndex_++;
92 }
93 }
94}; // class SampledScalar
95
96} // namespace ROL
97
98#endif
std::vector< bool > flags_
void reset(const bool flag=true)
std::vector< Real > scalars_
SampledScalar(void)
Constructor.
std::map< Key, int > indices_
bool get(Real &x, const Key &param)
Return vector corresponding to input parameter.
void set(const Real &x, const Key &param)
Set vector corresponding to input parameter.
void update(const bool flag=true)
Update for SampledScalar storage.