mirror of
https://github.com/BlackMATov/flat.hpp.git
synced 2025-12-15 02:12:23 +07:00
EBO for flat_map
This commit is contained in:
@@ -17,32 +17,55 @@
|
|||||||
|
|
||||||
namespace flat_hpp
|
namespace flat_hpp
|
||||||
{
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
template < typename Value, typename Compare >
|
||||||
|
class flat_map_compare : public Compare {
|
||||||
|
public:
|
||||||
|
flat_map_compare() = default;
|
||||||
|
|
||||||
|
flat_map_compare(const Compare& compare)
|
||||||
|
: Compare(compare) {}
|
||||||
|
|
||||||
|
bool operator()(
|
||||||
|
const typename Value::first_type& l,
|
||||||
|
const typename Value::first_type& r) const
|
||||||
|
{
|
||||||
|
return Compare::operator()(l, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator()(
|
||||||
|
const typename Value::first_type& l,
|
||||||
|
const Value& r) const
|
||||||
|
{
|
||||||
|
return Compare::operator()(l, r.first);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator()(
|
||||||
|
const Value& l,
|
||||||
|
const typename Value::first_type& r) const
|
||||||
|
{
|
||||||
|
return Compare::operator()(l.first, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator()(const Value& l, const Value& r) const {
|
||||||
|
return Compare::operator()(l.first, r.first);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
template < typename Key
|
template < typename Key
|
||||||
, typename Value
|
, typename Value
|
||||||
, typename Compare = std::less<Key>
|
, typename Compare = std::less<Key>
|
||||||
, typename Container = std::vector<std::pair<Key, Value>> >
|
, typename Container = std::vector<std::pair<Key, Value>> >
|
||||||
class flat_map final {
|
class flat_map
|
||||||
class uber_comparer_type : public Compare {
|
: private detail::flat_map_compare<
|
||||||
public:
|
typename Container::value_type,
|
||||||
uber_comparer_type() = default;
|
Compare>
|
||||||
uber_comparer_type(const Compare& c) : Compare(c) {}
|
{
|
||||||
|
using base_type = detail::flat_map_compare<
|
||||||
bool operator()(const Key& l, const Key& r) const {
|
typename Container::value_type,
|
||||||
return Compare::operator()(l, r);
|
Compare>;
|
||||||
}
|
|
||||||
|
|
||||||
bool operator()(const Key& l, typename Container::const_reference r) const {
|
|
||||||
return Compare::operator()(l, r.first);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator()(typename Container::const_reference l, const Key& r) const {
|
|
||||||
return Compare::operator()(l.first, r);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator()(typename Container::const_reference l, typename Container::const_reference r) const {
|
|
||||||
return Compare::operator()(l.first, r.first);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
public:
|
public:
|
||||||
using key_type = Key;
|
using key_type = Key;
|
||||||
using mapped_type = Value;
|
using mapped_type = Value;
|
||||||
@@ -64,23 +87,21 @@ namespace flat_hpp
|
|||||||
using reverse_iterator = typename Container::reverse_iterator;
|
using reverse_iterator = typename Container::reverse_iterator;
|
||||||
using const_reverse_iterator = typename Container::const_reverse_iterator;
|
using const_reverse_iterator = typename Container::const_reverse_iterator;
|
||||||
|
|
||||||
class value_compare {
|
class value_compare : private key_compare {
|
||||||
public:
|
public:
|
||||||
bool operator()(const value_type& l, const value_type& r) const {
|
bool operator()(const value_type& l, const value_type& r) const {
|
||||||
return compare_(l.first, r.first);
|
return key_compare::operator()(l.first, r.first);
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
friend class flat_map;
|
friend class flat_map;
|
||||||
explicit value_compare(key_compare compare)
|
explicit value_compare(key_compare compare)
|
||||||
: compare_(std::move(compare)) {}
|
: key_compare(std::move(compare)) {}
|
||||||
private:
|
|
||||||
key_compare compare_;
|
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
flat_map() {}
|
flat_map() {}
|
||||||
|
|
||||||
explicit flat_map(const Compare& c)
|
explicit flat_map(const Compare& c)
|
||||||
: compare_(c) {}
|
: base_type(c) {}
|
||||||
|
|
||||||
template < typename Allocator >
|
template < typename Allocator >
|
||||||
explicit flat_map(const Allocator& a)
|
explicit flat_map(const Allocator& a)
|
||||||
@@ -88,8 +109,8 @@ namespace flat_hpp
|
|||||||
|
|
||||||
template < typename Allocator >
|
template < typename Allocator >
|
||||||
flat_map(const Compare& c, const Allocator& a)
|
flat_map(const Compare& c, const Allocator& a)
|
||||||
: data_(a)
|
: base_type(c)
|
||||||
, compare_(c) {}
|
, data_(a) {}
|
||||||
|
|
||||||
template < typename InputIter >
|
template < typename InputIter >
|
||||||
flat_map(InputIter first, InputIter last) {
|
flat_map(InputIter first, InputIter last) {
|
||||||
@@ -98,7 +119,7 @@ namespace flat_hpp
|
|||||||
|
|
||||||
template < typename InputIter >
|
template < typename InputIter >
|
||||||
flat_map(InputIter first, InputIter last, const Compare& c)
|
flat_map(InputIter first, InputIter last, const Compare& c)
|
||||||
: compare_(c) {
|
: base_type(c) {
|
||||||
insert(first, last);
|
insert(first, last);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,8 +131,8 @@ namespace flat_hpp
|
|||||||
|
|
||||||
template < typename InputIter , typename Allocator >
|
template < typename InputIter , typename Allocator >
|
||||||
flat_map(InputIter first, InputIter last, const Compare& c, const Allocator& a)
|
flat_map(InputIter first, InputIter last, const Compare& c, const Allocator& a)
|
||||||
: data_(a)
|
: base_type(c)
|
||||||
, compare_(c) {
|
, data_(a) {
|
||||||
insert(first, last);
|
insert(first, last);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +141,7 @@ namespace flat_hpp
|
|||||||
}
|
}
|
||||||
|
|
||||||
flat_map(std::initializer_list<value_type> ilist, const Compare& c)
|
flat_map(std::initializer_list<value_type> ilist, const Compare& c)
|
||||||
: compare_(c) {
|
: base_type(c) {
|
||||||
insert(ilist);
|
insert(ilist);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,20 +153,20 @@ namespace flat_hpp
|
|||||||
|
|
||||||
template < typename Allocator >
|
template < typename Allocator >
|
||||||
flat_map(std::initializer_list<value_type> ilist, const Compare& c, const Allocator& a)
|
flat_map(std::initializer_list<value_type> ilist, const Compare& c, const Allocator& a)
|
||||||
: data_(a)
|
: base_type(c)
|
||||||
, compare_(c) {
|
, data_(a) {
|
||||||
insert(ilist);
|
insert(ilist);
|
||||||
}
|
}
|
||||||
|
|
||||||
template < typename Allocator >
|
template < typename Allocator >
|
||||||
flat_map(flat_map&& other, const Allocator& a)
|
flat_map(flat_map&& other, const Allocator& a)
|
||||||
: data_(std::move(other.data_), a)
|
: base_type(static_cast<base_type&&>(other))
|
||||||
, compare_(std::move(other.compare_)) {}
|
, data_(std::move(other.data_), a) {}
|
||||||
|
|
||||||
template < typename Allocator >
|
template < typename Allocator >
|
||||||
flat_map(const flat_map& other, const Allocator& a)
|
flat_map(const flat_map& other, const Allocator& a)
|
||||||
: data_(other.data_, a)
|
: base_type(static_cast<const base_type&>(other))
|
||||||
, compare_(other.compare_) {}
|
, data_(other.data_, a) {}
|
||||||
|
|
||||||
flat_map(flat_map&& other) = default;
|
flat_map(flat_map&& other) = default;
|
||||||
flat_map(const flat_map& other) = default;
|
flat_map(const flat_map& other) = default;
|
||||||
@@ -230,28 +251,28 @@ namespace flat_hpp
|
|||||||
|
|
||||||
std::pair<iterator, bool> insert(value_type&& value) {
|
std::pair<iterator, bool> insert(value_type&& value) {
|
||||||
const iterator iter = lower_bound(value.first);
|
const iterator iter = lower_bound(value.first);
|
||||||
return iter == end() || compare_(value, *iter)
|
return iter == end() || this->operator()(value, *iter)
|
||||||
? std::make_pair(data_.insert(iter, std::move(value)), true)
|
? std::make_pair(data_.insert(iter, std::move(value)), true)
|
||||||
: std::make_pair(iter, false);
|
: std::make_pair(iter, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<iterator, bool> insert(const value_type& value) {
|
std::pair<iterator, bool> insert(const value_type& value) {
|
||||||
const iterator iter = lower_bound(value.first);
|
const iterator iter = lower_bound(value.first);
|
||||||
return iter == end() || compare_(value, *iter)
|
return iter == end() || this->operator()(value, *iter)
|
||||||
? std::make_pair(data_.insert(iter, value), true)
|
? std::make_pair(data_.insert(iter, value), true)
|
||||||
: std::make_pair(iter, false);
|
: std::make_pair(iter, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator insert(const_iterator hint, value_type&& value) {
|
iterator insert(const_iterator hint, value_type&& value) {
|
||||||
return (hint == begin() || compare_(*(hint - 1), value))
|
return (hint == begin() || this->operator()(*(hint - 1), value))
|
||||||
&& (hint == end() || compare_(value, *hint))
|
&& (hint == end() || this->operator()(value, *hint))
|
||||||
? data_.insert(hint, std::move(value))
|
? data_.insert(hint, std::move(value))
|
||||||
: insert(std::move(value)).first;
|
: insert(std::move(value)).first;
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator insert(const_iterator hint, const value_type& value) {
|
iterator insert(const_iterator hint, const value_type& value) {
|
||||||
return (hint == begin() || compare_(*(hint - 1), value))
|
return (hint == begin() || this->operator()(*(hint - 1), value))
|
||||||
&& (hint == end() || compare_(value, *hint))
|
&& (hint == end() || this->operator()(value, *hint))
|
||||||
? data_.insert(hint, value)
|
? data_.insert(hint, value)
|
||||||
: insert(value).first;
|
: insert(value).first;
|
||||||
}
|
}
|
||||||
@@ -298,8 +319,10 @@ namespace flat_hpp
|
|||||||
|
|
||||||
void swap(flat_map& other) {
|
void swap(flat_map& other) {
|
||||||
using std::swap;
|
using std::swap;
|
||||||
|
swap(
|
||||||
|
static_cast<base_type&>(*this),
|
||||||
|
static_cast<base_type&>(other));
|
||||||
swap(data_, other.data_);
|
swap(data_, other.data_);
|
||||||
swap(compare_, other.compare_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_type count(const key_type& key) const {
|
size_type count(const key_type& key) const {
|
||||||
@@ -309,52 +332,57 @@ namespace flat_hpp
|
|||||||
|
|
||||||
iterator find(const key_type& key) {
|
iterator find(const key_type& key) {
|
||||||
const iterator iter = lower_bound(key);
|
const iterator iter = lower_bound(key);
|
||||||
return iter != end() && !compare_(key, iter->first)
|
return iter != end() && !this->operator()(key, iter->first)
|
||||||
? iter
|
? iter
|
||||||
: end();
|
: end();
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iterator find(const key_type& key) const {
|
const_iterator find(const key_type& key) const {
|
||||||
const const_iterator iter = lower_bound(key);
|
const const_iterator iter = lower_bound(key);
|
||||||
return iter != end() && !compare_(key, iter->first)
|
return iter != end() && !this->operator()(key, iter->first)
|
||||||
? iter
|
? iter
|
||||||
: end();
|
: end();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<iterator, iterator> equal_range(const key_type& key) {
|
std::pair<iterator, iterator> equal_range(const key_type& key) {
|
||||||
return std::equal_range(begin(), end(), key, compare_);
|
const base_type& comp = *this;
|
||||||
|
return std::equal_range(begin(), end(), key, comp);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const {
|
std::pair<const_iterator, const_iterator> equal_range(const key_type& key) const {
|
||||||
return std::equal_range(begin(), end(), key, compare_);
|
const base_type& comp = *this;
|
||||||
|
return std::equal_range(begin(), end(), key, comp);
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator lower_bound(const key_type& key) {
|
iterator lower_bound(const key_type& key) {
|
||||||
return std::lower_bound(begin(), end(), key, compare_);
|
const base_type& comp = *this;
|
||||||
|
return std::lower_bound(begin(), end(), key, comp);
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iterator lower_bound(const key_type& key) const {
|
const_iterator lower_bound(const key_type& key) const {
|
||||||
return std::lower_bound(begin(), end(), key, compare_);
|
const base_type& comp = *this;
|
||||||
|
return std::lower_bound(begin(), end(), key, comp);
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator upper_bound(const key_type& key) {
|
iterator upper_bound(const key_type& key) {
|
||||||
return std::upper_bound(begin(), end(), key, compare_);
|
const base_type& comp = *this;
|
||||||
|
return std::upper_bound(begin(), end(), key, comp);
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iterator upper_bound(const key_type& key) const {
|
const_iterator upper_bound(const key_type& key) const {
|
||||||
return std::upper_bound(begin(), end(), key, compare_);
|
const base_type& comp = *this;
|
||||||
|
return std::upper_bound(begin(), end(), key, comp);
|
||||||
}
|
}
|
||||||
|
|
||||||
key_compare key_comp() const {
|
key_compare key_comp() const {
|
||||||
return compare_;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
value_compare value_comp() const {
|
value_compare value_comp() const {
|
||||||
return value_compare(compare_);
|
return value_compare(key_comp());
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
container_type data_;
|
container_type data_;
|
||||||
uber_comparer_type compare_;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,6 +70,16 @@ namespace
|
|||||||
return !(l == r);
|
return !(l == r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template < typename T >
|
||||||
|
class dummy_less {
|
||||||
|
public:
|
||||||
|
dummy_less(int i) : i(i) {}
|
||||||
|
bool operator()(const T& l, const T& r) const {
|
||||||
|
return l < r;
|
||||||
|
}
|
||||||
|
int i = 0;
|
||||||
|
};
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
constexpr std::add_const_t<T>& my_as_const(T& t) noexcept {
|
constexpr std::add_const_t<T>& my_as_const(T& t) noexcept {
|
||||||
return t;
|
return t;
|
||||||
@@ -77,6 +87,14 @@ namespace
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("flat_map") {
|
TEST_CASE("flat_map") {
|
||||||
|
SECTION("sizeof") {
|
||||||
|
REQUIRE(sizeof(flat_map<int, unsigned>) == sizeof(std::vector<std::pair<int, unsigned>>));
|
||||||
|
|
||||||
|
struct vc : flat_map<int, unsigned>::value_compare {
|
||||||
|
int i;
|
||||||
|
};
|
||||||
|
REQUIRE(sizeof(vc) == sizeof(int));
|
||||||
|
}
|
||||||
SECTION("types") {
|
SECTION("types") {
|
||||||
using map_t = flat_map<int, unsigned>;
|
using map_t = flat_map<int, unsigned>;
|
||||||
|
|
||||||
@@ -416,6 +434,16 @@ TEST_CASE("flat_map") {
|
|||||||
REQUIRE(my_as_const(s0).key_comp().i == 42);
|
REQUIRE(my_as_const(s0).key_comp().i == 42);
|
||||||
REQUIRE(my_as_const(s0).value_comp()({2,50},{4,20}));
|
REQUIRE(my_as_const(s0).value_comp()({2,50},{4,20}));
|
||||||
}
|
}
|
||||||
|
SECTION("custom_less") {
|
||||||
|
using map_t = flat_map<int, unsigned, dummy_less<int>>;
|
||||||
|
auto s0 = map_t(dummy_less<int>(42));
|
||||||
|
auto s1 = map_t(dummy_less<int>(21));
|
||||||
|
REQUIRE(s0.key_comp().i == 42);
|
||||||
|
REQUIRE(s1.key_comp().i == 21);
|
||||||
|
s0.swap(s1);
|
||||||
|
REQUIRE(s0.key_comp().i == 21);
|
||||||
|
REQUIRE(s1.key_comp().i == 42);
|
||||||
|
}
|
||||||
SECTION("operators") {
|
SECTION("operators") {
|
||||||
using map_t = flat_map<int, unsigned>;
|
using map_t = flat_map<int, unsigned>;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user