1 // -*- C++ -*-
2 /**
3 * @brief Compact history storage
4 *
5 * Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6 *
7 * This library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21 #include <cstdlib>
22
23 #include "openturns/Compact.hxx"
24 #include "openturns/ResourceMap.hxx"
25 #include "openturns/PersistentObjectFactory.hxx"
26
27 BEGIN_NAMESPACE_OPENTURNS
28
29 /*
30 * @class Compact
31 */
32
33 CLASSNAMEINIT(Compact)
34
35 static const Factory<Compact> Factory_Compact;
36
37 /* Default constructor */
Compact()38 Compact::Compact()
39 : HistoryStrategyImplementation()
40 , halfMaximumSize_(ResourceMap::GetAsUnsignedInteger( "Compact-DefaultHalfMaximumSize" ))
41 , index_(0)
42 , step_(1)
43 , throwingCounter_(0)
44 {
45 // Nothing to do
46 }
47
48 /* Constructor with parameters */
Compact(const UnsignedInteger halfMaximumSize)49 Compact::Compact(const UnsignedInteger halfMaximumSize)
50 : HistoryStrategyImplementation()
51 , halfMaximumSize_(halfMaximumSize)
52 , index_(0)
53 , step_(1)
54 , throwingCounter_(0)
55 {
56 // Nothing to do
57 }
58
59 /* Clear the history storage and change dimension of Point stored */
setDimension(const UnsignedInteger dimension)60 void Compact::setDimension(const UnsignedInteger dimension)
61 {
62 sample_ = Sample(2 * halfMaximumSize_, dimension);
63 index_ = 0;
64 step_ = 1;
65 throwingCounter_ = 0;
66 }
67
68 /* Virtual constructor */
clone() const69 Compact * Compact::clone() const
70 {
71 return new Compact(*this);
72 }
73
74 /* Store the point according to the strategy */
store(const Point & point)75 void Compact::store(const Point & point)
76 {
77 // If we don't throw this point
78 if (throwingCounter_ == 0)
79 {
80 sample_.at(index_) = point;
81 ++index_;
82 // Reinitialize the counter
83 throwingCounter_ = step_;
84 }
85 // Check if one needs compression
86 if (index_ == 2 * halfMaximumSize_)
87 {
88 for (UnsignedInteger i = 0; i < halfMaximumSize_; ++i) sample_[i] = sample_[2 * i + 1];
89 step_ *= 2;
90 throwingCounter_ = step_;
91 index_ = halfMaximumSize_;
92 }
93 --throwingCounter_;
94 }
95
96 /* Sample accessor */
getSample() const97 Sample Compact::getSample() const
98 {
99 // If nothing has been stored
100 Sample outSample(index_, sample_.getDimension());
101 for (UnsignedInteger i = 0; i < index_; ++i) outSample[i] = sample_[i];
102 return outSample;
103 }
104
105 /* HalfMaximumSize accessor */
getHalfMaximumSize() const106 UnsignedInteger Compact::getHalfMaximumSize() const
107 {
108 return halfMaximumSize_;
109 }
110
111 /* Index accessor */
getIndex() const112 UnsignedInteger Compact::getIndex() const
113 {
114 return index_;
115 }
116
117 /* String converter */
__repr__() const118 String Compact::__repr__() const
119 {
120 OSS oss;
121 oss << "class=" << Compact::GetClassName();
122 oss << " sample_=" << sample_;
123 oss << " halfMaximumSize_=" << halfMaximumSize_;
124 oss << " index_=" << index_;
125 oss << " step_=" << step_;
126 oss << " throwingCounter_=" << throwingCounter_;
127 return oss;
128 }
129
130 /* Method save() stores the object through the StorageManager */
save(Advocate & adv) const131 void Compact::save(Advocate & adv) const
132 {
133 HistoryStrategyImplementation::save(adv);
134 adv.saveAttribute("halfMaximumSize_", halfMaximumSize_);
135 adv.saveAttribute("index_", index_);
136 adv.saveAttribute("step_", step_);
137 adv.saveAttribute("throwingCounter_", throwingCounter_);
138 }
139
140 /* Method load() reloads the object from the StorageManager */
load(Advocate & adv)141 void Compact::load(Advocate & adv)
142 {
143 HistoryStrategyImplementation::load(adv);
144 adv.loadAttribute("halfMaximumSize_", halfMaximumSize_);
145 adv.loadAttribute("index_", index_);
146 adv.loadAttribute("step_", step_);
147 adv.loadAttribute("throwingCounter_", throwingCounter_);
148
149 }
150
151
152 END_NAMESPACE_OPENTURNS
153