OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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
45template<class Key>
47{
48public:
49 bool operator()(const Key l, const Key r) const
50 {
51 return l < r;
52 }
53};
54
56
57template<class Key, class T, class Compare = dummy_less<Key> >
58class vmap
59{
60public:
61 // typedefs:
62
63 typedef Key key_type;
64 typedef std::pair<Key, T> value_type;
65 typedef Compare key_compare;
66
68 {
69 private:
70 Compare comp;
71 public:
72 value_compare(const Compare &c) : comp(c) {}
73 bool operator()(const value_type& x, const value_type& y) const
74 {
75 return comp(x.first, y.first);
76 }
77 };
78
79private:
80 typedef std::vector< value_type > rep_type;
81
82 // Here is the actual storage.
84
85 // The comparator object and some convenient permutations.
86 Compare Lt;
87 bool Gt(const key_type& a, const key_type& b) const { return Lt(b,a); }
88 bool Ge(const key_type& a, const key_type& b) const { return !Lt(a,b); }
89 bool Le(const key_type& a, const key_type& b) const { return !Lt(b,a); }
90
91public:
92
93 // More typedefs.
94
95 // typedef typename rep_type::pointer pointer;
96 typedef typename rep_type::reference reference;
97 typedef typename rep_type::const_reference const_reference;
98 typedef typename rep_type::iterator iterator;
99 typedef typename rep_type::const_iterator const_iterator;
100 typedef typename rep_type::reverse_iterator reverse_iterator;
101 typedef typename rep_type::const_reverse_iterator const_reverse_iterator;
102 typedef typename rep_type::size_type size_type;
103 typedef typename rep_type::difference_type difference_type;
104
105 // accessors:
106
107 iterator begin() { return V_c.begin(); }
108 iterator end() { return V_c.end(); }
109 reverse_iterator rbegin() { return V_c.rbegin(); }
110 reverse_iterator rend() { return V_c.rend(); }
111
112 const_iterator begin() const { return V_c.begin(); }
113 const_iterator end() const { return V_c.end(); }
114 const_reverse_iterator rbegin() const { return V_c.rbegin(); }
115 const_reverse_iterator rend() const { return V_c.rend(); }
116
117 key_compare key_comp() const { return Lt; }
119 bool empty() const { return V_c.empty(); }
120 size_type size() const { return V_c.size(); }
121 size_type max_size() const { return V_c.max_size(); }
122 size_type capacity() const { return V_c.capacity(); }
123
124 void swap(vmap<Key, T, Compare>& x) { V_c.swap(x.V_c); }
125 void reserve( size_type n ) { V_c.reserve(n); }
126
127 // allocation/deallocation/assignment
128
129 vmap(const Compare& comp = Compare()) : Lt(comp) {}
130 vmap(const vmap<Key, T, Compare>& x);
132
133 // insert functions.
134
135 std::pair<iterator,bool> insert(const value_type& x);
136 iterator insert(iterator hint_i, const value_type& x);
137 void insert(const value_type* first, const value_type* last);
138
139 // erase functions.
140
141 void erase(iterator position_i);
142 void erase(iterator first_i, iterator last_i);
143 size_type erase(const key_type& x);
144
145 // map operations:
146
147 T& operator[](const key_type& k);
148 iterator find(const key_type& x);
149 iterator lower_bound(const key_type& x);
150 iterator upper_bound(const key_type& x);
151 std::pair<iterator,iterator> equal_range(const key_type& x);
152
153 // const map operations.
154
155 size_type count(const key_type& x) const;
156 const T& operator[](const key_type& k) const;
157 const_iterator find(const key_type& x) const;
158 const_iterator lower_bound(const key_type& x) const;
159 const_iterator upper_bound(const key_type& x) const;
160 std::pair<const_iterator,const_iterator> equal_range(const key_type& x) const;
161
162};
163
164template <class Key, class T, class Compare>
165inline bool operator==(const vmap<Key, T, Compare>& x,
166 const vmap<Key, T, Compare>& y) {
167 return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
168}
169
170template <class Key, class T, class Compare>
171inline bool operator<(const vmap<Key, T, Compare>& x,
172 const vmap<Key, T, Compare>& y) {
173 return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
174}
175
176
178
179#include "Utility/vmap.hpp"
180
181#endif // VMAP_H
182
183/***************************************************************************
184 * $RCSfile: vmap.h,v $ $Author: adelmann $
185 * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:34 $
186 * IPPL_VERSION_ID: $Id: vmap.h,v 1.1.1.1 2003/01/23 07:40:34 adelmann Exp $
187 ***************************************************************************/
std::complex< double > a
bool operator<(const vmap< Key, T, Compare > &x, const vmap< Key, T, Compare > &y)
Definition: vmap.h:171
bool operator==(const vmap< Key, T, Compare > &x, const vmap< Key, T, Compare > &y)
Definition: vmap.h:165
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:45
std::string::iterator iterator
Definition: MSLang.h:16
bool operator()(const Key l, const Key r) const
Definition: vmap.h:49
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
reverse_iterator rbegin()
Definition: vmap.h:109
bool Gt(const key_type &a, const key_type &b) const
Definition: vmap.h:87
size_type max_size() const
Definition: vmap.h:121
size_type capacity() const
Definition: vmap.h:122
std::vector< value_type > rep_type
Definition: vmap.h:80
rep_type::const_reference const_reference
Definition: vmap.h:97
rep_type::iterator iterator
Definition: vmap.h:98
void reserve(size_type n)
Definition: vmap.h:125
void erase(iterator position_i)
Definition: vmap.hpp:165
std::pair< iterator, iterator > equal_range(const key_type &x)
Definition: vmap.hpp:265
size_type size() const
Definition: vmap.h:120
Compare Lt
Definition: vmap.h:86
const_reverse_iterator rbegin() const
Definition: vmap.h:114
rep_type::size_type size_type
Definition: vmap.h:102
const_iterator begin() const
Definition: vmap.h:112
bool Le(const key_type &a, const key_type &b) const
Definition: vmap.h:89
rep_type::const_reverse_iterator const_reverse_iterator
Definition: vmap.h:101
key_compare key_comp() const
Definition: vmap.h:117
void swap(vmap< Key, T, Compare > &x)
Definition: vmap.h:124
rep_type::reference reference
Definition: vmap.h:96
bool empty() const
Definition: vmap.h:119
T & operator[](const key_type &k)
Definition: vmap.hpp:278
iterator end()
Definition: vmap.h:108
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
value_compare value_comp() const
Definition: vmap.h:118
bool Ge(const key_type &a, const key_type &b) const
Definition: vmap.h:88
vmap(const Compare &comp=Compare())
Definition: vmap.h:129
const_iterator end() const
Definition: vmap.h:113
Compare key_compare
Definition: vmap.h:65
const_reverse_iterator rend() const
Definition: vmap.h:115
rep_type::reverse_iterator reverse_iterator
Definition: vmap.h:100
rep_type::difference_type difference_type
Definition: vmap.h:103
std::pair< iterator, bool > insert(const value_type &x)
Definition: vmap.hpp:73
reverse_iterator rend()
Definition: vmap.h:110
iterator begin()
Definition: vmap.h:107
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
Compare comp
Definition: vmap.h:70
value_compare(const Compare &c)
Definition: vmap.h:72
bool operator()(const value_type &x, const value_type &y) const
Definition: vmap.h:73