compute.h

00001 //
00002 // compute.h
00003 //
00004 // Copyright (C) 1996 Limit Point Systems, Inc.
00005 //
00006 // Author: Curtis Janssen <cljanss@limitpt.com>
00007 // Maintainer: LPS
00008 //
00009 // This file is part of the SC Toolkit.
00010 //
00011 // The SC Toolkit is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Library General Public License as published by
00013 // the Free Software Foundation; either version 2, or (at your option)
00014 // any later version.
00015 //
00016 // The SC Toolkit is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 // GNU Library General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Library General Public License
00022 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
00024 //
00025 // The U.S. Government is granted a limited license as per AL 91-7.
00026 //
00027 
00028 #ifdef __GNUC__
00029 #pragma interface
00030 #endif
00031 
00032 #ifndef _util_misc_compute_h
00033 #define _util_misc_compute_h
00034 
00035 #include <set>
00036 
00037 #include <util/state/state.h>
00038 #include <util/state/stateio.h>
00039 
00040 namespace sc {
00041 
00042 class ResultInfo;
00043 class StateIn;
00044 class StateOut;
00045 
00046 typedef ResultInfo* ResultInfoP;
00047 
00055 class Compute
00056 {
00057    friend class ResultInfo;
00058    friend class AccResultInfo;
00059   private:
00060     std::set<ResultInfoP> _results;
00061 
00062     void add(ResultInfo*);
00063 
00064     // Prohibit copy
00065     Compute(const Compute&) {};
00066 
00067   protected:
00071     virtual void compute() = 0;
00072   public:
00073     Compute();
00074     virtual ~Compute();
00075     virtual bool throw_if_tolerance_exceeded() const;
00076 
00079     virtual void obsolete();
00080 };
00081 
00086 class ResultInfo
00087 {
00088   protected:
00089     int _compute;
00090     int _computed;
00091     Compute* _c;
00092     // This make sure that the datum is up to date.  If it is not then
00093     // Compute::compute() will be called.
00094     virtual void update();
00095   protected:
00096     ResultInfo(StateIn&,Compute*);
00097     ResultInfo(const ResultInfo&,Compute*);
00098     virtual void save_data_state(StateOut&);
00099     virtual void restore_state(StateIn&);
00100     ResultInfo& operator=(const ResultInfo&);
00101   public:
00102     ResultInfo(Compute*c);
00103     virtual ~ResultInfo();
00104     int& compute() { return _compute; }
00105     const int& compute() const { return _compute; }
00106     int compute(int c) { int r = _compute; _compute = c; return r; }
00107     int& computed() { return _computed; }
00108     const int& computed() const { return _computed; }
00109     virtual int needed() const;
00110 };
00111 
00114 class AccResultInfo: public ResultInfo
00115 {
00116   private:
00117     double _actual_accuracy;
00118     double _desired_accuracy;
00119   protected:
00120     AccResultInfo(StateIn&,Compute*);
00121     AccResultInfo(const AccResultInfo&,Compute*);
00122     virtual void save_data_state(StateOut&);
00123     virtual void restore_state(StateIn&);
00124     AccResultInfo& operator=(const AccResultInfo&);
00125     void update();
00126   public:
00127     AccResultInfo(Compute*c);
00128     ~AccResultInfo();
00129     double actual_accuracy() const;
00130     double desired_accuracy() const;
00131     void set_desired_accuracy(double);
00132     void set_actual_accuracy(double);
00133     int computed_to_desired_accuracy() const
00134         { return computed() && _actual_accuracy <= _desired_accuracy; }
00135     int needed() const;
00136 };
00137 
00138 }
00139 
00140 #include <util/misc/comptmpl.h>
00141 
00142 namespace sc {
00143 
00144 typedef NCResult<int> Resultint;
00145 typedef NCResult<double> Resultdouble;
00146 typedef NCAccResult<double> AccResultdouble;
00147 
00148 }
00149 
00150 #endif
00151 
00152 // Local Variables:
00153 // mode: c++
00154 // c-file-style: "CLJ"
00155 // End:

Generated at Wed Sep 5 14:02:29 2007 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.5.2.
These pages are hosted on SourceForge.net