|
IterationPack: General framework for building iterative algorithms Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization 00005 // Copyright (2003) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // This library is free software; you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as 00012 // published by the Free Software Foundation; either version 2.1 of the 00013 // License, or (at your option) any later version. 00014 // 00015 // This library is distributed in the hope that it will be useful, but 00016 // WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 // Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public 00021 // License along with this library; if not, write to the Free Software 00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 // USA 00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 00025 // 00026 // *********************************************************************** 00027 // @HEADER 00028 00029 #ifndef ALGORITHM_STATE_H 00030 #define ALGORITHM_STATE_H 00031 00032 #include <limits> 00033 #include <vector> 00034 #include <map> 00035 #include <string> 00036 #include <sstream> 00037 #include <iosfwd> 00038 00039 #include "IterationPack_IterQuantity.hpp" 00040 #include "Teuchos_RCP.hpp" 00041 00042 namespace IterationPack { 00043 00080 class AlgorithmState { 00081 public: 00082 00085 00087 typedef size_t iq_id_type; 00089 typedef Teuchos::RCP<IterQuantity> IQ_ptr; 00091 enum { DOES_NOT_EXIST = INT_MAX }; // should not ever be this many insertions. 00092 00094 class DoesNotExist : public std::logic_error 00095 {public: DoesNotExist(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00096 00098 class AlreadyExists : public std::logic_error 00099 {public: AlreadyExists(const std::string& what_arg) : std::logic_error(what_arg) {}}; 00100 00102 00104 virtual ~AlgorithmState() {} 00105 00108 00113 explicit AlgorithmState(size_t reserve_size = 0); 00114 00116 00119 00121 void k(int k); 00123 int k() const; 00125 int incr_k(); 00126 00128 00131 00133 virtual size_t num_iter_quant() const; 00134 00148 virtual iq_id_type set_iter_quant(const std::string& iq_name, const IQ_ptr& iq); 00149 00162 virtual void erase_iter_quant(const std::string& iq_name); 00163 00171 virtual iq_id_type get_iter_quant_id(const std::string& iq_name) const; 00172 00183 virtual IQ_ptr& get_iter_quant(iq_id_type iq_id); 00184 00186 virtual const IQ_ptr& get_iter_quant(iq_id_type iq_id) const; 00187 00189 00192 00201 virtual IterQuantity& iter_quant(const std::string& iq_name ); 00203 virtual const IterQuantity& iter_quant(const std::string& iq_name ) const; 00211 virtual IterQuantity& iter_quant(iq_id_type iq_id); 00213 virtual const IterQuantity& iter_quant(iq_id_type iq_id) const; 00214 00216 00219 00223 virtual void next_iteration(bool incr_k = true); 00224 00226 00229 00238 virtual void dump_iter_quant(std::ostream& out) const; 00239 00241 00242 private: 00243 // /////////////////////////////////////////////////////////// 00244 // Private types 00245 00246 typedef std::vector<IQ_ptr> iq_t; 00247 typedef std::map<std::string,iq_id_type> iq_name_to_id_t; 00248 00249 // /////////////////////////////////////////////////////////// 00250 // Private data members 00251 00252 int k_; // Iteration counter. 00253 00254 iq_t iq_; 00255 // Array of RCP objects that point to set iteration quantities. 00256 // The index into this array is the iq_id for an IQ object. This array 00257 // is filled sequantially from the beginning using push_back(...). 00258 // When erase_iter_quant(...) is called the iq_[iq_id] is set to null which 00259 // reduces the reference count of the IQ object (possible deleing it if 00260 // there are no other references). Then if the user tries to access and 00261 // IQ object with this abandonded iq_id, the dereferencing operator for 00262 // RCP<...> will throw an exception. 00263 #ifdef DOXYGEN_COMPILE 00264 IterQuantity *iteration_quantities; 00265 #endif 00266 00267 iq_name_to_id_t iq_name_to_id_; 00268 // Mapping of an IQ name to its id. 00269 00270 // /////////////////////////////////////////////////////////// 00271 // Private member functions 00272 00274 iq_name_to_id_t::iterator find_and_assert(const std::string& iq_name); 00276 iq_name_to_id_t::const_iterator find_and_assert(const std::string& iq_name) const; 00277 00278 }; // end class AlgorithmState 00279 00280 // ///////////////////////////////////////////////////////////////////////////////// 00281 // Inline member definitions for AlgorithmState 00282 00283 inline 00284 AlgorithmState::AlgorithmState(size_t reserve_size) 00285 : k_(0) 00286 { iq_.reserve(reserve_size); } 00287 00288 // iteration counter 00289 00290 inline 00291 void AlgorithmState::k(int k) 00292 { k_ = k; } 00293 00294 inline 00295 int AlgorithmState::k() const 00296 { return k_; } 00297 00298 inline 00299 int AlgorithmState::incr_k() 00300 { return ++k_; } 00301 00302 // 00303 00304 inline 00305 size_t AlgorithmState::num_iter_quant() const { 00306 return iq_name_to_id_.size(); 00307 } 00308 00309 inline 00310 AlgorithmState::iq_id_type AlgorithmState::get_iter_quant_id( 00311 const std::string& iq_name) const 00312 { 00313 const iq_name_to_id_t::const_iterator itr = iq_name_to_id_.find(iq_name); 00314 return itr == iq_name_to_id_.end() ? DOES_NOT_EXIST : (*itr).second; 00315 } 00316 00317 inline 00318 AlgorithmState::IQ_ptr& AlgorithmState::get_iter_quant(iq_id_type iq_id) { 00319 return iq_.at(iq_id); 00320 } 00321 00322 inline 00323 const AlgorithmState::IQ_ptr& AlgorithmState::get_iter_quant( 00324 iq_id_type iq_id) const 00325 { 00326 return iq_.at(iq_id); 00327 } 00328 00329 inline 00330 IterQuantity& AlgorithmState::iter_quant(const std::string& iq_name ) { 00331 return *iq_[(*find_and_assert(iq_name)).second]; 00332 } 00333 00334 inline 00335 const IterQuantity& AlgorithmState::iter_quant(const std::string& iq_name ) const { 00336 return *iq_[(*find_and_assert(iq_name)).second]; 00337 } 00338 00339 } // end namespace IterationPack 00340 00341 #endif // ALGORITHM_STATE_H
1.7.4