1 | //
|
---|
2 | // compute.cc
|
---|
3 | //
|
---|
4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
5 | //
|
---|
6 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
7 | // Maintainer: LPS
|
---|
8 | //
|
---|
9 | // This file is part of the SC Toolkit.
|
---|
10 | //
|
---|
11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
12 | // it under the terms of the GNU Library General Public License as published by
|
---|
13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
14 | // any later version.
|
---|
15 | //
|
---|
16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | // GNU Library General Public License for more details.
|
---|
20 | //
|
---|
21 | // You should have received a copy of the GNU Library General Public License
|
---|
22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | //
|
---|
25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
26 | //
|
---|
27 |
|
---|
28 | #ifdef __GNUC__
|
---|
29 | #pragma implementation
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <math.h>
|
---|
33 | #include <float.h>
|
---|
34 | #ifdef HAVE_CONFIG_H
|
---|
35 | #include <scconfig.h>
|
---|
36 | #endif
|
---|
37 | #include <util/class/scexception.h>
|
---|
38 | #include <util/misc/formio.h>
|
---|
39 | #include <util/misc/compute.h>
|
---|
40 | #include <util/state/state.h>
|
---|
41 | #include <util/state/stateio.h>
|
---|
42 |
|
---|
43 | using namespace std;
|
---|
44 | using namespace sc;
|
---|
45 |
|
---|
46 | #ifdef EXPLICIT_TEMPLATE_INSTANTIATION
|
---|
47 | template class Result<int>;
|
---|
48 | template class Result<double>;
|
---|
49 | template class NCAccResult<double>;
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | Compute::Compute()
|
---|
53 | {
|
---|
54 | }
|
---|
55 |
|
---|
56 | Compute::~Compute()
|
---|
57 | {
|
---|
58 | }
|
---|
59 |
|
---|
60 | void
|
---|
61 | Compute::add(ResultInfo*r)
|
---|
62 | {
|
---|
63 | _results.insert(r);
|
---|
64 | }
|
---|
65 |
|
---|
66 | void
|
---|
67 | Compute::obsolete()
|
---|
68 | {
|
---|
69 | // go thru all of the results and mark them as obsolete
|
---|
70 | for (std::set<ResultInfoP>::iterator i = _results.begin();
|
---|
71 | i!=_results.end(); i++) {
|
---|
72 | (*i)->computed() = 0;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | ////////////////////////////////////////////////////////////////////////
|
---|
77 |
|
---|
78 | ResultInfo::ResultInfo(Compute*c):
|
---|
79 | _compute(0),_computed(0),_c(c)
|
---|
80 | {
|
---|
81 | c->add(this);
|
---|
82 | }
|
---|
83 |
|
---|
84 | void
|
---|
85 | ResultInfo::update() {
|
---|
86 | if (!computed()) {
|
---|
87 | int oldcompute = compute(1);
|
---|
88 | _c->compute();
|
---|
89 | compute() = oldcompute;
|
---|
90 | if (!computed()) {
|
---|
91 | ExEnv::errn() << "ResultInfo::update: nothing was computed" << endl;
|
---|
92 | abort();
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | ResultInfo::~ResultInfo()
|
---|
98 | {
|
---|
99 | }
|
---|
100 |
|
---|
101 | ResultInfo::ResultInfo(StateIn&s,Compute*c):
|
---|
102 | _c(c)
|
---|
103 | {
|
---|
104 | s.get(_compute);
|
---|
105 | s.get(_computed);
|
---|
106 |
|
---|
107 | c->add(this);
|
---|
108 | }
|
---|
109 |
|
---|
110 | ResultInfo::ResultInfo(const ResultInfo&r, Compute*c) :
|
---|
111 | _c(c)
|
---|
112 | {
|
---|
113 | _compute=r._compute;
|
---|
114 | _computed=r._computed;
|
---|
115 |
|
---|
116 | c->add(this);
|
---|
117 | }
|
---|
118 |
|
---|
119 | void
|
---|
120 | ResultInfo::restore_state(StateIn&s)
|
---|
121 | {
|
---|
122 | s.get(_compute);
|
---|
123 | s.get(_computed);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void
|
---|
127 | ResultInfo::save_data_state(StateOut&s)
|
---|
128 | {
|
---|
129 | s.put(_compute);
|
---|
130 | s.put(_computed);
|
---|
131 | }
|
---|
132 |
|
---|
133 | ResultInfo&
|
---|
134 | ResultInfo::operator=(const ResultInfo&r)
|
---|
135 | {
|
---|
136 | _compute=r._compute;
|
---|
137 | _computed=r._computed;
|
---|
138 | return *this;
|
---|
139 | }
|
---|
140 |
|
---|
141 | int
|
---|
142 | ResultInfo::needed() const
|
---|
143 | {
|
---|
144 | return _compute && (!_computed);
|
---|
145 | }
|
---|
146 |
|
---|
147 | /////////////////////////////////////////////////////////////////////////
|
---|
148 |
|
---|
149 | AccResultInfo::AccResultInfo(Compute*c):
|
---|
150 | ResultInfo(c),
|
---|
151 | _actual_accuracy(0.0),
|
---|
152 | _desired_accuracy(0.01)
|
---|
153 | {
|
---|
154 | }
|
---|
155 |
|
---|
156 | AccResultInfo::~AccResultInfo()
|
---|
157 | {
|
---|
158 | }
|
---|
159 |
|
---|
160 | double
|
---|
161 | AccResultInfo::actual_accuracy() const
|
---|
162 | {
|
---|
163 | return _actual_accuracy;
|
---|
164 | }
|
---|
165 |
|
---|
166 | double
|
---|
167 | AccResultInfo::desired_accuracy() const
|
---|
168 | {
|
---|
169 | return _desired_accuracy;
|
---|
170 | }
|
---|
171 |
|
---|
172 | void
|
---|
173 | AccResultInfo::set_desired_accuracy(double a)
|
---|
174 | {
|
---|
175 | _desired_accuracy = a;
|
---|
176 | if (_desired_accuracy < _actual_accuracy &&
|
---|
177 | (fabs(_actual_accuracy)-fabs(_desired_accuracy)) > DBL_EPSILON) {
|
---|
178 | computed() = 0;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | void
|
---|
183 | AccResultInfo::set_actual_accuracy(double a)
|
---|
184 | {
|
---|
185 | _actual_accuracy = a;
|
---|
186 | computed() = 1;
|
---|
187 | }
|
---|
188 |
|
---|
189 | AccResultInfo::AccResultInfo(StateIn&s,Compute*c):
|
---|
190 | ResultInfo(s,c)
|
---|
191 | {
|
---|
192 | s.get(_actual_accuracy);
|
---|
193 | s.get(_desired_accuracy);
|
---|
194 | }
|
---|
195 |
|
---|
196 | AccResultInfo::AccResultInfo(const AccResultInfo&a, Compute*c) :
|
---|
197 | ResultInfo(a,c)
|
---|
198 | {
|
---|
199 | _actual_accuracy=a._actual_accuracy;
|
---|
200 | _desired_accuracy=a._desired_accuracy;
|
---|
201 | }
|
---|
202 |
|
---|
203 | void
|
---|
204 | AccResultInfo::restore_state(StateIn&s)
|
---|
205 | {
|
---|
206 | ResultInfo::restore_state(s);
|
---|
207 | s.get(_actual_accuracy);
|
---|
208 | s.get(_desired_accuracy);
|
---|
209 | }
|
---|
210 |
|
---|
211 | void
|
---|
212 | AccResultInfo::save_data_state(StateOut&s)
|
---|
213 | {
|
---|
214 | ResultInfo::save_data_state(s);
|
---|
215 | s.put(_actual_accuracy);
|
---|
216 | s.put(_desired_accuracy);
|
---|
217 | }
|
---|
218 |
|
---|
219 | AccResultInfo&
|
---|
220 | AccResultInfo::operator=(const AccResultInfo&a)
|
---|
221 | {
|
---|
222 | ResultInfo::operator=(a);
|
---|
223 | _actual_accuracy=a._actual_accuracy;
|
---|
224 | _desired_accuracy=a._desired_accuracy;
|
---|
225 | return *this;
|
---|
226 | }
|
---|
227 |
|
---|
228 | int
|
---|
229 | AccResultInfo::needed() const
|
---|
230 | {
|
---|
231 | return compute() && !computed_to_desired_accuracy();
|
---|
232 | }
|
---|
233 |
|
---|
234 | void
|
---|
235 | AccResultInfo::update() {
|
---|
236 | if (!computed_to_desired_accuracy()) {
|
---|
237 | int oldcompute = compute(1);
|
---|
238 | _c->compute();
|
---|
239 | compute() = oldcompute;
|
---|
240 | if (!computed()) {
|
---|
241 | ExEnv::outn() << "AccResultInfo::update: nothing was computed"
|
---|
242 | << endl;
|
---|
243 | abort();
|
---|
244 | }
|
---|
245 | if (_actual_accuracy > _desired_accuracy) {
|
---|
246 | throw ToleranceExceeded("AccResultInfo::update(): "
|
---|
247 | "desired accuracy not achieved",
|
---|
248 | __FILE__, __LINE__,
|
---|
249 | _desired_accuracy, _actual_accuracy);
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | /////////////////////////////////////////////////////////////////////////////
|
---|
255 |
|
---|
256 | // Local Variables:
|
---|
257 | // mode: c++
|
---|
258 | // c-file-style: "CLJ"
|
---|
259 | // End:
|
---|