IterationPack: General framework for building iterative algorithms Version of the Day
IterationPack_IterQuantityAccessContiguousDecl.hpp
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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00038 // 
00039 // ***********************************************************************
00040 // @HEADER
00041 //
00042 // Change Log:
00043 //  11/18/99:
00044 //    * last_updated() Added
00045 //    * set_not_updated(k) Added
00046 //    * resize(n) Added
00047 //    * Lazy initialization implemented.
00048 //  12/20/01:
00049 //      * AbstractFactory added to handle memory management
00050 
00051 #ifndef ITER_QUANITY_ACCESS_CONTIGUOUS_DECL_H
00052 #define ITER_QUANITY_ACCESS_CONTIGUOUS_DECL_H
00053 
00054 #include <vector>
00055 #include <limits>
00056 
00057 #include "IterationPack_IterQuantityAccess.hpp"
00058 #include "Teuchos_AbstractFactoryStd.hpp"
00059 
00060 namespace IterationPack {
00061 
00062 // ToDo: Use an implementation subclass for the operations to avoid code blot.
00063 
00085 template<class T_info>
00086 class IterQuantityAccessContiguous : public IterQuantityAccess<T_info> {
00087 public:
00088 
00090   typedef IterQuantityAccess<T_info> base_t;
00091 
00093   typedef Teuchos::RCP<
00094     const Teuchos::AbstractFactory<T_info> >          abstract_factory_ptr_t;
00096   typedef Teuchos::AbstractFactoryStd<T_info,T_info>    abstract_factory_std_t;
00097 
00100 
00114   IterQuantityAccessContiguous(
00115     int                              num_quantities
00116     ,const std::string&              name
00117 #ifdef _MIPS_CXX // MipsPro 7.3.1.1 tries to instantiate the default type even when one is specified?
00118     ,const abstract_factory_ptr_t&   abstract_factory
00119 #else
00120     ,const abstract_factory_ptr_t&   abstract_factory  = Teuchos::rcp(new abstract_factory_std_t())
00121 #endif
00122     );
00123 
00134   void set_factory( const abstract_factory_ptr_t& abstract_factory );
00135 
00142   void resize( int num_quantities );
00143 
00145   ~IterQuantityAccessContiguous();
00146 
00148 
00151 
00153   int num_quantities() const;
00154 
00156 
00159 
00161   IterQuantity* clone() const;
00163   const char* name() const; 
00165   bool has_storage_k(int offset) const;
00167   bool updated_k(int offset) const;
00169   int last_updated() const;
00171   void set_not_updated_k(int offset);
00173   void set_all_not_updated();
00175   bool will_loose_mem(int offset, int set_offset) const;
00177   void next_iteration();
00179   void print_concrete_type( std::ostream& out ) const;
00180 
00182 
00185 
00187   T_info& get_k(int offset);
00189   const T_info& get_k(int offset) const;
00191   T_info& set_k(int offset);
00193   T_info& set_k(int set_offset, int get_offset);
00194 
00196 
00197 private:
00198 
00199   // ///////////////////////////////////////////////
00200   // Private types
00201 
00202   //
00203   typedef std::vector<bool>                                                       updated_t;
00204   //
00205   typedef std::vector<typename abstract_factory_ptr_t::element_type::obj_ptr_t>   store_t;
00206   //
00207   typedef std::vector<T_info*>                                                    quantities_t;
00208 
00209   // ///////////////////////////////////////////////
00210   // Private data members
00211 
00212   // number of contigous iterations memory is reserved for.
00213   int num_quantities_;
00214   // The name of the quantity (useful for debugging)
00215   std::string name_;
00216   // The abstract factory used to create the objects themselves
00217   abstract_factory_ptr_t   abstract_factory_;
00218   // The highest offset for iteration we are providing storage for.  We are providing
00219   // storage for iterations:
00220   //   [ k + max_offset_, k + max_offset_ - 1, ..., k + max_offset_ - num_quanities_ + 1 ].
00221   // For max_offset_ == 1 and num_quantities_  == 3:  [ k+1, k, k-1 ].
00222   int max_offset_;  
00223   // Flags for if the iteration quanity was updated.
00224   //    updated_[max_offset - offset]
00225   //        for offset = max_offset, max_offset - 1, ..., max_offset_ - num_quanities_ + 1
00226   // returns true if and only if the quantity (k + offset) has been updated.
00227   updated_t     updated_;
00228   // Storage vector for the iteration quantities
00229   store_t       store_;
00230   // The vector of pointers to the storage quantities
00231   quantities_t  quantities_;
00232 
00233   // ///////////////////////////////////////////////
00234   // Private member functions
00235 
00236   // Returns true if storage is initialized and false otherwise.
00237   bool is_initialized() const;
00238 
00239   // Called to make sure that we are initialized before a
00240   // nonconst operation is performed.
00241   void lazy_initialization();
00242 
00243   // Called to release current memory
00244   void release_mem();
00245 
00246   // Not defined and not to be called
00247   IterQuantityAccessContiguous();
00248   IterQuantityAccessContiguous(const IterQuantityAccessContiguous&);
00249   IterQuantityAccessContiguous& operator=(const IterQuantityAccessContiguous&);
00250 
00251 };  // end class IterQuantityAccessContiguous
00252 
00253 // /////////////////////////////////////////////////////////////
00254 // Inline members
00255 
00256 template <class T_info>
00257 inline
00258 int IterQuantityAccessContiguous<T_info>::num_quantities() const
00259 { 
00260   return num_quantities_; 
00261 }
00262 
00263 } // end namespace IterationPack
00264 
00265 #endif  // ITER_QUANITY_ACCESS_CONTIGUOUS_DECL_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends