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

Side by Side Diff: base/containers/README.md

Issue 2944523002: Improving flat containers interface. (Closed)
Patch Set: Review, minimizing diff 2. Created 3 years, 5 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 | « base/bind_internal.h ('k') | base/containers/container_test_utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # base/containers library 1 # base/containers library
2 2
3 ## What goes here 3 ## What goes here
4 4
5 This directory contains some STL-like containers. 5 This directory contains some STL-like containers.
6 6
7 Things should be moved here that are generally applicable across the code base. 7 Things should be moved here that are generally applicable across the code base.
8 Don't add things here just because you need them in one place and think others 8 Don't add things here just because you need them in one place and think others
9 may someday want something similar. You can put specialized containers in 9 may someday want something similar. You can put specialized containers in
10 your component's directory and we can promote them here later if we feel there 10 your component's directory and we can promote them here later if we feel there
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 set sizes, std::vector's doubling-when-full strategy can waste memory. 117 set sizes, std::vector's doubling-when-full strategy can waste memory.
118 118
119 Supports efficient construction from a vector of items which avoids the O(n^2) 119 Supports efficient construction from a vector of items which avoids the O(n^2)
120 insertion time of each element separately. 120 insertion time of each element separately.
121 121
122 The per-item overhead will depend on the underlying std::vector's reallocation 122 The per-item overhead will depend on the underlying std::vector's reallocation
123 strategy and the memory access pattern. Assuming items are being linearly added, 123 strategy and the memory access pattern. Assuming items are being linearly added,
124 one would expect it to be 3/4 full, so per-item overhead will be 0.25 * 124 one would expect it to be 3/4 full, so per-item overhead will be 0.25 *
125 sizeof(T). 125 sizeof(T).
126 126
127
128 flat\_set/flat\_map support C++14 interface and a notion of transparent
Nico 2017/07/20 18:26:53 We're not on c++14 yet, and we don't know how and
danakj 2017/07/20 18:51:15 Do you think mentioning c++14 is bad? It gives a r
129 comparisons. Therefore you can, for example, lookup base::StringPiece in a set
130 of std::strings without constructing a temporary std::string.
131 You can find more information about transparent comparisons here:
132 http://en.cppreference.com/w/cpp/utility/functional/less_void
133 Example, smart pointer set:
134 // Define a custom comparator.
135 struct UniquePtrComparator {
136 // Mark your comparison as transparent.
137 using is_transparent = int;
138 template <typename T>
139 bool operator()(const std::unique_ptr<T>& lhs,
140 const std::unique_ptr<T>& rhs) const {
141 return lhs < rhs;
142 }
143 template <typename T>
144 bool operator()(const T* lhs, const std::unique_ptr<T>& rhs) const {
145 return lhs < rhs.get();
146 }
147 template <typename T>
148 bool operator()(const std::unique_ptr<T>& lhs, const T* rhs) const {
149 return lhs.get() < rhs;
150 }
151 };
152 // Declare a typedef.
153 template <typename T>
154 using UniquePtrSet =
155 base::flat_set<std::unique_ptr<T>, UniquePtrComparator>;
156 // ...
157 // Collect data.
158 std::vector<std::unique_ptr<int>> ptr_vec;
159 ptr_vec.reserve(5);
160 std::generate_n(std::back_inserter(ptr_vec), 5, []{
161 return base::MakeUnique<int>(0);
162 });
163 // Construct a set.
164 UniquePtrSet<int> ptr_set(std::move(ptr_vec), base::KEEP_FIRST_OF_DUPES);
165 // Use raw pointers to lookup keys.
166 int* ptr = ptr_set.begin()->get();
167 EXPECT_TRUE(ptr_set.find(ptr) == ptr_set.begin());
168 Example flat_map<std::string, int>:
169 base::flat_map<std::string, int> str_to_int({{"a", 1}, {"c", 2},{"b", 2}},
170 base::KEEP_FIRST_OF_DUPES);
171 // Does not construct temporary strings.
172 str_to_int.find("c")->second = 3;
173 str_to_int.erase("c");
174 EXPECT_EQ(str_to_int.end(), str_to_int.find("c")->second);
175 // NOTE: This does construct a temporary string. This happens since if the
176 // item is not in the container, then it needs to be constructed, which is
177 // something that transparent comparators don't have to guarantee.
178 str_to_int["c"] = 3;
179
127 ### base::small\_map 180 ### base::small\_map
128 181
129 A small inline buffer that is brute-force searched that overflows into a full 182 A small inline buffer that is brute-force searched that overflows into a full
130 std::map or std::unordered\_map. This gives the memory benefit of 183 std::map or std::unordered\_map. This gives the memory benefit of
131 base::flat\_map for small data sizes without the degenerate insertion 184 base::flat\_map for small data sizes without the degenerate insertion
132 performance for large container sizes. 185 performance for large container sizes.
133 186
134 Since instantiations require both code for a std::map and a brute-force search 187 Since instantiations require both code for a std::map and a brute-force search
135 of the inline container, plus a fancy iterator to cover both cases, code size 188 of the inline container, plus a fancy iterator to cover both cases, code size
136 is larger. 189 is larger.
(...skipping 24 matching lines...) Expand all
161 printf("Found is %d\n", (int)(found == foo.end())); 214 printf("Found is %d\n", (int)(found == foo.end()));
162 found = foo.find("bar"); 215 found = foo.find("bar");
163 printf("Found is %d\n", (int)(found == foo.end())); 216 printf("Found is %d\n", (int)(found == foo.end()));
164 found = foo.find("asdfhf"); 217 found = foo.find("asdfhf");
165 printf("Found is %d\n", (int)(found == foo.end())); 218 printf("Found is %d\n", (int)(found == foo.end()));
166 found = foo.find("bar1"); 219 found = foo.find("bar1");
167 printf("Found is %d\n", (int)(found == foo.end())); 220 printf("Found is %d\n", (int)(found == foo.end()));
168 } 221 }
169 ``` 222 ```
170 223
OLDNEW
« no previous file with comments | « base/bind_internal.h ('k') | base/containers/container_test_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698