| 1 | // | 
|---|
| 2 | // compute.h | 
|---|
| 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 interface | 
|---|
| 30 | #endif | 
|---|
| 31 |  | 
|---|
| 32 | #ifndef _util_misc_compute_h | 
|---|
| 33 | #define _util_misc_compute_h | 
|---|
| 34 |  | 
|---|
| 35 | #include <set> | 
|---|
| 36 |  | 
|---|
| 37 | #include <util/state/state.h> | 
|---|
| 38 | #include <util/state/stateio.h> | 
|---|
| 39 |  | 
|---|
| 40 | namespace sc { | 
|---|
| 41 |  | 
|---|
| 42 | class ResultInfo; | 
|---|
| 43 | class StateIn; | 
|---|
| 44 | class StateOut; | 
|---|
| 45 |  | 
|---|
| 46 | typedef ResultInfo* ResultInfoP; | 
|---|
| 47 |  | 
|---|
| 48 | /** The Compute class provides a means of keeping results up | 
|---|
| 49 | to date.  Derived classes can have member data which is | 
|---|
| 50 | registered with the compute class.  When this member data | 
|---|
| 51 | is accessed and it is not available, the compute member | 
|---|
| 52 | function is called.  The compute member must be implemented | 
|---|
| 53 | in derived classes and is responsible for computed the | 
|---|
| 54 | requested result. */ | 
|---|
| 55 | class Compute | 
|---|
| 56 | { | 
|---|
| 57 | friend class ResultInfo; | 
|---|
| 58 | friend class AccResultInfo; | 
|---|
| 59 | private: | 
|---|
| 60 | std::set<ResultInfoP> _results; | 
|---|
| 61 | void add(ResultInfo*); | 
|---|
| 62 |  | 
|---|
| 63 | // Prohibit copy | 
|---|
| 64 | Compute(const Compute&) {}; | 
|---|
| 65 |  | 
|---|
| 66 | protected: | 
|---|
| 67 | /** Recompute at least the results that have compute true | 
|---|
| 68 | and are not already computed.  This should only be called | 
|---|
| 69 | by Result's members. */ | 
|---|
| 70 | virtual void compute() = 0; | 
|---|
| 71 | public: | 
|---|
| 72 | Compute(); | 
|---|
| 73 | virtual ~Compute(); | 
|---|
| 74 |  | 
|---|
| 75 | /** Marks all results as being out of date.  Any subsequent access | 
|---|
| 76 | to results will cause Compute::compute() to be called. */ | 
|---|
| 77 | virtual void obsolete(); | 
|---|
| 78 | }; | 
|---|
| 79 |  | 
|---|
| 80 | /** This is a base class for all of Compute's result types.  Usually | 
|---|
| 81 | Result<Type> will be used to create a result that has a particular datum | 
|---|
| 82 | associated with it, however a ResultInfo can also be declared to keep | 
|---|
| 83 | track of datum's for which it is awkward to use Result_dec. */ | 
|---|
| 84 | class ResultInfo | 
|---|
| 85 | { | 
|---|
| 86 | protected: | 
|---|
| 87 | int _compute; | 
|---|
| 88 | int _computed; | 
|---|
| 89 | Compute* _c; | 
|---|
| 90 | // This make sure that the datum is up to date.  If it is not then | 
|---|
| 91 | // Compute::compute() will be called. | 
|---|
| 92 | virtual void update(); | 
|---|
| 93 | protected: | 
|---|
| 94 | ResultInfo(StateIn&,Compute*); | 
|---|
| 95 | ResultInfo(const ResultInfo&,Compute*); | 
|---|
| 96 | virtual void save_data_state(StateOut&); | 
|---|
| 97 | virtual void restore_state(StateIn&); | 
|---|
| 98 | ResultInfo& operator=(const ResultInfo&); | 
|---|
| 99 | public: | 
|---|
| 100 | ResultInfo(Compute*c); | 
|---|
| 101 | virtual ~ResultInfo(); | 
|---|
| 102 | int& compute() { return _compute; } | 
|---|
| 103 | const int& compute() const { return _compute; } | 
|---|
| 104 | int compute(int c) { int r = _compute; _compute = c; return r; } | 
|---|
| 105 | int& computed() { return _computed; } | 
|---|
| 106 | const int& computed() const { return _computed; } | 
|---|
| 107 | virtual int needed() const; | 
|---|
| 108 | }; | 
|---|
| 109 |  | 
|---|
| 110 | /** This is like ResultInfo but the accuracy with which a result was | 
|---|
| 111 | computed as well as the desired accuracy are stored. */ | 
|---|
| 112 | class AccResultInfo: public ResultInfo | 
|---|
| 113 | { | 
|---|
| 114 | private: | 
|---|
| 115 | double _actual_accuracy; | 
|---|
| 116 | double _desired_accuracy; | 
|---|
| 117 | protected: | 
|---|
| 118 | AccResultInfo(StateIn&,Compute*); | 
|---|
| 119 | AccResultInfo(const AccResultInfo&,Compute*); | 
|---|
| 120 | virtual void save_data_state(StateOut&); | 
|---|
| 121 | virtual void restore_state(StateIn&); | 
|---|
| 122 | AccResultInfo& operator=(const AccResultInfo&); | 
|---|
| 123 | void update(); | 
|---|
| 124 | public: | 
|---|
| 125 | AccResultInfo(Compute*c); | 
|---|
| 126 | ~AccResultInfo(); | 
|---|
| 127 | double actual_accuracy() const; | 
|---|
| 128 | double desired_accuracy() const; | 
|---|
| 129 | void set_desired_accuracy(double); | 
|---|
| 130 | void set_actual_accuracy(double); | 
|---|
| 131 | int computed_to_desired_accuracy() const | 
|---|
| 132 | { return computed() && _actual_accuracy <= _desired_accuracy; } | 
|---|
| 133 | int needed() const; | 
|---|
| 134 | }; | 
|---|
| 135 |  | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | #include <util/misc/comptmpl.h> | 
|---|
| 139 |  | 
|---|
| 140 | namespace sc { | 
|---|
| 141 |  | 
|---|
| 142 | typedef NCResult<int> Resultint; | 
|---|
| 143 | typedef NCResult<double> Resultdouble; | 
|---|
| 144 | typedef NCAccResult<double> AccResultdouble; | 
|---|
| 145 |  | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | #endif | 
|---|
| 149 |  | 
|---|
| 150 | // Local Variables: | 
|---|
| 151 | // mode: c++ | 
|---|
| 152 | // c-file-style: "CLJ" | 
|---|
| 153 | // End: | 
|---|