libMesh
Loading...
Searching...
No Matches
vectormap.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19
20#ifndef LIBMESH_VECTORMAP_H
21#define LIBMESH_VECTORMAP_H
22
23// C++ Includes -----------------------------------
24#include <vector>
25#include <algorithm>
26
27// libMesh includes
28#include "libmesh/libmesh_common.h"
29
30
31namespace libMesh
32{
33
60template <typename Key, typename Tp>
61class vectormap : public std::vector<std::pair<Key, Tp>>
62{
63
64public:
65
66 typedef Key key_type;
67 typedef Tp mapped_type;
68 typedef std::pair<Key, Tp> value_type;
69 typedef std::vector<value_type> vector_type;
70 typedef typename vector_type::difference_type difference_type;
71 typedef typename vector_type::iterator iterator;
72 typedef typename vector_type::const_iterator const_iterator;
73
74private:
75
80 {
81 bool operator()(const value_type & lhs,
82 const value_type & rhs) const
83 { return lhs.first < rhs.first; }
84 };
85
90 {
91 bool operator()(const value_type & lhs,
92 const value_type & rhs) const
93 { return lhs.first == rhs.first; }
94 };
95
96public:
97
102 _sorted(false)
103 {}
104
109 std::vector<std::pair<Key, Tp>> (other),
110 _sorted(other._sorted)
111 {}
112
116 void insert (const value_type & x)
117 {
118 _sorted = false;
119 this->push_back(x);
120 }
121
126 template<class... Args>
127 void emplace(Args&&... args)
128 {
129 _sorted = false;
130 this->emplace_back(args...);
131 }
132
136 void sort()
137 {
138 std::sort (this->begin(), this->end(), FirstOrder());
139
140 this->erase (std::unique (this->begin(), this->end(), FirstCompare()), this->end());
141
142 _sorted = true;
143 }
144
148 const Tp & operator[](const key_type & key) const
149 {
150 if (!_sorted)
151 const_cast<vectormap<Key, Tp> *>(this)->sort();
152
154
155 value_type to_find;
156 to_find.first = key;
157
158 const_iterator lower_bound = std::lower_bound (this->begin(), this->end(), to_find, FirstOrder());
159
160 libmesh_error_msg_if(lower_bound == this->end() || lower_bound->first != key,
161 "Error in vectormap::operator[], key not found. "
162 "If you are searching for values you aren't sure exist, try vectormap::find() instead.");
163
164 return lower_bound->second;
165 }
166
171 iterator find (const key_type & key)
172 {
173 if (!_sorted)
174 this->sort();
175
177
178 value_type to_find;
179 to_find.first = key;
180
181 iterator lb = std::lower_bound (this->begin(), this->end(), to_find, FirstOrder());
182
183 // If we didn't find the key, return end()
184 if (lb == this->end() || lb->first != key)
185 return this->end();
186
187 return lb;
188 }
189
194 const_iterator find (const key_type & key) const
195 {
196 // This function isn't really const, we might have to sort the
197 // underlying container before we can search in it.
198 if (!_sorted)
199 const_cast<vectormap<Key, Tp> *>(this)->sort();
200
202
203 value_type to_find;
204 to_find.first = key;
205
206 const_iterator lb = std::lower_bound (this->begin(), this->end(), to_find, FirstOrder());
207
208 // If we didn't find the key, return end()
209 if (lb == this->end() || lb->first != key)
210 return this->end();
211
212 return lb;
213 }
214
221 count (const key_type & key) const
222 {
223 if (!_sorted)
224 const_cast<vectormap<Key, Tp> *>(this)->sort();
225
227
228 value_type to_find;
229 to_find.first = key;
230
231 const_iterator lb = std::lower_bound (this->begin(), this->end(), to_find, FirstOrder());
232
233 // If we didn't find the key, the count is 0.
234 if (lb == this->end() || lb->first != key)
235 return 0;
236
237 return 1;
238 }
239
240
241private:
242
244};
245
246} // namespace libMesh
247
248#endif // LIBMESH_VECTORMAP_H
This vectormap templated class is intended to provide the performance characteristics of a sorted std...
Definition vectormap.h:62
std::pair< Key, Tp > value_type
Definition vectormap.h:68
const Tp & operator[](const key_type &key) const
Definition vectormap.h:148
void insert(const value_type &x)
Inserts x into the vectormap.
Definition vectormap.h:116
difference_type count(const key_type &key) const
Definition vectormap.h:221
vector_type::const_iterator const_iterator
Definition vectormap.h:72
const_iterator find(const key_type &key) const
Definition vectormap.h:194
void emplace(Args &&... args)
Inserts x into the vector map, using "args" to construct it in-place.
Definition vectormap.h:127
void sort()
Sort & unique the vectormap, preparing for use.
Definition vectormap.h:136
std::vector< value_type > vector_type
Definition vectormap.h:69
vectormap()
Default constructor.
Definition vectormap.h:101
iterator find(const key_type &key)
Definition vectormap.h:171
vector_type::iterator iterator
Definition vectormap.h:71
vectormap(const vectormap< Key, Tp > &other)
Copy constructor.
Definition vectormap.h:108
vector_type::difference_type difference_type
Definition vectormap.h:70
The libMesh namespace provides an interface to certain functionality in the library.
libmesh_assert(ctx)
Equality comparison, based solely on first element in a pair.
Definition vectormap.h:90
bool operator()(const value_type &lhs, const value_type &rhs) const
Definition vectormap.h:91
Strict weak ordering, based solely on first element in a pair.
Definition vectormap.h:80
bool operator()(const value_type &lhs, const value_type &rhs) const
Definition vectormap.h:81