FEI  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
snl_fei_MapContig.hpp
1 #ifndef _snl_fei_MapContig_hpp_
2 #define _snl_fei_MapContig_hpp_
3 
4 /*--------------------------------------------------------------------*/
5 /* Copyright 2005 Sandia Corporation. */
6 /* Under the terms of Contract DE-AC04-94AL85000, there is a */
7 /* non-exclusive license for use of this work by or on behalf */
8 /* of the U.S. Government. Export of this program may require */
9 /* a license from the United States Government. */
10 /*--------------------------------------------------------------------*/
11 
12 #include <fei_macros.hpp>
13 
14 namespace snl_fei {
15 
18 template<typename VAL_TYPE>
19 class MapContig {
20  public:
22  MapContig(int firstKey, int lastKey);
24  MapContig(const MapContig<VAL_TYPE>& src);
26  virtual ~MapContig();
27 
29  typedef int key_type;
30 
32  typedef VAL_TYPE mapped_type;
33 
35  typedef typename std::pair<int,VAL_TYPE> value_type;
36 
38  class iterator {
39  public:
41  iterator() : offset_(-1), mapPtr_(0) {}
42 
44  iterator(int offset,
45  MapContig<VAL_TYPE>* mapPtr)
46  : offset_(offset), mapPtr_(mapPtr)
47  {
48  }
49 
51  virtual ~iterator() {}
52 
55  {
56  if (!mapPtr_) return(*this);
57  int len = mapPtr_->len_;
58  int* keysPtr = mapPtr_->keysPtr_;
59  int first = mapPtr_->first_;
60  if (offset_ < len) {
61  ++offset_;
62  while(offset_ < len) {
63  if (keysPtr[offset_] >= first) break;
64  ++offset_;
65  }
66  }
67  return(*this);
68  }
69 
71  bool operator==(const iterator& rhs)
72  {
73  return( offset_ == rhs.offset_);
74  }
75 
77  bool operator!=(const iterator& rhs)
78  {
79  return( offset_ != rhs.offset_ );
80  }
81 
84  {
85  if (!mapPtr_) return(value_type(0,0));
86 
87  if (offset_ == mapPtr_->len_) return(value_type(0,0));
88 
89  return(value_type(mapPtr_->keysPtr_[offset_],mapPtr_->valuesPtr_[offset_]));
90  }
91 
93  iterator& operator=(const iterator& src)
94  {
95  offset_ = src.offset_;
96  mapPtr_ = src.mapPtr_;
97  return(*this);
98  }
99 
101  int offset_;
102  private:
103  MapContig<VAL_TYPE>* mapPtr_;
104  };//class iterator
105 
107  iterator begin();
109  iterator& end();
110 
112  std::pair<iterator,bool> insert(value_type val);
113 
115  iterator insert(iterator& pos, value_type val);
116 
118  iterator find(int key);
119 
121  iterator lower_bound(int key);
122 
124  int size() const;
125 
126  private:
127  friend class iterator;
128 
129  std::vector<int> keys_;
130  int* keysPtr_;
131  iterator m_end_;
132  std::vector<VAL_TYPE> values_;
133  VAL_TYPE* valuesPtr_;
134  int first_;
135  int len_;
136 }; //class MapContig
137 
138 template<typename VAL_TYPE>
139 MapContig<VAL_TYPE>::MapContig(int firstKey, int lastKey)
140  : keys_(lastKey-firstKey+1),
141  m_end_(),
142  values_(lastKey-firstKey+1),
143  first_(firstKey),
144  len_(lastKey-firstKey+1)
145 {
146  keysPtr_ = keys_.size()>0 ? &keys_[0] : NULL;
147  for(int i=0; i<len_; ++i) {
148  keysPtr_[i] = firstKey+i;
149  }
150  valuesPtr_ = values_.size()>0 ? &values_[0] : NULL;
151  len_ = keys_.size();
152  m_end_ = iterator(len_, this);
153 }
154 
155 template<typename VAL_TYPE>
156 MapContig<VAL_TYPE>::MapContig(const MapContig<VAL_TYPE>& src)
157  : keys_(src.keys_),
158  m_end_(),
159  values_(src.values_),
160  first_(src.first_),
161  len_(src.len_)
162 {
163  keysPtr_ = keys_.size()>0 ? &keys_[0] : NULL;
164  valuesPtr_ = values_.size()>0 ? &values_[0] : NULL;
165  m_end_ = iterator(len_, this);
166 }
167 
168 template<typename VAL_TYPE>
169 MapContig<VAL_TYPE>::~MapContig()
170 {
171 }
172 
173 template<typename VAL_TYPE>
174 inline typename MapContig<VAL_TYPE>::iterator MapContig<VAL_TYPE>::begin()
175 {
176  return( iterator(0, this) );
177 }
178 
179 template<typename VAL_TYPE>
180 inline typename MapContig<VAL_TYPE>::iterator& MapContig<VAL_TYPE>::end()
181 {
182  return( m_end_ );
183 }
184 
185 template<typename VAL_TYPE>
186 inline std::pair<typename MapContig<VAL_TYPE>::iterator,bool>
187  MapContig<VAL_TYPE>::insert(typename MapContig<VAL_TYPE>::value_type val)
188 {
189  int localkey = val.first - first_;
190  if (localkey < 0 || localkey >= len_) {
191  return( std::pair<iterator,bool>(m_end_, false) );
192  }
193 
194  valuesPtr_[localkey] = val.second;
195 
196  return( std::pair<iterator,bool>(iterator(localkey, this),true));
197 }
198 
199 template<typename VAL_TYPE>
200 inline typename MapContig<VAL_TYPE>::iterator
201  MapContig<VAL_TYPE>::insert(typename MapContig<VAL_TYPE>::iterator& pos,
202  typename MapContig<VAL_TYPE>::value_type val)
203 {
204  int offset = pos.offset_;
205  if (offset < 0 || offset >=len_ || pos == m_end_) {
206  offset = val.first - first_;
207  if (offset < 0 || offset >= len_) {
208  return(m_end_);
209  }
210  }
211 
212  valuesPtr_[offset] = val.second;
213 
214  return( iterator(offset, this) );
215 }
216 
217 template<typename VAL_TYPE>
218 inline typename MapContig<VAL_TYPE>::iterator MapContig<VAL_TYPE>::find(int key)
219 {
220  int localkey = key - first_;
221  if (localkey < 0 || localkey >= len_) {
222  return( m_end_ );
223  }
224 
225  return(iterator(localkey, this));
226 }
227 
228 template<typename VAL_TYPE>
229 inline typename MapContig<VAL_TYPE>::iterator MapContig<VAL_TYPE>::lower_bound(int key)
230 {
231  int localkey = key - first_;
232  if (localkey < 0 || localkey >= len_) {
233  return( m_end_ );
234  }
235 
236  return(iterator(localkey, this));
237 }
238 
239 template<typename VAL_TYPE>
240 int MapContig<VAL_TYPE>::size() const
241 {
242  return(len_);
243 }
244 
245 }//namespace snl_fei
246 
247 #endif
248 
std::pair< int, VAL_TYPE > value_type
std::pair< iterator, bool > insert(value_type val)
iterator & operator=(const iterator &src)
iterator find(int key)
bool operator==(const iterator &rhs)
iterator lower_bound(int key)
bool operator!=(const iterator &rhs)
MapContig(int firstKey, int lastKey)