AbstractLinAlgPack_SpVecIndexLookupClassDecl.hpp

Go to the documentation of this file.
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 SPVEC_INDEX_LOOKUP_CLASS_DECL_H
00030 #define SPVEC_INDEX_LOOKUP_CLASS_DECL_H
00031 
00032 #include <stdexcept>
00033 
00034 #include "AbstractLinAlgPack_Types.hpp"
00035 
00036 namespace AbstractLinAlgPack {
00037 
00038 namespace SparseVectorUtilityPack {
00039 
00040 // ///////////////////////////////////////////////////////////////////////
00050 template <class T_Element>
00051 class SpVecIndexLookup {
00052 public:
00053 
00056 
00058   typedef T_Element             element_type;
00060   typedef typename element_type::index_type index_type;
00062   typedef ptrdiff_t             difference_type;
00064   class NoSpVecSetException : public std::logic_error
00065   {public: NoSpVecSetException(const std::string& what_arg) : std::logic_error(what_arg) {}};
00067   class InvalidInternalStateException : public std::logic_error
00068   {public: InvalidInternalStateException(const std::string& what_arg) : std::logic_error(what_arg) {}};
00070   enum UpperLower { UPPER_ELE, LOWER_ELE };
00072   enum ElementRelation { BEFORE_ELE, AFTER_ELE, EQUAL_TO_ELE };
00074   struct poss_type {
00075     poss_type() : poss(0), rel(EQUAL_TO_ELE) {} 
00076     poss_type(size_type _poss, ElementRelation _rel) : poss(_poss), rel(_rel) {} 
00077     size_type     poss;
00078     ElementRelation   rel;
00079   };
00080 
00082 
00085 
00087   SpVecIndexLookup()
00088     : ele_(0), nz_(0), offset_(0), index_cached_(0)
00089   {}
00090 
00092   SpVecIndexLookup(element_type* ele, size_type nz, difference_type offset)
00093     : ele_(ele), nz_(nz), offset_(offset), index_cached_(0)
00094   {}
00095 
00097 
00100 
00105   void set_sp_vec(element_type* ele, size_type nz, difference_type offset) {
00106     ele_ = ele;   nz_ = nz;   offset_ = offset;
00107     sp_vec_was_modified();
00108   }
00109 
00111   void incr_nz() {
00112     nz_++;
00113   }
00114 
00116 
00119 
00121   element_type*   ele() const {
00122     return ele_;
00123   }
00125   size_type     nz() const {
00126     return nz_;
00127   }
00129   difference_type   offset() const {
00130     return offset_;
00131   }
00132 
00134 
00137 
00166   poss_type find_poss(index_type index, UpperLower uplow) const;
00167 
00185   size_type find_element( index_type index, bool is_sorted ) const;
00186   
00188 
00191 
00195   void sp_vec_was_modified() {
00196     index_cached_ = 0;
00197   }
00198 
00209   void validate_state() const;
00210 
00212 
00213 private:
00214   // ///////////////////////////////////////////////////////////////////////////////
00215   // Private types
00216 
00217   // //////////////////////////////////////////////////////////////////////////////
00218   // Private data members
00219 
00220   element_type*   ele_;   // pointer to array of elements
00221   size_type     nz_;    // number of nonzero elements in ele_
00222   difference_type   offset_;  // offset for each element in ele_.  The actuall index for
00223                   // each element i is ele_[i].index() + offset().
00224   mutable index_type    index_cached_;  // index of last binary search
00225   mutable size_type   poss_cached_; // possition last looked up
00226   mutable ElementRelation ele_rel_cached_;// Specifies where the element looked up is in relation
00227                       // to the element at poss_cached:
00228                       //    before_ele: zero element before poss_cached_
00229                       //    after_ele:  zero element after poss_cached_
00230                       //    equal_to_ele: nonzero element at poss_cached_
00231 
00232   // ////////////////////////
00233   // Private member functions
00234 
00236   void assert_sp_vec_setup() const {
00237     if(!ele_)
00238       throw NoSpVecSetException("The sparse vector (ele, nz, offset) has not been set yet");
00239   }
00240 
00242   size_type adjust_cached_poss(UpperLower uplow) const;
00243 
00261   poss_type binary_ele_search(index_type index, UpperLower uplow) const;
00262 
00263 
00264 };  // end class SpVecIndexLookup
00265 
00266 } // namespace SparseVectorUtilityPack
00267 
00268 } // end namespace AbstractLinAlgPack
00269 
00270 #endif // SPVEC_INDEX_LOOKUP_CLASS_DECL_H

Generated on Thu Sep 18 12:35:13 2008 for MOOCHO (Single Doxygen Collection) by doxygen 1.3.9.1