IterationPack: General framework for building iterative algorithms  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IterationPack_AlgorithmState.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef ALGORITHM_STATE_H
43 #define ALGORITHM_STATE_H
44 
45 #include <limits>
46 #include <vector>
47 #include <map>
48 #include <string>
49 #include <sstream>
50 #include <iosfwd>
51 
52 #include "IterationPack_IterQuantity.hpp"
53 #include "Teuchos_RCP.hpp"
54 
55 namespace IterationPack {
56 
94 public:
95 
98 
100  typedef size_t iq_id_type;
104  enum { DOES_NOT_EXIST = INT_MAX }; // should not ever be this many insertions.
105 
107  class DoesNotExist : public std::logic_error
108  {public: DoesNotExist(const std::string& what_arg) : std::logic_error(what_arg) {}};
109 
111  class AlreadyExists : public std::logic_error
112  {public: AlreadyExists(const std::string& what_arg) : std::logic_error(what_arg) {}};
113 
115 
117  virtual ~AlgorithmState() {}
118 
121 
126  explicit AlgorithmState(size_t reserve_size = 0);
127 
129 
132 
134  void k(int k);
136  int k() const;
138  int incr_k();
139 
141 
144 
146  virtual size_t num_iter_quant() const;
147 
161  virtual iq_id_type set_iter_quant(const std::string& iq_name, const IQ_ptr& iq);
162 
175  virtual void erase_iter_quant(const std::string& iq_name);
176 
184  virtual iq_id_type get_iter_quant_id(const std::string& iq_name) const;
185 
196  virtual IQ_ptr& get_iter_quant(iq_id_type iq_id);
197 
199  virtual const IQ_ptr& get_iter_quant(iq_id_type iq_id) const;
200 
202 
205 
214  virtual IterQuantity& iter_quant(const std::string& iq_name );
216  virtual const IterQuantity& iter_quant(const std::string& iq_name ) const;
224  virtual IterQuantity& iter_quant(iq_id_type iq_id);
226  virtual const IterQuantity& iter_quant(iq_id_type iq_id) const;
227 
229 
232 
236  virtual void next_iteration(bool incr_k = true);
237 
239 
242 
251  virtual void dump_iter_quant(std::ostream& out) const;
252 
254 
255 private:
256  // ///////////////////////////////////////////////////////////
257  // Private types
258 
259  typedef std::vector<IQ_ptr> iq_t;
260  typedef std::map<std::string,iq_id_type> iq_name_to_id_t;
261 
262  // ///////////////////////////////////////////////////////////
263  // Private data members
264 
265  int k_; // Iteration counter.
266 
267  iq_t iq_;
268  // Array of RCP objects that point to set iteration quantities.
269  // The index into this array is the iq_id for an IQ object. This array
270  // is filled sequantially from the beginning using push_back(...).
271  // When erase_iter_quant(...) is called the iq_[iq_id] is set to null which
272  // reduces the reference count of the IQ object (possible deleing it if
273  // there are no other references). Then if the user tries to access and
274  // IQ object with this abandonded iq_id, the dereferencing operator for
275  // RCP<...> will throw an exception.
276 #ifdef DOXYGEN_COMPILE
277  IterQuantity *iteration_quantities;
278 #endif
279 
280  iq_name_to_id_t iq_name_to_id_;
281  // Mapping of an IQ name to its id.
282 
283  // ///////////////////////////////////////////////////////////
284  // Private member functions
285 
287  iq_name_to_id_t::iterator find_and_assert(const std::string& iq_name);
289  iq_name_to_id_t::const_iterator find_and_assert(const std::string& iq_name) const;
290 
291 }; // end class AlgorithmState
292 
293 // /////////////////////////////////////////////////////////////////////////////////
294 // Inline member definitions for AlgorithmState
295 
296 inline
297 AlgorithmState::AlgorithmState(size_t reserve_size)
298  : k_(0)
299 { iq_.reserve(reserve_size); }
300 
301 // iteration counter
302 
303 inline
304 void AlgorithmState::k(int k)
305 { k_ = k; }
306 
307 inline
308 int AlgorithmState::k() const
309 { return k_; }
310 
311 inline
313 { return ++k_; }
314 
315 //
316 
317 inline
319  return iq_name_to_id_.size();
320 }
321 
322 inline
324  const std::string& iq_name) const
325 {
326  const iq_name_to_id_t::const_iterator itr = iq_name_to_id_.find(iq_name);
327  return itr == iq_name_to_id_.end() ? DOES_NOT_EXIST : (*itr).second;
328 }
329 
330 inline
332  return iq_.at(iq_id);
333 }
334 
335 inline
337  iq_id_type iq_id) const
338 {
339  return iq_.at(iq_id);
340 }
341 
342 inline
343 IterQuantity& AlgorithmState::iter_quant(const std::string& iq_name ) {
344  return *iq_[(*find_and_assert(iq_name)).second];
345 }
346 
347 inline
348 const IterQuantity& AlgorithmState::iter_quant(const std::string& iq_name ) const {
349  return *iq_[(*find_and_assert(iq_name)).second];
350 }
351 
352 } // end namespace IterationPack
353 
354 #endif // ALGORITHM_STATE_H
AlgorithmState(size_t reserve_size=0)
Construct with an initial guess for the number of iteration quantities.
virtual size_t num_iter_quant() const
Return the number of iteration quantities.
virtual void erase_iter_quant(const std::string &iq_name)
Removes the iteration quantity with name iq_name.
virtual void dump_iter_quant(std::ostream &out) const
iteration quantity information dumping.
virtual IQ_ptr & get_iter_quant(iq_id_type iq_id)
Returns the RCP<...> for the iteration quantiy with iq_id.
virtual iq_id_type set_iter_quant(const std::string &iq_name, const IQ_ptr &iq)
Inserts the iteration quantity through a RCP<...> object.
Iterface for information about Iteration Quantities.
virtual IterQuantity & iter_quant(const std::string &iq_name)
Iteration quantity encapsulation object access with via iq_name.
Abstacts a set of iteration quantities for an iterative algorithm.
virtual iq_id_type get_iter_quant_id(const std::string &iq_name) const
Return the iteration quantity id (iq_id) for the iteration quantity.
virtual void next_iteration(bool incr_k=true)
iteration quantity forwarding.