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

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

Issue 2944523002: Improving flat containers interface. (Closed)
Patch Set: Removing redundant tests. Created 3 years, 6 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
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"
11 #include "base/functional.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 13
13 namespace base { 14 namespace base {
14 15
15 namespace internal { 16 namespace internal {
16 17
17 // An implementation of the flat_tree GetKeyFromValue template parameter that 18 // An implementation of the flat_tree GetKeyFromValue template parameter that
18 // extracts the key as the first element of a pair. 19 // extracts the key as the first element of a pair.
19 template <class Key, class Mapped> 20 template <class Key, class Mapped>
20 struct GetKeyFromValuePairFirst { 21 struct GetKeyFromValuePairFirst {
21 const Key& operator()(const std::pair<Key, Mapped>& p) const { 22 const Key& operator()(const std::pair<Key, Mapped>& p) const {
22 return p.first; 23 return p.first;
23 } 24 }
24 }; 25 };
25 26
26 } // namespace internal 27 } // namespace internal
27 28
28 // flat_map is a container with a std::map-like interface that stores its 29 // flat_map is a container with a std::map-like interface that stores its
29 // contents in a sorted vector. 30 // contents in a sorted vector.
30 // 31 //
31 // Please see //base/containers/README.md for an overview of which container 32 // Please see //base/containers/README.md for an overview of which container
32 // to select. 33 // to select.
33 // 34 //
34 // PROS 35 // PROS
35 // 36 //
36 // - Good memory locality. 37 // - Good memory locality.
37 // - Low overhead, especially for smaller maps. 38 // - Low overhead, especially for smaller maps.
38 // - Performance is good for more workloads than you might expect (see 39 // - Performance is good for more workloads than you might expect (see
39 // overview link above). 40 // overview link above).
41 // - Supports C++14 set interface.
42 // - Uses base::less which can compare pointers and smart pointers.
40 // 43 //
41 // CONS 44 // CONS
42 // 45 //
43 // - Inserts and removals are O(n). 46 // - Inserts and removals are O(n).
44 // 47 //
45 // IMPORTANT NOTES 48 // IMPORTANT NOTES
46 // 49 //
47 // - Iterators are invalidated across mutations. 50 // - Iterators are invalidated across mutations.
48 // - If possible, construct a flat_map in one operation by inserting into 51 // - If possible, construct a flat_map in one operation by inserting into
49 // a std::vector and moving that vector into the flat_map constructor. 52 // a std::vector and moving that vector into the flat_map constructor.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 // const_iterator end() const; 90 // const_iterator end() const;
88 // const_iterator cend() const; 91 // const_iterator cend() const;
89 // reverse_iterator rbegin(); 92 // reverse_iterator rbegin();
90 // const reverse_iterator rbegin() const; 93 // const reverse_iterator rbegin() const;
91 // const_reverse_iterator crbegin() const; 94 // const_reverse_iterator crbegin() const;
92 // reverse_iterator rend(); 95 // reverse_iterator rend();
93 // const_reverse_iterator rend() const; 96 // const_reverse_iterator rend() const;
94 // const_reverse_iterator crend() const; 97 // const_reverse_iterator crend() const;
95 // 98 //
96 // Insert and accessor functions: 99 // Insert and accessor functions:
97 // Mapped& operator[](const Key&); 100 // Mapped& operator[](const key_type&);
98 // Mapped& operator[](Key&&); 101 // Mapped& operator[](key_type&&);
99 // pair<iterator, bool> insert(const pair<Key, Mapped>&); 102 // pair<iterator, bool> insert(const value_type&);
100 // pair<iterator, bool> insert(pair<Key, Mapped>&&); 103 // pair<iterator, bool> insert(value_type&&);
101 // void insert(InputIterator first, InputIterator last, 104 // void insert(InputIterator first, InputIterator last,
102 // FlatContainerDupes); 105 // FlatContainerDupes);
106 // template <typename ...Args>
brettw 2017/06/20 18:13:35 I find these ...Args template lines make it more d
103 // pair<iterator, bool> emplace(Args&&...); 107 // pair<iterator, bool> emplace(Args&&...);
108 // template <typename ...Args>
104 // iterator emplace_hint(const_iterator, Args&&...); 109 // iterator emplace_hint(const_iterator, Args&&...);
105 // 110 //
106 // Erase functions: 111 // Erase functions:
112 // iterator erase(iterator);
107 // iterator erase(const_iterator); 113 // iterator erase(const_iterator);
108 // iterator erase(const_iterator first, const_iterator& last); 114 // iterator erase(const_iterator first, const_iterator& last);
109 // size_t erase(const Key& key) 115 // template <typename K>
116 // size_t erase(const K& key)
brettw 2017/06/20 18:13:35 Here and in the ones below we really need the temp
110 // 117 //
111 // Comparators (see std::map documentation). 118 // Comparators (see std::map documentation).
112 // key_compare key_comp() const; 119 // key_compare key_comp() const;
113 // value_compare value_comp() const; 120 // value_compare value_comp() const;
114 // 121 //
115 // Search functions: 122 // Search functions:
116 // size_t count(const Key&) const; 123 // template <typename K>
117 // iterator find(const Key&); 124 // size_t count(const K&) const;
118 // const_iterator find(const Key&) const; 125 // template <typename K>
119 // pair<iterator, iterator> equal_range(Key&) 126 // iterator find(const K&);
120 // iterator lower_bound(const Key&); 127 // template <typename K>
121 // const_iterator lower_bound(const Key&) const; 128 // const_iterator find(const K&) const;
122 // iterator upper_bound(const Key&); 129 // template <typename K>
123 // const_iterator upper_bound(const Key&) const; 130 // pair<iterator, iterator> equal_range(const K&);
131 // template <typename K>
132 // iterator lower_bound(const K&);
133 // template <typename K>
134 // const_iterator lower_bound(const K&) const;
135 // template <typename K>
136 // iterator upper_bound(const K&);
137 // const_iterator upper_bound(const K&) const;
124 // 138 //
125 // General functions: 139 // General functions:
126 // void swap(flat_map&&) 140 // void swap(flat_map&&)
127 // 141 //
128 // Non-member operators: 142 // Non-member operators:
129 // bool operator==(const flat_map&, const flat_map); 143 // bool operator==(const flat_map&, const flat_map);
130 // bool operator!=(const flat_map&, const flat_map); 144 // bool operator!=(const flat_map&, const flat_map);
131 // bool operator<(const flat_map&, const flat_map); 145 // bool operator<(const flat_map&, const flat_map);
132 // bool operator>(const flat_map&, const flat_map); 146 // bool operator>(const flat_map&, const flat_map);
133 // bool operator>=(const flat_map&, const flat_map); 147 // bool operator>=(const flat_map&, const flat_map);
134 // bool operator<=(const flat_map&, const flat_map); 148 // bool operator<=(const flat_map&, const flat_map);
135 // 149 //
136 template <class Key, class Mapped, class Compare = std::less<Key>> 150 template <class Key, class Mapped, class Compare = less>
137 // Meets the requirements of Container, AssociativeContainer, 151 // Meets the requirements of Container, AssociativeContainer,
138 // ReversibleContainer. 152 // ReversibleContainer.
139 // Requires: Key is Movable, Compare is a StrictWeakOrdering on Key. 153 // Requires: Key is Movable, Compare is a StrictWeakOrdering on Key.
140 class flat_map : public ::base::internal::flat_tree< 154 class flat_map : public ::base::internal::flat_tree<
141 Key, 155 Key,
142 std::pair<Key, Mapped>, 156 std::pair<Key, Mapped>,
143 ::base::internal::GetKeyFromValuePairFirst<Key, Mapped>, 157 ::base::internal::GetKeyFromValuePairFirst<Key, Mapped>,
144 Compare> { 158 Compare> {
145 private: 159 private:
146 using tree = typename ::base::internal::flat_tree< 160 using tree = typename ::base::internal::flat_tree<
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 // General operations. 313 // General operations.
300 314
301 template <class Key, class Mapped, class Compare> 315 template <class Key, class Mapped, class Compare>
302 void flat_map<Key, Mapped, Compare>::swap(flat_map& other) { 316 void flat_map<Key, Mapped, Compare>::swap(flat_map& other) {
303 tree::swap(other); 317 tree::swap(other);
304 } 318 }
305 319
306 } // namespace base 320 } // namespace base
307 321
308 #endif // BASE_CONTAINERS_FLAT_MAP_H_ 322 #endif // BASE_CONTAINERS_FLAT_MAP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698