Commit 28bbe069 authored by Daniel Cheng's avatar Daniel Cheng Committed by Commit Bot

[base] Fix sorted_unique flat_map constructors when values are unsortable

Bug: none
Change-Id: I5f9ad4c4b10090b8fb9d76acf86f24cf71d5d1c2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2548964
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Auto-Submit: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarJan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#829143}
parent 0ed5bf8b
......@@ -22,7 +22,7 @@ namespace internal {
// extracts the key as the first element of a pair.
struct GetFirst {
template <class Key, class Mapped>
const Key& operator()(const std::pair<Key, Mapped>& p) const {
constexpr const Key& operator()(const std::pair<Key, Mapped>& p) const {
return p.first;
}
};
......
......@@ -22,6 +22,23 @@ using ::testing::ElementsAre;
namespace base {
namespace {
struct Unsortable {
int value;
};
bool operator==(const Unsortable& lhs, const Unsortable& rhs) {
return lhs.value == rhs.value;
}
bool operator<(const Unsortable& lhs, const Unsortable& rhs) = delete;
bool operator<=(const Unsortable& lhs, const Unsortable& rhs) = delete;
bool operator>(const Unsortable& lhs, const Unsortable& rhs) = delete;
bool operator>=(const Unsortable& lhs, const Unsortable& rhs) = delete;
} // namespace
TEST(FlatMap, IncompleteType) {
struct A {
using Map = flat_map<A, A>;
......@@ -79,6 +96,42 @@ TEST(FlatMap, InitializerListConstructor) {
std::make_pair(10, 10)));
}
TEST(FlatMap, SortedRangeConstructor) {
using PairType = std::pair<int, Unsortable>;
using MapType = flat_map<int, Unsortable>;
MapType::value_type input_vals[] = {{1, {1}}, {2, {1}}, {3, {1}}};
MapType map(sorted_unique, std::begin(input_vals), std::end(input_vals));
EXPECT_THAT(
map, ElementsAre(PairType(1, {1}), PairType(2, {1}), PairType(3, {1})));
}
TEST(FlatMap, SortedCopyFromVectorConstructor) {
using PairType = std::pair<int, Unsortable>;
using MapType = flat_map<int, Unsortable>;
std::vector<PairType> vect{{1, {1}}, {2, {1}}};
MapType map(sorted_unique, vect);
EXPECT_THAT(map, ElementsAre(PairType(1, {1}), PairType(2, {1})));
}
TEST(FlatMap, SortedMoveFromVectorConstructor) {
using PairType = std::pair<int, Unsortable>;
using MapType = flat_map<int, Unsortable>;
std::vector<PairType> vect{{1, {1}}, {2, {1}}};
MapType map(sorted_unique, std::move(vect));
EXPECT_THAT(map, ElementsAre(PairType(1, {1}), PairType(2, {1})));
}
TEST(FlatMap, SortedInitializerListConstructor) {
using PairType = std::pair<int, Unsortable>;
flat_map<int, Unsortable> map(
sorted_unique,
{{1, {1}}, {2, {2}}, {3, {3}}, {4, {4}}, {5, {5}}, {8, {8}}, {10, {10}}});
EXPECT_THAT(map,
ElementsAre(PairType(1, {1}), PairType(2, {2}), PairType(3, {3}),
PairType(4, {4}), PairType(5, {5}), PairType(8, {8}),
PairType(10, {10})));
}
TEST(FlatMap, InitializerListAssignment) {
flat_map<int, int> cont;
cont = {{1, 1}, {2, 2}};
......
......@@ -153,7 +153,8 @@ class flat_tree {
constexpr explicit value_compare(Cmp&& compare_arg)
: KeyCompare(std::forward<Cmp>(compare_arg)) {}
bool operator()(const value_type& left, const value_type& right) const {
constexpr bool operator()(const value_type& left,
const value_type& right) const {
GetKeyFromValue extractor;
return key_compare::operator()(extractor(left), extractor(right));
}
......@@ -346,8 +347,8 @@ class flat_tree {
// --------------------------------------------------------------------------
// Comparators.
key_compare key_comp() const;
value_compare value_comp() const;
constexpr key_compare key_comp() const;
constexpr value_compare value_comp() const;
// --------------------------------------------------------------------------
// Search operations.
......@@ -560,8 +561,8 @@ class flat_tree {
: value_compare(std::forward<Cmp>(compare_arg)),
body_(std::forward<Body>(underlying_type_args)...) {}
const value_compare& get_value_comp() const { return *this; }
const key_compare& get_key_comp() const { return *this; }
constexpr const value_compare& get_value_comp() const { return *this; }
constexpr const key_compare& get_key_comp() const { return *this; }
container_type body_;
} impl_;
......@@ -620,7 +621,7 @@ flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::flat_tree(
InputIterator last,
const KeyCompare& comp)
: impl_(comp, first, last) {
DCHECK(is_sorted_and_unique(*this, comp));
DCHECK(is_sorted_and_unique(*this, value_comp()));
}
template <class Key, class GetKeyFromValue, class KeyCompare, class Container>
......@@ -629,7 +630,7 @@ flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::flat_tree(
const container_type& items,
const KeyCompare& comp)
: impl_(comp, items) {
DCHECK(is_sorted_and_unique(*this, comp));
DCHECK(is_sorted_and_unique(*this, value_comp()));
}
template <class Key, class GetKeyFromValue, class KeyCompare, class Container>
......@@ -638,7 +639,7 @@ constexpr flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::flat_tree(
container_type&& items,
const KeyCompare& comp)
: impl_(comp, std::move(items)) {
DCHECK(is_sorted_and_unique(*this, comp));
DCHECK(is_sorted_and_unique(*this, value_comp()));
}
template <class Key, class GetKeyFromValue, class KeyCompare, class Container>
......@@ -935,13 +936,15 @@ auto flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::erase(
// Comparators.
template <class Key, class GetKeyFromValue, class KeyCompare, class Container>
auto flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::key_comp() const
constexpr auto
flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::key_comp() const
-> key_compare {
return impl_.get_key_comp();
}
template <class Key, class GetKeyFromValue, class KeyCompare, class Container>
auto flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::value_comp() const
constexpr auto
flat_tree<Key, GetKeyFromValue, KeyCompare, Container>::value_comp() const
-> value_compare {
return impl_.get_value_comp();
}
......
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