OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
vmap.hpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  * This program was prepared by PSI.
7  * All rights in the program are reserved by PSI.
8  * Neither PSI nor the author(s)
9  * makes any warranty, express or implied, or assumes any liability or
10  * responsibility for the use of this software
11  *
12  * Visit www.amas.web.psi for more details
13  *
14  ***************************************************************************/
15 
16 // -*- C++ -*-
17 /***************************************************************************
18  *
19  * The IPPL Framework
20  *
21  *
22  * Visit http://people.web.psi.ch/adelmann/ for more details
23  *
24  ***************************************************************************/
25 
26 // include files
27 #include "Utility/vmap.h"
28 #include <algorithm>
29 
31 //
32 // Copy ctor
33 //
35 template<class Key, class T, class Compare>
37  : Lt(x_ac.Lt) // Copy the comparison object.
38 {
39 
40 
41  V_c = x_ac.V_c; // Copy the vector of data.
42 }
43 
45 //
46 // Assignment
47 //
49 template<class Key, class T, class Compare>
52 {
53 
54 
55  Lt = x_ac.Lt; // Copy the comparison object.
56  V_c = x_ac.V_c; // Copy the vector of data.
57  return *this;
58 }
59 
61 //
62 // Insert operations.
63 //
65 
66 //
67 // Insert a single value.
68 // Return ptr to the inserted and a bool for success.
69 //
70 
71 template<class Key, class T, class Compare>
74 {
75  iterator f_i;
76 
77 
78 
79  bool success;
80  f_i = lower_bound( value.first ); // Look for it.
81  if ( f_i == end() ) // Are we at the end?
82  { // Yes, we can't deref f_i.
83  V_c.push_back(value); // Append. Could realloc
84  f_i = begin() + size() - 1;
85  success = true;
86  }
87  // BFH: Ge -> Le
88  else if ( Le((*f_i).first,value.first)) // Did you find it?
89  {
90  success = false;
91  }
92  else
93  {
94  size_type n = f_i - begin(); // Not there, remember location.
95  V_c.insert( f_i , value ); // Insert. Could cause realloc.
96  f_i = begin() + n;
97  success = true;
98  }
99  return std::pair<iterator,bool>(f_i,success); // Return ptr to inserted.
100 }
101 
102 //
103 // Insert a single value given a hint for where it should go.
104 // It is robust: even if the hint is wrong it will insert in the right place.
105 // If you start with values sorted in increasing order and give it
106 // end() as the hint then it will insert in constant time.
107 // Return a ptr to the inserted position.
108 //
109 
110 template<class Key, class T, class Compare>
113 {
114 
115 
116  iterator low_i = begin(); // The bounds for the search range
117  iterator high_i = end(); // to find where to really put it.
118  if ( hint_i == high_i ) { // Is the hint to append?
119  if ( empty() || Ge( value.first , hint_i[-1].first ) ) { // Is that right? Yes!
120  V_c.push_back(value); // Quick append. Could cause realloc!
121  return end()-1; // return where we put it.
122  }
123  }
124  else { // Hint is not for append.
125  if ( Lt(value.first,(*hint_i).first) ) // Is it to the left?
126  high_i = hint_i; // range = (begin,hint)
127  else // It is to the right.
128  low_i = hint_i; // range = (hint,end)
129  }
130  // Given the range figured out above, find where to really put it.
131  hint_i = std::upper_bound( low_i, high_i, value, value_compare(Lt) );
132  size_type n = hint_i - begin(); // Remember the location.
133  V_c.insert(hint_i,value); // Insert here, could realloc.
134  return V_c.begin() + n; // return iterator.
135 }
136 
137 //
138 // Insert a range of values, with append as the hint.
139 // If the values being inserted are sorted and all greater than or equal
140 // to the elements in the vmap this works out to a fast append.
141 //
142 
143 template<class Key, class T, class Compare>
144 void
145 vmap<Key,T,Compare>::insert(const value_type *first_i,const value_type *last_i)
146 {
147 
148 
149  for (; first_i != last_i; ++first_i)
150  insert( end() , *first_i );
151 }
152 
154 //
155 // erase operations.
156 //
158 
159 //
160 // Erase a single element.
161 //
162 
163 template<class Key, class T, class Compare>
164 void
166 {
167 
168 
169  V_c.erase(p_i);
170 }
171 
172 //
173 // Erase a range of elements.
174 //
175 
176 template<class Key, class T, class Compare>
177 void
179 {
180 
181 
182  V_c.erase(first_i, last_i);
183 }
184 
185 //
186 // Erase all the values with key equal to the given.
187 // Return the number erased.
188 //
189 
190 template<class Key, class T, class Compare>
193 {
194 
195 
196  std::pair<iterator,iterator> range( equal_range(key) );
197  erase(range.first, range.second);
198  return range.second - range.first;
199 }
200 
202 //
203 // Map operations.
204 //
206 
207 //
208 // Find a value if it is there.
209 // Return end() if it is not.
210 //
211 
212 template<class Key, class T, class Compare>
215 {
216 
217 
218  iterator f_i = lower_bound(key); // Look for it.
219  if ( f_i == end() ) // Are we at the end?
220  return end(); // Yes, don't try to deref.
221  // BFH: Ge -> Le
222  if ( Le((*f_i).first , key) ) // Did you find it?
223  return f_i; // yes, return it.
224  else // or...
225  return end(); // no, return end().
226 }
227 
228 //
229 // Return ptr to the first element that is >= the given one.
230 // Can also think of it as the first place it could be put
231 // without violating the sequence.
232 //
233 
234 template<class Key, class T, class Compare>
237 {
238 
239 
240  return std::lower_bound(begin(),end(),value_type(key,T()),value_compare(Lt));
241 }
242 
243 //
244 // Return ptr to the first element > the given one.
245 // Can also think of it as the last place it could be put
246 // without violating the sequence.
247 //
248 
249 template<class Key, class T, class Compare>
252 {
253 
254 
255  return std::upper_bound(begin(),end(),value_type(key,T()),value_compare(Lt));
256 }
257 
258 //
259 // Return a lower_bound, upper_bound pair.
260 //
261 
262 template<class Key, class T, class Compare>
266 {
267 
268  return std::equal_range(begin(),end(),value_type(key,T()),value_compare(Lt));
269 }
270 
271 //
272 // Look up an element given a key.
273 // If there is not one there already, make space for it.
274 //
275 
276 template<class Key, class T, class Compare>
277 T&
279 {
280 
281 
282  size_type n; // We will need to calculate the offset.
283  iterator f_i; // Pointer to the data we find.
284  if ( V_c.empty() ) // Is there anything here?
285  { // No. Can't dereference.
286  n = 0; // The new one will be at the beginning.
287  f_i = begin(); // Point to the beginning.
288  }
289  else // Yes, there is something here.
290  { //
291  f_i = lower_bound(key); // Search the whole range.
292  // BFH: Ge -> Le
293  if ((f_i!=end()) && Le((*f_i).first,key)) // Did we find this one?
294  return (*f_i).second; // Yes, return ref to data.
295  n = f_i - begin(); // Not there, remember location.
296  }
297  V_c.insert( f_i , value_type(key,T()) ); // Insert. Could cause realloc.
298  return V_c[n].second; // Return ref to data.
299 }
300 
302 //
303 // Const map operations.
304 //
306 
307 //
308 // Find how many have the given key.
309 //
310 
311 template<class Key, class T, class Compare>
314 {
315 
316 
317  std::pair<const_iterator,const_iterator>
318  range( equal_range(key) ); // Find the bounds.
319  return range.second - range.first; // Difference to get the count.
320 }
321 
322 //
323 // Find a value if it is there.
324 // Return end() if it is not.
325 //
326 
327 template<class Key, class T, class Compare>
330 {
331 
332 
333  const_iterator f_i = lower_bound(key); // Look for it.
334  // BFH: Ge -> Le
335  if ( Le((*f_i).first , key )) // Did you find it?
336  return f_i; // yes, return it.
337  else // or...
338  return end(); // no, return end().
339 }
340 
341 //
342 // Return ptr to the first element that is >= the given one.
343 // Can also think of it as the first place it could be put
344 // without violating the sequence.
345 //
346 
347 template<class Key, class T, class Compare>
350 {
351 
352 
353  return std::lower_bound(begin(),end(),value_type(key,T()),value_compare(Lt));
354 }
355 
356 //
357 // Return ptr to the first element > the given one.
358 // Can also think of it as the last place it could be put
359 // without violating the sequence.
360 //
361 
362 template<class Key, class T, class Compare>
365 {
366 
367 
368  return std::upper_bound(begin(),end(),value_type(key,T()),value_compare(Lt));
369 }
370 
371 //
372 // Return a lower_bound, upper_bound pair.
373 //
374 
375 template<class Key, class T, class Compare>
376 std::pair<typename vmap<Key,T,Compare>::const_iterator,
379 {
380 
381  return std::equal_range(begin(),end(),value_type(key,T()),value_compare(Lt));
382 }
383 
384 
385 //
386 // Look up an element given a key.
387 // Scream and die if it is not there.
388 //
389 
390 template<class Key, class T, class Compare>
391 const T&
393 {
394 
395 
396  const_iterator f_i = lower_bound(key); // Search the whole range.
397  return (*f_i).second; // Return what you found.
398 }
399 
400 /***************************************************************************
401  * $RCSfile: vmap.cpp,v $ $Author: adelmann $
402  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:34 $
403  * IPPL_VERSION_ID: $Id: vmap.cpp,v 1.1.1.1 2003/01/23 07:40:34 adelmann Exp $
404  ***************************************************************************/
T * value_type(const SliceIterator< T > &)
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
bool Ge(double a, double b)
Definition: Expressions.cpp:86
bool Lt(double a, double b)
Definition: Expressions.cpp:83
bool Le(double a, double b)
Definition: Expressions.cpp:80
std::string::iterator iterator
Definition: MSLang.h:16
Definition: vmap.h:59
vmap< Key, T, Compare > & operator=(const vmap< Key, T, Compare > &x)
Definition: vmap.hpp:51
iterator upper_bound(const key_type &x)
Definition: vmap.hpp:251
rep_type::iterator iterator
Definition: vmap.h:98
void erase(iterator position_i)
Definition: vmap.hpp:165
std::pair< iterator, iterator > equal_range(const key_type &x)
Definition: vmap.hpp:265
Compare Lt
Definition: vmap.h:86
rep_type::size_type size_type
Definition: vmap.h:102
T & operator[](const key_type &k)
Definition: vmap.hpp:278
iterator find(const key_type &x)
Definition: vmap.hpp:214
rep_type V_c
Definition: vmap.h:83
size_type count(const key_type &x) const
Definition: vmap.hpp:313
std::pair< Key, T > value_type
Definition: vmap.h:64
vmap(const Compare &comp=Compare())
Definition: vmap.h:129
std::pair< iterator, bool > insert(const value_type &x)
Definition: vmap.hpp:73
iterator lower_bound(const key_type &x)
Definition: vmap.hpp:236
Key key_type
Definition: vmap.h:63
rep_type::const_iterator const_iterator
Definition: vmap.h:99