snl_fei_MapContig.hpp

00001 #ifndef _snl_fei_MapContig_hpp_
00002 #define _snl_fei_MapContig_hpp_
00003 
00004 /*--------------------------------------------------------------------*/
00005 /*    Copyright 2005 Sandia Corporation.                              */
00006 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00007 /*    non-exclusive license for use of this work by or on behalf      */
00008 /*    of the U.S. Government.  Export of this program may require     */
00009 /*    a license from the United States Government.                    */
00010 /*--------------------------------------------------------------------*/
00011 
00012 #include <fei_macros.hpp>
00013 
00014 namespace snl_fei {
00015 
00018 template<typename VAL_TYPE>
00019 class MapContig {
00020  public:
00022   MapContig(int firstKey, int lastKey);
00024   MapContig(const MapContig<VAL_TYPE>& src);
00026   virtual ~MapContig();
00027 
00029   typedef int key_type;
00030 
00032   typedef VAL_TYPE mapped_type;
00033 
00035   typedef typename std::pair<int,VAL_TYPE> value_type;
00036 
00038   class iterator {
00039    public:
00041     iterator() : offset_(-1), mapPtr_(0) {}
00042 
00044     iterator(int offset,
00045        MapContig<VAL_TYPE>* mapPtr)
00046       : offset_(offset), mapPtr_(mapPtr)
00047     {
00048     }
00049 
00051     virtual ~iterator() {}
00052 
00054     iterator& operator++()
00055     {
00056       if (!mapPtr_) return(*this);
00057       int len = mapPtr_->len_;
00058       int* keysPtr = mapPtr_->keysPtr_;
00059       int first = mapPtr_->first_;
00060       if (offset_ < len) {
00061         ++offset_;
00062   while(offset_ < len) {
00063           if (keysPtr[offset_] >= first) break;
00064           ++offset_;
00065         }
00066       }
00067       return(*this);
00068     }
00069 
00071     bool operator==(const iterator& rhs)
00072     {
00073       return( offset_ == rhs.offset_);
00074     }
00075 
00077     bool operator!=(const iterator& rhs)
00078     {
00079       return( offset_ != rhs.offset_ );
00080     }
00081 
00083     value_type operator*()
00084     {
00085       if (!mapPtr_) return(value_type(0,0));
00086 
00087       if (offset_ == mapPtr_->len_) return(value_type(0,0));
00088 
00089       return(value_type(mapPtr_->keysPtr_[offset_],mapPtr_->valuesPtr_[offset_]));
00090     }
00091 
00093     iterator& operator=(const iterator& src)
00094     {
00095       offset_ = src.offset_;
00096       mapPtr_ = src.mapPtr_;
00097       return(*this);
00098     }
00099 
00101     int offset_;
00102    private:
00103     MapContig<VAL_TYPE>* mapPtr_;
00104   };//class iterator
00105 
00107   iterator begin();
00109   iterator& end();
00110 
00112   std::pair<iterator,bool> insert(value_type val);
00113 
00115   iterator insert(iterator& pos, value_type val);
00116 
00118   iterator find(int key);
00119 
00121   iterator lower_bound(int key);
00122 
00124   int size() const;
00125 
00126  private:
00127   friend class iterator;
00128 
00129   std::vector<int> keys_;
00130   int* keysPtr_;
00131   iterator m_end_;
00132   std::vector<VAL_TYPE> values_;
00133   VAL_TYPE* valuesPtr_;
00134   int first_;
00135   int len_;
00136 }; //class MapContig
00137 
00138 template<typename VAL_TYPE>
00139 MapContig<VAL_TYPE>::MapContig(int firstKey, int lastKey)
00140   : keys_(lastKey-firstKey+1),
00141     m_end_(),
00142     values_(lastKey-firstKey+1),
00143     first_(firstKey),
00144     len_(lastKey-firstKey+1)
00145 {
00146   keysPtr_ = &keys_[0];
00147   for(int i=0; i<len_; ++i) {
00148     keysPtr_[i] = firstKey+i;
00149   }
00150   valuesPtr_ = &values_[0];
00151   len_ = keys_.size();
00152   m_end_ = iterator(len_, this);
00153 }
00154 
00155 template<typename VAL_TYPE>
00156 MapContig<VAL_TYPE>::MapContig(const MapContig<VAL_TYPE>& src)
00157  : keys_(src.keys_),
00158   m_end_(),
00159   values_(src.values_),
00160   first_(src.first_),
00161   len_(src.len_)
00162 {
00163   keysPtr_ = &keys_[0];
00164   valuesPtr_ = &values_[0];
00165   m_end_ = iterator(len_, this);
00166 }
00167 
00168 template<typename VAL_TYPE>
00169 MapContig<VAL_TYPE>::~MapContig()
00170 {
00171 }
00172 
00173 template<typename VAL_TYPE>
00174 inline typename MapContig<VAL_TYPE>::iterator MapContig<VAL_TYPE>::begin()
00175 {
00176   return( iterator(0, this) );
00177 }
00178 
00179 template<typename VAL_TYPE>
00180 inline typename MapContig<VAL_TYPE>::iterator& MapContig<VAL_TYPE>::end()
00181 {
00182   return( m_end_ );
00183 }
00184 
00185 template<typename VAL_TYPE>
00186 inline std::pair<typename MapContig<VAL_TYPE>::iterator,bool>
00187  MapContig<VAL_TYPE>::insert(typename MapContig<VAL_TYPE>::value_type val)
00188 {
00189   int localkey = val.first - first_;
00190   if (localkey < 0 || localkey >= len_) {
00191     return( std::pair<iterator,bool>(m_end_, false) );
00192   }
00193 
00194   valuesPtr_[localkey] = val.second;
00195 
00196   return( std::pair<iterator,bool>(iterator(localkey, this),true));
00197 }
00198 
00199 template<typename VAL_TYPE>
00200 inline typename MapContig<VAL_TYPE>::iterator
00201  MapContig<VAL_TYPE>::insert(typename MapContig<VAL_TYPE>::iterator& pos,
00202            typename MapContig<VAL_TYPE>::value_type val)
00203 {
00204   int offset = pos.offset_;
00205   if (offset < 0 || offset >=len_ || pos == m_end_) {
00206     offset = val.first - first_;
00207     if (offset < 0 || offset >= len_) {
00208       return(m_end_);
00209     }
00210   }
00211 
00212   valuesPtr_[offset] = val.second;
00213 
00214   return( iterator(offset, this) );
00215 }
00216 
00217 template<typename VAL_TYPE>
00218 inline typename MapContig<VAL_TYPE>::iterator MapContig<VAL_TYPE>::find(int key)
00219 {
00220   int localkey = key - first_;
00221   if (localkey < 0 || localkey >= len_) {
00222     return( m_end_ );
00223   }
00224 
00225   return(iterator(localkey, this));
00226 }
00227 
00228 template<typename VAL_TYPE>
00229 inline typename MapContig<VAL_TYPE>::iterator MapContig<VAL_TYPE>::lower_bound(int key)
00230 {
00231   int localkey = key - first_;
00232   if (localkey < 0 || localkey >= len_) {
00233     return( m_end_ );
00234   }
00235 
00236   return(iterator(localkey, this));
00237 }
00238 
00239 template<typename VAL_TYPE>
00240 int MapContig<VAL_TYPE>::size() const
00241 {
00242   return(len_);
00243 }
00244 
00245 }//namespace snl_fei
00246 
00247 #endif
00248 

Generated on Tue Jul 13 09:27:46 2010 for FEI by  doxygen 1.4.7