Library of Assembled Shared Sources
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-2011 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... Args> std::pair<iterator, bool> emplace(Args&&... args);
152 template<typename... Args> iterator emplace_hint(const_iterator hint, Args&&... args);
153 template<typename... Args> std::pair<iterator, bool> try_emplace(const key_type& key, Args&&... args);
154 template<typename... Args> std::pair<iterator, bool> try_emplace(key_type&& key, Args&&... args);
155 template<typename... Args> iterator try_emplace(const_iterator hint, const key_type& key, Args&&... args);
156 template<typename... Args> iterator try_emplace(const_iterator hint, key_type&& key, Args&&... args);
157
158 void erase(const_iterator position);
159 size_type erase(const key_type& x);
160 void erase(const_iterator first, const_iterator last);
161 void swap(vector_map<Key, T, Compare, Allocator>& other) noexcept;
162 void clear() noexcept;
163
164 key_compare key_comp() const;
165 value_compare value_comp() const;
166
167 iterator find(const key_type& x);
168 const_iterator find(const key_type& x) const;
169 size_type count(const key_type& x) const;
170 bool contains(const Key& key) const;
171
172 iterator lower_bound(const key_type& x);
173 const_iterator lower_bound(const key_type& x) const;
174 iterator upper_bound(const key_type& x);
175 const_iterator upper_bound(const key_type& x) const;
176
177 std::pair<iterator, iterator> equal_range(const key_type& x);
178 std::pair<const_iterator, const_iterator> equal_range(const key_type& x) const;
179
180private:
181
182 bool is_insert_position(const_iterator hint, const key_type& x) const;
183
184 vector_type data_;
185 key_compare key_comp_;
186};
187
188template <typename Key, typename T, typename Compare, typename Allocator>
189bool operator==(const vector_map<Key, T, Compare, Allocator>& a,
190 const vector_map<Key, T, Compare, Allocator>& b);
191
192template <typename Key, typename T, typename Compare, typename Allocator>
193bool operator<(const vector_map<Key, T, Compare, Allocator>& a,
194 const vector_map<Key, T, Compare, Allocator>& b);
195
196template <typename Key, typename T, typename Compare, typename Allocator>
197bool operator!=(const vector_map<Key, T, Compare, Allocator>& a,
198 const vector_map<Key, T, Compare, Allocator>& b);
199
200template <typename Key, typename T, typename Compare, typename Allocator>
201bool operator>(const vector_map<Key, T, Compare, Allocator>& a,
202 const vector_map<Key, T, Compare, Allocator>& b);
203
204template <typename Key, typename T, typename Compare, typename Allocator>
205bool operator>=(const vector_map<Key, T, Compare, Allocator>& a,
206 const vector_map<Key, T, Compare, Allocator>& b);
207
208template <typename Key, typename T, typename Compare, typename Allocator>
209bool operator<=(const vector_map<Key, T, Compare, Allocator>& a,
210 const vector_map<Key, T, Compare, Allocator>& b);
211
212template <typename Key, typename T, typename Compare, typename Allocator, typename Char, typename Traits>
213std::basic_ostream<Char, Traits>&
214operator<<(std::basic_ostream<Char, Traits>& o_stream,
215 const vector_map<Key, T, Compare, Allocator>& container);
216template <typename Key, typename T, typename Compare, typename Allocator, typename Char, typename Traits>
217std::basic_istream<Char, Traits>&
218operator>>(std::basic_istream<Char, Traits>& i_stream,
219 vector_map<Key, T, Compare, Allocator>& container);
220
221}
222}
223
224namespace std
225{
226
227template <typename K, typename T, typename C, typename A>
229
230}
231
232#include "vector_map.inl"
233
234#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_PYMAP_H
235# include "../python/pymap.h"
236#endif
237
238#endif
239
240// 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