Library of Assembled Shared Sources
 
Loading...
Searching...
No Matches
vector_map.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2026 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43/** @class lass::stde::vector_map
44 * @brief a map-like container built on a sorted vector (write-rarely, read-many)
45 * @author Bram de Greve [Bramz]
46 */
47
48#ifndef LASS_GUARDIAN_OF_INCLUSION_STDE_VECTOR_MAP_H
49#define LASS_GUARDIAN_OF_INCLUSION_STDE_VECTOR_MAP_H
50
51#include "stde_common.h"
52
53namespace lass
54{
55namespace stde
56{
57
58template
59<
60 typename Key,
61 typename T,
62 typename Compare = std::less<Key>,
63 typename Allocator = std::allocator< std::pair<Key, T> >
64>
65class vector_map
66{
67public:
68
69 typedef Key key_type;
70 typedef T mapped_type;
71
72 /** @warning this actually _should_ be a std::pair<const Key, T> as required by associative containers.
73 * But if we do, the underlying std::vector that stores the elements will bork on it as it also
74 * requires value_type to be copy assignable. Until we have a solution that works for both,
75 * we'll open the security hole by have a mutable key. It of course goes without saying that you
76 * should never ever mutate the key of an element stored in a vector_map!
77 */
78 typedef std::pair<Key, T> value_type;
79
80 typedef Compare key_compare;
81 class value_compare
82 {
83 public:
84 typedef value_type first_argument_type;
85 typedef value_type second_argument_type;
86 typedef bool result_type;
87 bool operator()(const value_type& a, const value_type& b) const
88 {
89 return key_comp_(a.first, b.first);
90 }
91 private:
92 friend class vector_map;
93 value_compare(const key_compare& key_comp): key_comp_(key_comp) {}
94 key_compare key_comp_;
95 };
96
97 typedef value_type& reference;
98 typedef const value_type& const_reference;
99 typedef typename std::allocator_traits<Allocator>::template rebind_alloc<value_type> allocator_type;
100 typedef typename std::allocator_traits<Allocator>::pointer pointer;
101 typedef typename std::allocator_traits<Allocator>::const_pointer const_pointer;
102 typedef typename std::allocator_traits<Allocator>::size_type size_type;
103 typedef typename std::allocator_traits<Allocator>::difference_type difference_type;
104
105 typedef std::vector<value_type, Allocator> vector_type;
106 typedef typename vector_type::iterator iterator;
107 typedef typename vector_type::const_iterator const_iterator;
108 typedef typename vector_type::reverse_iterator reverse_iterator;
109 typedef typename vector_type::const_reverse_iterator const_reverse_iterator;
110
111 explicit vector_map(const key_compare& key_comp = key_compare(), const allocator_type& allocator = Allocator());
112 template <typename InputIterator>
113 vector_map(InputIterator first, InputIterator last,
114 const key_compare& key_comp = key_compare(), const allocator_type& allocator = Allocator());
117 vector_map(std::initializer_list<value_type> init,
118 const key_compare& key_comp = key_compare(), const allocator_type& allocator = Allocator());
119 ~vector_map();
120
123
124 iterator begin() noexcept;
125 const_iterator begin() const noexcept;
126 const_iterator cbegin() const noexcept;
127 iterator end() noexcept;
128 const_iterator end() const noexcept;
129 const_iterator cend() const noexcept;
130 reverse_iterator rbegin() noexcept;
131 const_reverse_iterator rbegin() const noexcept;
132 const_reverse_iterator crbegin() const noexcept;
133 reverse_iterator rend() noexcept;
134 const_reverse_iterator rend() const noexcept;
135 const_reverse_iterator crend() const noexcept;
136
137 bool empty() const noexcept;
138 size_type size() const noexcept;
139 size_type max_size() const noexcept;
140
141 mapped_type& at(const key_type& key);
142 const mapped_type& at(const key_type& key) const;
143 mapped_type& operator[](const key_type& x);
144 mapped_type& operator[](key_type&& x);
145
146 std::pair<iterator, bool> insert(const value_type& x);
147 std::pair<iterator, bool> insert(value_type&& value);
148 iterator insert(const_iterator hint, const value_type& x);
149 template <typename InputIterator> void insert(InputIterator first, InputIterator last);
150
151 template<typename M> std::pair<iterator, bool> insert_or_assign(const key_type& key, M&& obj);
152 template<typename M> std::pair<iterator, bool> insert_or_assign(key_type&& key, M&& obj);
153 template<typename M> iterator insert_or_assign(const_iterator hint, const key_type& key, M&& obj);
154 template<typename M> iterator insert_or_assign(const_iterator hint, key_type&& key, M&& obj);
155
156 template<typename... Args> std::pair<iterator, bool> emplace(Args&&... args);
157 template<typename... Args> iterator emplace_hint(const_iterator hint, Args&&... args);
158 template<typename... Args> std::pair<iterator, bool> try_emplace(const key_type& key, Args&&... args);
159 template<typename... Args> std::pair<iterator, bool> try_emplace(key_type&& key, Args&&... args);
160 template<typename... Args> iterator try_emplace(const_iterator hint, const key_type& key, Args&&... args);
161 template<typename... Args> iterator try_emplace(const_iterator hint, key_type&& key, Args&&... args);
162
163 void erase(const_iterator position);
164 size_type erase(const key_type& x);
165 void erase(const_iterator first, const_iterator last);
166 void swap(vector_map<Key, T, Compare, Allocator>& other) noexcept;
167 void clear() noexcept;
168
169 key_compare key_comp() const;
170 value_compare value_comp() const;
171
172 iterator find(const key_type& x);
173 const_iterator find(const key_type& x) const;
174 size_type count(const key_type& x) const;
175 bool contains(const Key& key) const;
176
177 iterator lower_bound(const key_type& x);
178 const_iterator lower_bound(const key_type& x) const;
179 iterator upper_bound(const key_type& x);
180 const_iterator upper_bound(const key_type& x) const;
181
182 std::pair<iterator, iterator> equal_range(const key_type& x);
183 std::pair<const_iterator, const_iterator> equal_range(const key_type& x) const;
184
185private:
186
187 bool is_insert_position(const_iterator hint, const key_type& x) const;
188
189 vector_type data_;
190 key_compare key_comp_;
191};
192
193template <typename Key, typename T, typename Compare, typename Allocator>
194bool operator==(const vector_map<Key, T, Compare, Allocator>& a,
195 const vector_map<Key, T, Compare, Allocator>& b);
196
197template <typename Key, typename T, typename Compare, typename Allocator>
198bool operator<(const vector_map<Key, T, Compare, Allocator>& a,
199 const vector_map<Key, T, Compare, Allocator>& b);
200
201template <typename Key, typename T, typename Compare, typename Allocator>
202bool operator!=(const vector_map<Key, T, Compare, Allocator>& a,
203 const vector_map<Key, T, Compare, Allocator>& b);
204
205template <typename Key, typename T, typename Compare, typename Allocator>
206bool operator>(const vector_map<Key, T, Compare, Allocator>& a,
207 const vector_map<Key, T, Compare, Allocator>& b);
208
209template <typename Key, typename T, typename Compare, typename Allocator>
210bool operator>=(const vector_map<Key, T, Compare, Allocator>& a,
211 const vector_map<Key, T, Compare, Allocator>& b);
212
213template <typename Key, typename T, typename Compare, typename Allocator>
214bool operator<=(const vector_map<Key, T, Compare, Allocator>& a,
215 const vector_map<Key, T, Compare, Allocator>& b);
216
217template <typename Key, typename T, typename Compare, typename Allocator, typename Char, typename Traits>
218std::basic_ostream<Char, Traits>&
219operator<<(std::basic_ostream<Char, Traits>& o_stream,
220 const vector_map<Key, T, Compare, Allocator>& container);
221template <typename Key, typename T, typename Compare, typename Allocator, typename Char, typename Traits>
222std::basic_istream<Char, Traits>&
223operator>>(std::basic_istream<Char, Traits>& i_stream,
224 vector_map<Key, T, Compare, Allocator>& container);
225
226}
227}
228
229namespace std
230{
231
232template <typename K, typename T, typename C, typename A>
234
235}
236
237#include "vector_map.inl"
238
239#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_PYMAP_H
240# include "../python/pymap.h"
241#endif
242
243#endif
244
245// EOF
a map-like container built on a sorted vector (write-rarely, read-many)
Definition vector_map.h:66
std::pair< Key, T > value_type
Definition vector_map.h:78
lass extensions to the standard library
Library for Assembled Shared Sources.
Definition config.h:53