FEI Version of the Day
Loading...
Searching...
No Matches
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
14namespace snl_fei {
15
18template<typename VAL_TYPE>
19class MapContig {
20 public:
22 MapContig(int firstKey, int lastKey);
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
94 {
95 offset_ = src.offset_;
96 mapPtr_ = src.mapPtr_;
97 return(*this);
98 }
99
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
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
138template<typename VAL_TYPE>
139MapContig<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
155template<typename VAL_TYPE>
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
168template<typename VAL_TYPE>
170{
171}
172
173template<typename VAL_TYPE>
175{
176 return( iterator(0, this) );
177}
178
179template<typename VAL_TYPE>
181{
182 return( m_end_ );
183}
184
185template<typename VAL_TYPE>
186inline std::pair<typename MapContig<VAL_TYPE>::iterator,bool>
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
199template<typename VAL_TYPE>
200inline typename MapContig<VAL_TYPE>::iterator
201 MapContig<VAL_TYPE>::insert(typename MapContig<VAL_TYPE>::iterator& pos,
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
217template<typename VAL_TYPE>
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
228template<typename VAL_TYPE>
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
239template<typename VAL_TYPE>
241{
242 return(len_);
243}
244
245}//namespace snl_fei
246
247#endif
248
bool operator!=(const iterator &rhs)
iterator(int offset, MapContig< VAL_TYPE > *mapPtr)
iterator & operator=(const iterator &src)
bool operator==(const iterator &rhs)
MapContig(int firstKey, int lastKey)
std::pair< iterator, bool > insert(value_type val)
iterator find(int key)
iterator lower_bound(int key)
std::pair< int, VAL_TYPE > value_type
iterator insert(iterator &pos, value_type val)
MapContig(const MapContig< VAL_TYPE > &src)