FEI Version of the Day
snl_fei_MapContig.hpp
00001 /*
00002 // @HEADER
00003 // ************************************************************************
00004 //             FEI: Finite Element Interface to Linear Solvers
00005 //                  Copyright (2005) Sandia Corporation.
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the
00008 // U.S. Government retains certain rights in this software.
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 Alan Williams (william@sandia.gov) 
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 */
00042 
00043 #ifndef _snl_fei_MapContig_hpp_
00044 #define _snl_fei_MapContig_hpp_
00045 
00046 
00047 #include <fei_macros.hpp>
00048 
00049 namespace snl_fei {
00050 
00053 template<typename VAL_TYPE>
00054 class MapContig {
00055  public:
00057   MapContig(int firstKey, int lastKey);
00059   MapContig(const MapContig<VAL_TYPE>& src);
00061   virtual ~MapContig();
00062 
00064   typedef int key_type;
00065 
00067   typedef VAL_TYPE mapped_type;
00068 
00070   typedef typename std::pair<int,VAL_TYPE> value_type;
00071 
00073   class iterator {
00074    public:
00076     iterator() : offset_(-1), mapPtr_(0) {}
00077 
00079     iterator(int offset,
00080        MapContig<VAL_TYPE>* mapPtr)
00081       : offset_(offset), mapPtr_(mapPtr)
00082     {
00083     }
00084 
00086     virtual ~iterator() {}
00087 
00089     iterator& operator++()
00090     {
00091       if (!mapPtr_) return(*this);
00092       int len = mapPtr_->len_;
00093       int* keysPtr = mapPtr_->keysPtr_;
00094       int first = mapPtr_->first_;
00095       if (offset_ < len) {
00096         ++offset_;
00097   while(offset_ < len) {
00098           if (keysPtr[offset_] >= first) break;
00099           ++offset_;
00100         }
00101       }
00102       return(*this);
00103     }
00104 
00106     bool operator==(const iterator& rhs)
00107     {
00108       return( offset_ == rhs.offset_);
00109     }
00110 
00112     bool operator!=(const iterator& rhs)
00113     {
00114       return( offset_ != rhs.offset_ );
00115     }
00116 
00118     value_type operator*()
00119     {
00120       if (!mapPtr_) return(value_type(0,0));
00121 
00122       if (offset_ == mapPtr_->len_) return(value_type(0,0));
00123 
00124       return(value_type(mapPtr_->keysPtr_[offset_],mapPtr_->valuesPtr_[offset_]));
00125     }
00126 
00128     iterator& operator=(const iterator& src)
00129     {
00130       offset_ = src.offset_;
00131       mapPtr_ = src.mapPtr_;
00132       return(*this);
00133     }
00134 
00136     int offset_;
00137    private:
00138     MapContig<VAL_TYPE>* mapPtr_;
00139   };//class iterator
00140 
00142   iterator begin();
00144   iterator& end();
00145 
00147   std::pair<iterator,bool> insert(value_type val);
00148 
00150   iterator insert(iterator& pos, value_type val);
00151 
00153   iterator find(int key);
00154 
00156   iterator lower_bound(int key);
00157 
00159   int size() const;
00160 
00161  private:
00162   friend class iterator;
00163 
00164   std::vector<int> keys_;
00165   int* keysPtr_;
00166   iterator m_end_;
00167   std::vector<VAL_TYPE> values_;
00168   VAL_TYPE* valuesPtr_;
00169   int first_;
00170   int len_;
00171 }; //class MapContig
00172 
00173 template<typename VAL_TYPE>
00174 MapContig<VAL_TYPE>::MapContig(int firstKey, int lastKey)
00175   : keys_(lastKey-firstKey+1),
00176     m_end_(),
00177     values_(lastKey-firstKey+1),
00178     first_(firstKey),
00179     len_(lastKey-firstKey+1)
00180 {
00181   keysPtr_ = &keys_[0];
00182   for(int i=0; i<len_; ++i) {
00183     keysPtr_[i] = firstKey+i;
00184   }
00185   valuesPtr_ = &values_[0];
00186   len_ = keys_.size();
00187   m_end_ = iterator(len_, this);
00188 }
00189 
00190 template<typename VAL_TYPE>
00191 MapContig<VAL_TYPE>::MapContig(const MapContig<VAL_TYPE>& src)
00192  : keys_(src.keys_),
00193   m_end_(),
00194   values_(src.values_),
00195   first_(src.first_),
00196   len_(src.len_)
00197 {
00198   keysPtr_ = &keys_[0];
00199   valuesPtr_ = &values_[0];
00200   m_end_ = iterator(len_, this);
00201 }
00202 
00203 template<typename VAL_TYPE>
00204 MapContig<VAL_TYPE>::~MapContig()
00205 {
00206 }
00207 
00208 template<typename VAL_TYPE>
00209 inline typename MapContig<VAL_TYPE>::iterator MapContig<VAL_TYPE>::begin()
00210 {
00211   return( iterator(0, this) );
00212 }
00213 
00214 template<typename VAL_TYPE>
00215 inline typename MapContig<VAL_TYPE>::iterator& MapContig<VAL_TYPE>::end()
00216 {
00217   return( m_end_ );
00218 }
00219 
00220 template<typename VAL_TYPE>
00221 inline std::pair<typename MapContig<VAL_TYPE>::iterator,bool>
00222  MapContig<VAL_TYPE>::insert(typename MapContig<VAL_TYPE>::value_type val)
00223 {
00224   int localkey = val.first - first_;
00225   if (localkey < 0 || localkey >= len_) {
00226     return( std::pair<iterator,bool>(m_end_, false) );
00227   }
00228 
00229   valuesPtr_[localkey] = val.second;
00230 
00231   return( std::pair<iterator,bool>(iterator(localkey, this),true));
00232 }
00233 
00234 template<typename VAL_TYPE>
00235 inline typename MapContig<VAL_TYPE>::iterator
00236  MapContig<VAL_TYPE>::insert(typename MapContig<VAL_TYPE>::iterator& pos,
00237            typename MapContig<VAL_TYPE>::value_type val)
00238 {
00239   int offset = pos.offset_;
00240   if (offset < 0 || offset >=len_ || pos == m_end_) {
00241     offset = val.first - first_;
00242     if (offset < 0 || offset >= len_) {
00243       return(m_end_);
00244     }
00245   }
00246 
00247   valuesPtr_[offset] = val.second;
00248 
00249   return( iterator(offset, this) );
00250 }
00251 
00252 template<typename VAL_TYPE>
00253 inline typename MapContig<VAL_TYPE>::iterator MapContig<VAL_TYPE>::find(int key)
00254 {
00255   int localkey = key - first_;
00256   if (localkey < 0 || localkey >= len_) {
00257     return( m_end_ );
00258   }
00259 
00260   return(iterator(localkey, this));
00261 }
00262 
00263 template<typename VAL_TYPE>
00264 inline typename MapContig<VAL_TYPE>::iterator MapContig<VAL_TYPE>::lower_bound(int key)
00265 {
00266   int localkey = key - first_;
00267   if (localkey < 0 || localkey >= len_) {
00268     return( m_end_ );
00269   }
00270 
00271   return(iterator(localkey, this));
00272 }
00273 
00274 template<typename VAL_TYPE>
00275 int MapContig<VAL_TYPE>::size() const
00276 {
00277   return(len_);
00278 }
00279 
00280 }//namespace snl_fei
00281 
00282 #endif
00283 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends