librcsb-core-wrapper 1.005
mapped_vector.C
Go to the documentation of this file.
1//$$FILE$$
2//$$VERSION$$
3//$$DATE$$
4//$$LICENSE$$
5
6
7#ifndef MAPPED_VECTOR_C
8#define MAPPED_VECTOR_C
9
10
11#include <stdexcept>
12#include <vector>
13
14#include <rcsb/mapped_vector.h>
15
16
17using std::out_of_range;
18using std::vector;
19
20
21template <typename T, typename StringCompareT>
23{
24
25 _current.first.clear();
26 _current.second = 0;
27
28}
29
30
31template <typename T, typename StringCompareT>
33 : _index(cmp)
34{
35
36 _current.first.clear();
37 _current.second = 0;
41
42template <typename T, typename StringCompareT>
44 const mapped_vector& inMappedVector)
45{
47 _index = inMappedVector._index;
48 _vector = inMappedVector._vector;
50}
53template <typename T, typename StringCompareT>
57 clear();
58
60
61
62template <typename T, typename StringCompareT>
64{
65
66 _vector.push_back(inT);
67
68 typename tIndex::value_type valuePair(inT, _vector.size() - 1);
69
70 _index.insert(valuePair);
71
72 _current.first = inT;
73 _current.second = _vector.size() - 1;
74
75}
76
77
78template <typename T, typename StringCompareT>
80{
81
82 return(_vector.size());
83
84}
85
86
87template <typename T, typename StringCompareT>
89{
90
91 return(_vector.empty());
92
93}
94
95
96template <typename T, typename StringCompareT>
98 const mapped_vector& inMappedVector)
99{
100
101 _index = inMappedVector._index;
102 _vector = inMappedVector._vector;
103
104}
105
106
107template <typename T, typename StringCompareT>
108void mapped_vector<T, StringCompareT>::operator=(const vector<T>& inVector)
109{
110
111 clear();
112
113 for (unsigned int index = 0; index < inVector.size(); ++index)
114 {
115 push_back(inVector[index]);
116 }
117
118}
119
120
121template <typename T, typename StringCompareT>
123 const mapped_vector& inMappedVector)
124{
125
126 return(_vector == inMappedVector._vector);
127
128}
129
130
131template <typename T, typename StringCompareT>
133 const mapped_vector& inMappedVector)
134{
135
136 return(!operator==(inMappedVector));
137
138}
139
140
141template <typename T, typename StringCompareT>
142const T& mapped_vector<T, StringCompareT>::operator[](unsigned int index) const
143{
144
145 if (index >= size())
146 {
147 throw out_of_range("Invalid index in mapped_vector::operator[]");
148 }
149
150 return(_vector[index]);
151
152}
153
154
155template <typename T, typename StringCompareT>
157{
158
159 return(_vector);
160
161}
162
163
164template <typename T, typename StringCompareT>
166{
167
168 return(_vector);
169
170}
171
172
173template <typename T, typename StringCompareT>
175{
176
177 unsigned int index = get_index(inT);
178 if (index >= size())
179 {
180 throw out_of_range("Element not found in mapped_vector::erase");
181 }
182
183 if (is_equal(_current.first, _vector[index]))
184 {
185 _current.first.clear();
186 _current.second = 0;
187 }
188
189 _vector.erase(_vector.begin() + index);
190
191 _index.erase(inT);
192
193 for (typename tIndex::iterator pos = _index.begin();
194 pos != _index.end(); ++pos)
195 {
196 if (pos->second >= index)
197 {
198 --(pos->second);
199 }
200 }
201
202}
203
204
205template <typename T, typename StringCompareT>
206void mapped_vector<T, StringCompareT>::insert(const unsigned int index,
207 const T& inT)
208{
209
210 unsigned int existingIndex = get_index(inT);
211 if (existingIndex != size())
212 {
213 throw out_of_range("Element exists in mapped_vector::insert");
214 }
215
216 _current.first = inT;
217 _current.second = index;
218
219 _vector.insert(_vector.begin() + index, inT);
220
221 for (typename tIndex::iterator pos = _index.begin(); pos != _index.end();
222 ++pos)
223 {
224 if (pos->second >= index)
225 {
226 ++(pos->second);
227 }
228 }
229
230 typename tIndex::value_type valuePair(inT, index);
231
232 _index.insert(valuePair);
233
234}
235
236
237template <typename T, typename StringCompareT>
239{
240
241 for (unsigned int index = 0; index < _vector.size(); ++index)
242 {
243 typename tIndex::value_type valuePair(_vector[index], index);
244
245 _index.insert(valuePair);
246 }
247
248}
249
250
251template <typename T, typename StringCompareT>
253{
254
255 _index.clear();
256 _vector.clear();
257
258 _current.first.clear();
259 _current.second = 0;
260
261}
262
263
264template <typename T, typename StringCompareT>
265unsigned int mapped_vector<T, StringCompareT>::find(const T& inT) const
266{
267
268 return(get_index(inT));
269
270}
271
272
273template <typename T, typename StringCompareT>
274unsigned int mapped_vector<T, StringCompareT>::get_index(const T& inT) const
275{
276
277 if (is_equal(_current.first, inT))
278 {
279 return(_current.second);
280 }
281
282 // Return index of found value or invalid index
283 typename tIndex::const_iterator pos = _index.find(inT);
284 if (pos != _index.end())
285 {
286 // Found
287 _current.first = inT;
288 _current.second = pos->second;
289
290 return(pos->second);
291 }
292 else
293 {
294 // Not found. Return invalid index.
295 return(_vector.size());
296 }
297
298}
299
300
301template <typename T, typename StringCompareT>
303 const T& secondT) const
304{
305
306 typename tIndex::key_compare keyComp = _index.key_comp();
307
308 return(!(keyComp(firstT, secondT) || keyComp(secondT, firstT)));
309
310}
311
312
313#endif
314
Definition mapped_vector.h:22
const std::vector< T > & get_vector() const
Definition mapped_vector.C:156
void clear()
Definition mapped_vector.C:252
void index_it()
Definition mapped_vector.C:238
const T & operator[](unsigned int index) const
Definition mapped_vector.C:142
bool empty() const
Definition mapped_vector.C:88
unsigned int size() const
Definition mapped_vector.C:79
void erase(const T &inT)
Definition mapped_vector.C:174
void push_back(const T &inT)
Definition mapped_vector.C:63
void operator=(const mapped_vector &inMappedVector)
Definition mapped_vector.C:97
bool operator!=(const mapped_vector &inMappedVector)
Definition mapped_vector.C:132
mapped_vector()
Definition mapped_vector.C:22
~mapped_vector()
Definition mapped_vector.C:54
void insert(const unsigned int index, const T &inT)
Definition mapped_vector.C:206
bool operator==(const mapped_vector &inMappedVector)
Definition mapped_vector.C:122
unsigned int find(const T &inT) const
When not found, returns size()
Definition mapped_vector.C:265