Commit 10423db9 authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[base] Enable copy and move from underlying_type in flat_tree

This change adds copy and move constructors from the underlying_storage
to base::flat_tree, replacing the previous by value constructor. This
results in a small win for perf and binary size, as the number of
temporaries is reduced.

Bug: 682254
Change-Id: I0c3c8fd6e785556ada363cf406d7f58f22a60284
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2037674Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#738627}
parent 2cfdca0e
...@@ -63,7 +63,9 @@ struct GetKeyFromValuePairFirst { ...@@ -63,7 +63,9 @@ struct GetKeyFromValuePairFirst {
// const Compare& compare = Compare()); // const Compare& compare = Compare());
// flat_map(const flat_map&); // flat_map(const flat_map&);
// flat_map(flat_map&&); // flat_map(flat_map&&);
// flat_map(std::vector<value_type>, // flat_map(const std::vector<value_type>& items,
// const Compare& compare = Compare());
// flat_map(std::vector<value_type>&& items,
// const Compare& compare = Compare()); // Re-use storage. // const Compare& compare = Compare()); // Re-use storage.
// flat_map(std::initializer_list<value_type> ilist, // flat_map(std::initializer_list<value_type> ilist,
// const Compare& comp = Compare()); // const Compare& comp = Compare());
...@@ -159,6 +161,7 @@ class flat_map : public ::base::internal::flat_tree< ...@@ -159,6 +161,7 @@ class flat_map : public ::base::internal::flat_tree<
std::pair<Key, Mapped>, std::pair<Key, Mapped>,
::base::internal::GetKeyFromValuePairFirst<Key, Mapped>, ::base::internal::GetKeyFromValuePairFirst<Key, Mapped>,
Compare>; Compare>;
using underlying_type = typename tree::underlying_type;
public: public:
using key_type = typename tree::key_type; using key_type = typename tree::key_type;
...@@ -186,8 +189,8 @@ class flat_map : public ::base::internal::flat_tree< ...@@ -186,8 +189,8 @@ class flat_map : public ::base::internal::flat_tree<
flat_map(const flat_map&) = default; flat_map(const flat_map&) = default;
flat_map(flat_map&&) noexcept = default; flat_map(flat_map&&) noexcept = default;
flat_map(std::vector<value_type> items, flat_map(const underlying_type& items, const Compare& comp = Compare());
const Compare& comp = Compare()); flat_map(underlying_type&& items, const Compare& comp = Compare());
flat_map(std::initializer_list<value_type> ilist, flat_map(std::initializer_list<value_type> ilist,
const Compare& comp = Compare()); const Compare& comp = Compare());
...@@ -254,7 +257,12 @@ flat_map<Key, Mapped, Compare>::flat_map(InputIterator first, ...@@ -254,7 +257,12 @@ flat_map<Key, Mapped, Compare>::flat_map(InputIterator first,
: tree(first, last, comp) {} : tree(first, last, comp) {}
template <class Key, class Mapped, class Compare> template <class Key, class Mapped, class Compare>
flat_map<Key, Mapped, Compare>::flat_map(std::vector<value_type> items, flat_map<Key, Mapped, Compare>::flat_map(const underlying_type& items,
const Compare& comp)
: tree(items, comp) {}
template <class Key, class Mapped, class Compare>
flat_map<Key, Mapped, Compare>::flat_map(underlying_type&& items,
const Compare& comp) const Compare& comp)
: tree(std::move(items), comp) {} : tree(std::move(items), comp) {}
......
...@@ -49,7 +49,9 @@ namespace base { ...@@ -49,7 +49,9 @@ namespace base {
// const Compare& compare = Compare()); // const Compare& compare = Compare());
// flat_set(const flat_set&); // flat_set(const flat_set&);
// flat_set(flat_set&&); // flat_set(flat_set&&);
// flat_set(std::vector<Key>, // flat_set(const std::vector<Key>& items,
// const Compare& compare = Compare());
// flat_set(std::vector<Key>&& items,
// const Compare& compare = Compare()); // Re-use storage. // const Compare& compare = Compare()); // Re-use storage.
// flat_set(std::initializer_list<value_type> ilist, // flat_set(std::initializer_list<value_type> ilist,
// const Compare& comp = Compare()); // const Compare& comp = Compare());
......
...@@ -48,7 +48,7 @@ struct IsTransparentCompare<T, void_t<typename T::is_transparent>> ...@@ -48,7 +48,7 @@ struct IsTransparentCompare<T, void_t<typename T::is_transparent>>
// const Key& operator()(const Value&). // const Key& operator()(const Value&).
template <class Key, class Value, class GetKeyFromValue, class KeyCompare> template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
class flat_tree { class flat_tree {
private: protected:
using underlying_type = std::vector<Value>; using underlying_type = std::vector<Value>;
public: public:
...@@ -109,8 +109,9 @@ class flat_tree { ...@@ -109,8 +109,9 @@ class flat_tree {
flat_tree(const flat_tree&); flat_tree(const flat_tree&);
flat_tree(flat_tree&&) noexcept = default; flat_tree(flat_tree&&) noexcept = default;
flat_tree(std::vector<value_type> items, flat_tree(const underlying_type& items,
const key_compare& comp = key_compare()); const key_compare& comp = key_compare());
flat_tree(underlying_type&& items, const key_compare& comp = key_compare());
flat_tree(std::initializer_list<value_type> ilist, flat_tree(std::initializer_list<value_type> ilist,
const key_compare& comp = key_compare()); const key_compare& comp = key_compare());
...@@ -472,7 +473,15 @@ flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( ...@@ -472,7 +473,15 @@ flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree(
template <class Key, class Value, class GetKeyFromValue, class KeyCompare> template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree( flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree(
std::vector<value_type> items, const underlying_type& items,
const KeyCompare& comp)
: impl_(comp, items) {
sort_and_unique(begin(), end());
}
template <class Key, class Value, class GetKeyFromValue, class KeyCompare>
flat_tree<Key, Value, GetKeyFromValue, KeyCompare>::flat_tree(
underlying_type&& items,
const KeyCompare& comp) const KeyCompare& comp)
: impl_(comp, std::move(items)) { : impl_(comp, std::move(items)) {
sort_and_unique(begin(), end()); sort_and_unique(begin(), end());
......
...@@ -338,9 +338,19 @@ TEST(FlatTree, MoveConstructor) { ...@@ -338,9 +338,19 @@ TEST(FlatTree, MoveConstructor) {
EXPECT_EQ(1U, moved.count(MoveOnlyInt(4))); EXPECT_EQ(1U, moved.count(MoveOnlyInt(4)));
} }
// flat_tree(std::vector<value_type>) // flat_tree(const std::vector<value_type>&)
TEST(FlatTree, VectorConstructor) { TEST(FlatTree, VectorCopyConstructor) {
std::vector<int> items = {1, 2, 3, 4};
IntTree tree = items;
EXPECT_THAT(tree, ElementsAre(1, 2, 3, 4));
EXPECT_THAT(items, ElementsAre(1, 2, 3, 4));
}
// flat_tree(std::vector<value_type>&&)
TEST(FlatTree, VectorMoveConstructor) {
using Pair = std::pair<int, MoveOnlyInt>; using Pair = std::pair<int, MoveOnlyInt>;
// Construct an unsorted vector with a duplicate item in it. Sorted by the // Construct an unsorted vector with a duplicate item in it. Sorted by the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment