add operators interface

This commit is contained in:
2019-05-04 12:59:35 +07:00
parent d95c6f9b05
commit 4e6c24a126
4 changed files with 94 additions and 15 deletions

View File

@@ -247,15 +247,43 @@ namespace flat_hpp
data_type data_;
key_compare compare_;
};
}
template < typename Key
, typename Value
, typename Compare
, typename Allocator >
void swap(
flat_map<Key, Value, Compare, Allocator>& l,
flat_map<Key, Value, Compare, Allocator>& r)
{
namespace flat_hpp
{
template < typename K, typename V, typename C, typename A >
void swap(flat_map<K, V, C, A>& l, flat_map<K, V, C, A>& r) {
l.swap(r);
}
template < typename K, typename V, typename C, typename A >
bool operator==(const flat_map<K, V, C, A>& l, const flat_map<K, V, C, A>& r) {
return l.size() == r.size()
&& std::equal(l.begin(), l.end(), r.begin(), r.end());
}
template < typename K, typename V, typename C, typename A >
bool operator!=(const flat_map<K, V, C, A>& l, const flat_map<K, V, C, A>& r) {
return !(l == r);
}
template < typename K, typename V, typename C, typename A >
bool operator<(const flat_map<K, V, C, A>& l, const flat_map<K, V, C, A>& r) {
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
}
template < typename K, typename V, typename C, typename A >
bool operator>(const flat_map<K, V, C, A>& l, const flat_map<K, V, C, A>& r) {
return r < l;
}
template < typename K, typename V, typename C, typename A >
bool operator<=(const flat_map<K, V, C, A>& l, const flat_map<K, V, C, A>& r) {
return !(r < l);
}
template < typename K, typename V, typename C, typename A >
bool operator>=(const flat_map<K, V, C, A>& l, const flat_map<K, V, C, A>& r) {
return !(l < r);
}
}

View File

@@ -158,4 +158,15 @@ TEST_CASE("flat_map") {
my_as_const(s0).key_comp();
my_as_const(s0).value_comp();
}
SECTION("operators") {
using map_t = flat_map<int, unsigned>;
map_t s0;
map_t s1;
REQUIRE(s0 == s1);
REQUIRE_FALSE(s0 != s1);
REQUIRE_FALSE(s0 < s1);
REQUIRE_FALSE(s0 > s1);
REQUIRE(s0 <= s1);
REQUIRE(s0 >= s1);
}
}

View File

@@ -233,14 +233,43 @@ namespace flat_hpp
data_type data_;
key_compare compare_;
};
}
template < typename Key
, typename Compare
, typename Allocator >
void swap(
flat_set<Key, Compare, Allocator>& l,
flat_set<Key, Compare, Allocator>& r)
{
namespace flat_hpp
{
template < typename K, typename C, typename A >
void swap(flat_set<K, C, A>& l, flat_set<K, C, A>& r) {
l.swap(r);
}
template < typename K, typename C, typename A >
bool operator==(const flat_set<K, C, A>& l, const flat_set<K, C, A>& r) {
return l.size() == r.size()
&& std::equal(l.begin(), l.end(), r.begin(), r.end());
}
template < typename K, typename C, typename A >
bool operator!=(const flat_set<K, C, A>& l, const flat_set<K, C, A>& r) {
return !(l == r);
}
template < typename K, typename C, typename A >
bool operator<(const flat_set<K, C, A>& l, const flat_set<K, C, A>& r) {
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
}
template < typename K, typename C, typename A >
bool operator>(const flat_set<K, C, A>& l, const flat_set<K, C, A>& r) {
return r < l;
}
template < typename K, typename C, typename A >
bool operator<=(const flat_set<K, C, A>& l, const flat_set<K, C, A>& r) {
return !(r < l);
}
template < typename K, typename C, typename A >
bool operator>=(const flat_set<K, C, A>& l, const flat_set<K, C, A>& r) {
return !(l < r);
}
}

View File

@@ -149,4 +149,15 @@ TEST_CASE("flat_set") {
my_as_const(s0).key_comp();
my_as_const(s0).value_comp();
}
SECTION("operators") {
using set_t = flat_set<int>;
set_t s0;
set_t s1;
REQUIRE(s0 == s1);
REQUIRE_FALSE(s0 != s1);
REQUIRE_FALSE(s0 < s1);
REQUIRE_FALSE(s0 > s1);
REQUIRE(s0 <= s1);
REQUIRE(s0 >= s1);
}
}