Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: base/containers/flat_map.h

Issue 2776793002: Make flat containers stable, allow constructing from vector. (Closed)
Patch Set: Put back media log change lost in merge Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/containers/flat_map_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_CONTAINERS_FLAT_MAP_H_ 5 #ifndef BASE_CONTAINERS_FLAT_MAP_H_
6 #define BASE_CONTAINERS_FLAT_MAP_H_ 6 #define BASE_CONTAINERS_FLAT_MAP_H_
7 7
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/containers/flat_tree.h" 10 #include "base/containers/flat_tree.h"
(...skipping 20 matching lines...) Expand all
31 // sorted containers that stores its elements in contiguous memory (a vector). 31 // sorted containers that stores its elements in contiguous memory (a vector).
32 // 32 //
33 // Additional documentation and usage advice is in flat_set.h. 33 // Additional documentation and usage advice is in flat_set.h.
34 // 34 //
35 // DOCUMENTATION 35 // DOCUMENTATION
36 // 36 //
37 // Most of the core functionality is inherited from flat_tree. Please see 37 // Most of the core functionality is inherited from flat_tree. Please see
38 // flat_tree.h for more details for most of these functions. As a quick 38 // flat_tree.h for more details for most of these functions. As a quick
39 // reference, the functions available are: 39 // reference, the functions available are:
40 // 40 //
41 // Constructors (inputs need not be sorted):
42 // flat_map(InputIterator first, InputIterator last,
43 // FlatContainerDupes, const Compare& compare = Compare());
44 // flat_map(const flat_map&);
45 // flat_map(flat_map&&);
46 // flat_map(std::vector<value_type>, FlatContainerDupes); // Re-use storage.
47 // flat_map(std::initializer_list<value_type> ilist,
48 // const Compare& comp = Compare());
49 //
41 // Assignment functions: 50 // Assignment functions:
42 // flat_map& operator=(const flat_map&); 51 // flat_map& operator=(const flat_map&);
43 // flat_map& operator=(flat_map&&); 52 // flat_map& operator=(flat_map&&);
44 // flat_map& operator=(initializer_list<pair<Key, Mapped>>); 53 // flat_map& operator=(initializer_list<pair<Key, Mapped>>);
45 // 54 //
46 // Memory management functions: 55 // Memory management functions:
47 // void reserve(size_t); 56 // void reserve(size_t);
48 // size_t capacity() const; 57 // size_t capacity() const;
49 // void shrink_to_fit(); 58 // void shrink_to_fit();
50 // 59 //
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 // Search functions: 97 // Search functions:
89 // size_t count(const Key&) const; 98 // size_t count(const Key&) const;
90 // iterator find(const Key&); 99 // iterator find(const Key&);
91 // const_iterator find(const Key&) const; 100 // const_iterator find(const Key&) const;
92 // pair<iterator, iterator> equal_range(Key&) 101 // pair<iterator, iterator> equal_range(Key&)
93 // iterator lower_bound(const Key&); 102 // iterator lower_bound(const Key&);
94 // const_iterator lower_bound(const Key&) const; 103 // const_iterator lower_bound(const Key&) const;
95 // iterator upper_bound(const Key&); 104 // iterator upper_bound(const Key&);
96 // const_iterator upper_bound(const Key&) const; 105 // const_iterator upper_bound(const Key&) const;
97 // 106 //
98 // General functions 107 // General functions:
99 // void swap(flat_map&&) 108 // void swap(flat_map&&)
100 // 109 //
101 // Non-member operators: 110 // Non-member operators:
102 // bool operator==(const flat_map&, const flat_map); 111 // bool operator==(const flat_map&, const flat_map);
103 // bool operator!=(const flat_map&, const flat_map); 112 // bool operator!=(const flat_map&, const flat_map);
104 // bool operator<(const flat_map&, const flat_map); 113 // bool operator<(const flat_map&, const flat_map);
105 // bool operator>(const flat_map&, const flat_map); 114 // bool operator>(const flat_map&, const flat_map);
106 // bool operator>=(const flat_map&, const flat_map); 115 // bool operator>=(const flat_map&, const flat_map);
107 // bool operator<=(const flat_map&, const flat_map); 116 // bool operator<=(const flat_map&, const flat_map);
108 // 117 //
(...skipping 18 matching lines...) Expand all
127 using value_type = typename tree::value_type; 136 using value_type = typename tree::value_type;
128 137
129 // -------------------------------------------------------------------------- 138 // --------------------------------------------------------------------------
130 // Lifetime. 139 // Lifetime.
131 // 140 //
132 // Constructors that take range guarantee O(N * log(N)) + O(N) complexity 141 // Constructors that take range guarantee O(N * log(N)) + O(N) complexity
133 // (N is a range length). Thr range constructors are NOT stable. If there are 142 // (N is a range length). Thr range constructors are NOT stable. If there are
134 // duplicates an arbitrary one will be chosen. 143 // duplicates an arbitrary one will be chosen.
135 // 144 //
136 // Assume that move constructors invalidate iterators and references. 145 // Assume that move constructors invalidate iterators and references.
146 //
147 // The constructors that take ranges, lists, and vectors do not require that
148 // the input be sorted.
137 149
138 flat_map(); 150 flat_map();
139 explicit flat_map(const Compare& comp); 151 explicit flat_map(const Compare& comp);
140 152
141 // Not stable in the presence of duplicates in the initializer list.
142 template <class InputIterator> 153 template <class InputIterator>
143 flat_map(InputIterator first, 154 flat_map(InputIterator first,
144 InputIterator last, 155 InputIterator last,
156 FlatContainerDupes dupe_handling,
145 const Compare& comp = Compare()); 157 const Compare& comp = Compare());
146 158
147 flat_map(const flat_map&); 159 flat_map(const flat_map&);
148 flat_map(flat_map&&); 160 flat_map(flat_map&&);
149 161
150 // Not stable in the presence of duplicates in the initializer list. 162 flat_map(std::vector<value_type> items,
163 FlatContainerDupes dupe_handling,
164 const Compare& comp = Compare());
165
151 flat_map(std::initializer_list<value_type> ilist, 166 flat_map(std::initializer_list<value_type> ilist,
167 FlatContainerDupes dupe_handling,
152 const Compare& comp = Compare()); 168 const Compare& comp = Compare());
153 169
154 ~flat_map(); 170 ~flat_map();
155 171
156 // -------------------------------------------------------------------------- 172 // --------------------------------------------------------------------------
157 // Assignments. 173 // Assignments.
158 // 174 //
159 // Assume that move assignment invalidates iterators and references. 175 // Assume that move assignment invalidates iterators and references.
160 176
161 flat_map& operator=(const flat_map&); 177 flat_map& operator=(const flat_map&);
162 flat_map& operator=(flat_map&&); 178 flat_map& operator=(flat_map&&);
163 // Not stable in the presence of duplicates in the initializer list. 179 // Takes the first if there are duplicates in the initializer list.
164 flat_map& operator=(std::initializer_list<value_type> ilist); 180 flat_map& operator=(std::initializer_list<value_type> ilist);
165 181
166 // -------------------------------------------------------------------------- 182 // --------------------------------------------------------------------------
167 // Map-specific insert operations. 183 // Map-specific insert operations.
168 // 184 //
169 // Normal insert() functions are inherited from flat_tree. 185 // Normal insert() functions are inherited from flat_tree.
170 // 186 //
171 // Assume that every operation invalidates iterators and references. 187 // Assume that every operation invalidates iterators and references.
172 // Insertion of one element can take O(size). 188 // Insertion of one element can take O(size).
173 189
(...skipping 16 matching lines...) Expand all
190 template <class Key, class Mapped, class Compare> 206 template <class Key, class Mapped, class Compare>
191 flat_map<Key, Mapped, Compare>::flat_map() = default; 207 flat_map<Key, Mapped, Compare>::flat_map() = default;
192 208
193 template <class Key, class Mapped, class Compare> 209 template <class Key, class Mapped, class Compare>
194 flat_map<Key, Mapped, Compare>::flat_map(const Compare& comp) : tree(comp) {} 210 flat_map<Key, Mapped, Compare>::flat_map(const Compare& comp) : tree(comp) {}
195 211
196 template <class Key, class Mapped, class Compare> 212 template <class Key, class Mapped, class Compare>
197 template <class InputIterator> 213 template <class InputIterator>
198 flat_map<Key, Mapped, Compare>::flat_map(InputIterator first, 214 flat_map<Key, Mapped, Compare>::flat_map(InputIterator first,
199 InputIterator last, 215 InputIterator last,
216 FlatContainerDupes dupe_handling,
200 const Compare& comp) 217 const Compare& comp)
201 : tree(first, last, comp) {} 218 : tree(first, last, dupe_handling, comp) {}
202 219
203 template <class Key, class Mapped, class Compare> 220 template <class Key, class Mapped, class Compare>
204 flat_map<Key, Mapped, Compare>::flat_map(const flat_map&) = default; 221 flat_map<Key, Mapped, Compare>::flat_map(const flat_map&) = default;
205 222
206 template <class Key, class Mapped, class Compare> 223 template <class Key, class Mapped, class Compare>
207 flat_map<Key, Mapped, Compare>::flat_map(flat_map&&) = default; 224 flat_map<Key, Mapped, Compare>::flat_map(flat_map&&) = default;
208 225
209 template <class Key, class Mapped, class Compare> 226 template <class Key, class Mapped, class Compare>
227 flat_map<Key, Mapped, Compare>::flat_map(std::vector<value_type> items,
228 FlatContainerDupes dupe_handling,
229 const Compare& comp)
230 : tree(std::move(items), dupe_handling, comp) {}
231
232 template <class Key, class Mapped, class Compare>
210 flat_map<Key, Mapped, Compare>::flat_map( 233 flat_map<Key, Mapped, Compare>::flat_map(
211 std::initializer_list<value_type> ilist, 234 std::initializer_list<value_type> ilist,
235 FlatContainerDupes dupe_handling,
212 const Compare& comp) 236 const Compare& comp)
213 : flat_map(std::begin(ilist), std::end(ilist), comp) {} 237 : flat_map(std::begin(ilist), std::end(ilist), dupe_handling, comp) {}
214 238
215 template <class Key, class Mapped, class Compare> 239 template <class Key, class Mapped, class Compare>
216 flat_map<Key, Mapped, Compare>::~flat_map() = default; 240 flat_map<Key, Mapped, Compare>::~flat_map() = default;
217 241
218 // ---------------------------------------------------------------------------- 242 // ----------------------------------------------------------------------------
219 // Assignments. 243 // Assignments.
220 244
221 template <class Key, class Mapped, class Compare> 245 template <class Key, class Mapped, class Compare>
222 auto flat_map<Key, Mapped, Compare>::operator=(const flat_map&) 246 auto flat_map<Key, Mapped, Compare>::operator=(const flat_map&)
223 -> flat_map& = default; 247 -> flat_map& = default;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 // General operations. 281 // General operations.
258 282
259 template <class Key, class Mapped, class Compare> 283 template <class Key, class Mapped, class Compare>
260 void flat_map<Key, Mapped, Compare>::swap(flat_map& other) { 284 void flat_map<Key, Mapped, Compare>::swap(flat_map& other) {
261 tree::swap(other); 285 tree::swap(other);
262 } 286 }
263 287
264 } // namespace base 288 } // namespace base
265 289
266 #endif // BASE_CONTAINERS_FLAT_MAP_H_ 290 #endif // BASE_CONTAINERS_FLAT_MAP_H_
OLDNEW
« no previous file with comments | « no previous file | base/containers/flat_map_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698