OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
vmap.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef VMAP_H
12 #define VMAP_H
13 
15 /*
16 
17  class vmap
18 
19  A class with the same interface as a map but implemented
20  with a sorted vector.
21 
22  This then has less overhead than a map for access but higher overhead
23  for insert and delete operations. It is appropriate for cases where
24  the contents of the vmap are read much more than they are written.
25 
26  Searching for a tag takes log time because a binary search is used.
27 
28  Insert and delete take linear time when inserting in the middle, but
29  constant time for inserting at the end. The efficient way to fill
30  the map is to:
31  1. Call reserve(size_type) with the number of elements you will be adding.
32  2. Add elements in increasing order with end() as the hint.
33  This will fill the vmap in linear time.
34 
35 */
37 
38 // include files
39 #include <vector>
40 #include <utility>
41 #include <functional>
42 
44 
45 template<class Key>
47 {
48 public:
49 #ifdef IPPL_PURIFY
50  // Add explicit default/copy constructors and op= to avoid UMR's.
51  dummy_less() {}
52  dummy_less(const dummy_less<Key> &) {}
53  dummy_less<Key>& operator=(const dummy_less<Key> &) { return *this; }
54 #endif
55 
56  bool operator()(const Key l, const Key r) const
57  {
58  return l < r;
59  }
60 };
61 
63 
64 template<class Key, class T, class Compare = dummy_less<Key> >
65 class vmap
66 {
67 public:
68  // typedefs:
69 
70  typedef Key key_type;
71  typedef std::pair<Key, T> value_type;
72  typedef Compare key_compare;
73 
74  class value_compare : public
75 
76 std::binary_function<value_type, value_type, bool>
77  {
78  private:
79  Compare comp;
80  public:
81  value_compare(const Compare &c) : comp(c) {}
82 #ifdef IPPL_PURIFY
83  value_compare() {}
84  value_compare(const value_compare &model) : comp(model.comp) {}
86  {
87  comp = rhs.comp;
88  return *this;
89  }
90 #endif
91  bool operator()(const value_type& x, const value_type& y) const
92  {
93  return comp(x.first, y.first);
94  }
95  };
96 
97 private:
98  typedef std::vector< value_type > rep_type;
99 
100  // Here is the actual storage.
102 
103  // The comparator object and some convenient permutations.
104  Compare Lt;
105  bool Gt(const key_type& a, const key_type& b) const { return Lt(b,a); }
106  bool Ge(const key_type& a, const key_type& b) const { return !Lt(a,b); }
107  bool Le(const key_type& a, const key_type& b) const { return !Lt(b,a); }
108 
109 public:
110 
111  // More typedefs.
112 
113  // typedef typename rep_type::pointer pointer;
114  typedef typename rep_type::reference reference;
115  typedef typename rep_type::const_reference const_reference;
116  typedef typename rep_type::iterator iterator;
117  typedef typename rep_type::const_iterator const_iterator;
118  typedef typename rep_type::reverse_iterator reverse_iterator;
119  typedef typename rep_type::const_reverse_iterator const_reverse_iterator;
120  typedef typename rep_type::size_type size_type;
121  typedef typename rep_type::difference_type difference_type;
122 
123  // accessors:
124 
125  iterator begin() { return V_c.begin(); }
126  iterator end() { return V_c.end(); }
127  reverse_iterator rbegin() { return V_c.rbegin(); }
128  reverse_iterator rend() { return V_c.rend(); }
129 
130  const_iterator begin() const { return V_c.begin(); }
131  const_iterator end() const { return V_c.end(); }
132  const_reverse_iterator rbegin() const { return V_c.rbegin(); }
133  const_reverse_iterator rend() const { return V_c.rend(); }
134 
135  key_compare key_comp() const { return Lt; }
136  value_compare value_comp() const { return value_compare(Lt); }
137  bool empty() const { return V_c.empty(); }
138  size_type size() const { return V_c.size(); }
139  size_type max_size() const { return V_c.max_size(); }
140  size_type capacity() const { return V_c.capacity(); }
141 
142  void swap(vmap<Key, T, Compare>& x) { V_c.swap(x.V_c); }
143  void reserve( size_type n ) { V_c.reserve(n); }
144 
145  // allocation/deallocation/assignment
146 
147  vmap(const Compare& comp = Compare()) : Lt(comp) {}
148  vmap(const vmap<Key, T, Compare>& x);
150 
151  // insert functions.
152 
153  std::pair<iterator,bool> insert(const value_type& x);
154  iterator insert(iterator hint_i, const value_type& x);
155  void insert(const value_type* first, const value_type* last);
156 
157  // erase functions.
158 
159  void erase(iterator position_i);
160  void erase(iterator first_i, iterator last_i);
161  size_type erase(const key_type& x);
162 
163  // map operations:
164 
165  T& operator[](const key_type& k);
166  iterator find(const key_type& x);
167  iterator lower_bound(const key_type& x);
168  iterator upper_bound(const key_type& x);
169  std::pair<iterator,iterator> equal_range(const key_type& x);
170 
171  // const map operations.
172 
173  size_type count(const key_type& x) const;
174  const T& operator[](const key_type& k) const;
175  const_iterator find(const key_type& x) const;
176  const_iterator lower_bound(const key_type& x) const;
177  const_iterator upper_bound(const key_type& x) const;
178  std::pair<const_iterator,const_iterator> equal_range(const key_type& x) const;
179 
180 };
181 
182 template <class Key, class T, class Compare>
183 inline bool operator==(const vmap<Key, T, Compare>& x,
184  const vmap<Key, T, Compare>& y) {
185  return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
186 }
187 
188 template <class Key, class T, class Compare>
189 inline bool operator<(const vmap<Key, T, Compare>& x,
190  const vmap<Key, T, Compare>& y) {
191  return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
192 }
193 
194 
196 
197 #include "Utility/vmap.hpp"
198 
199 #endif // VMAP_H
200 
201 /***************************************************************************
202  * $RCSfile: vmap.h,v $ $Author: adelmann $
203  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:34 $
204  * IPPL_VERSION_ID: $Id: vmap.h,v 1.1.1.1 2003/01/23 07:40:34 adelmann Exp $
205  ***************************************************************************/
iterator begin()
Definition: vmap.h:125
rep_type::reference reference
Definition: vmap.h:114
bool operator()(const Key l, const Key r) const
Definition: vmap.h:56
rep_type::const_reverse_iterator const_reverse_iterator
Definition: vmap.h:119
std::pair< iterator, bool > insert(const value_type &x)
Definition: vmap.hpp:73
rep_type::size_type size_type
Definition: vmap.h:120
const_reverse_iterator rbegin() const
Definition: vmap.h:132
bool Ge(const key_type &a, const key_type &b) const
Definition: vmap.h:106
std::pair< iterator, iterator > equal_range(const key_type &x)
Definition: vmap.hpp:265
key_compare key_comp() const
Definition: vmap.h:135
Definition: rbendmap.h:8
const_iterator end() const
Definition: vmap.h:131
reverse_iterator rbegin()
Definition: vmap.h:127
bool operator()(const value_type &x, const value_type &y) const
Definition: vmap.h:91
std::vector< value_type > rep_type
Definition: vmap.h:98
Key key_type
Definition: vmap.h:70
vmap< Key, T, Compare > & operator=(const vmap< Key, T, Compare > &x)
Definition: vmap.hpp:51
Compare Lt
Definition: vmap.h:104
void reserve(size_type n)
Definition: vmap.h:143
rep_type V_c
Definition: vmap.h:101
size_type capacity() const
Definition: vmap.h:140
bool Gt(const key_type &a, const key_type &b) const
Definition: vmap.h:105
reverse_iterator rend()
Definition: vmap.h:128
void swap(vmap< Key, T, Compare > &x)
Definition: vmap.h:142
rep_type::difference_type difference_type
Definition: vmap.h:121
vmap(const Compare &comp=Compare())
Definition: vmap.h:147
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
iterator lower_bound(const key_type &x)
Definition: vmap.hpp:236
bool empty() const
Definition: vmap.h:137
T & operator[](const key_type &k)
Definition: vmap.hpp:278
rep_type::const_reference const_reference
Definition: vmap.h:115
Compare key_compare
Definition: vmap.h:72
void erase(iterator position_i)
Definition: vmap.hpp:165
std::pair< Key, T > value_type
Definition: vmap.h:71
const_reverse_iterator rend() const
Definition: vmap.h:133
iterator upper_bound(const key_type &x)
Definition: vmap.hpp:251
size_type count(const key_type &x) const
Definition: vmap.hpp:313
Compare comp
Definition: vmap.h:79
const_iterator begin() const
Definition: vmap.h:130
size_type size() const
Definition: vmap.h:138
std::string::iterator iterator
Definition: MSLang.h:16
rep_type::iterator iterator
Definition: vmap.h:116
iterator find(const key_type &x)
Definition: vmap.hpp:214
bool Le(const key_type &a, const key_type &b) const
Definition: vmap.h:107
value_compare value_comp() const
Definition: vmap.h:136
value_compare(const Compare &c)
Definition: vmap.h:81
rep_type::const_iterator const_iterator
Definition: vmap.h:117
rep_type::reverse_iterator reverse_iterator
Definition: vmap.h:118
iterator end()
Definition: vmap.h:126
Definition: vmap.h:65
bool operator==(const TwoPolynomial &left, const TwoPolynomial &right)
size_type max_size() const
Definition: vmap.h:139