Allow using custom comparators in flat_[map|set].

This commit is contained in:
John Preston 2017-10-05 16:35:31 +01:00
parent ecbc0ae57e
commit fdd89d65ca
3 changed files with 258 additions and 127 deletions

View File

@ -26,15 +26,22 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
namespace base { namespace base {
template <typename Key, typename Type> template <
typename Key,
typename Type,
typename Compare = std::less<>>
class flat_map; class flat_map;
template <typename Key, typename Type> template <
typename Key,
typename Type,
typename Compare = std::less<>>
class flat_multi_map; class flat_multi_map;
template < template <
typename Key, typename Key,
typename Type, typename Type,
typename Compare,
typename iterator_impl, typename iterator_impl,
typename pointer_impl, typename pointer_impl,
typename reference_impl> typename reference_impl>
@ -43,6 +50,7 @@ class flat_multi_map_iterator_base_impl;
template < template <
typename Key, typename Key,
typename Type, typename Type,
typename Compare,
typename iterator_impl, typename iterator_impl,
typename pointer_impl, typename pointer_impl,
typename reference_impl> typename reference_impl>
@ -50,12 +58,12 @@ class flat_multi_map_iterator_base_impl {
public: public:
using iterator_category = typename iterator_impl::iterator_category; using iterator_category = typename iterator_impl::iterator_category;
using value_type = typename flat_multi_map<Key, Type>::value_type; using value_type = typename flat_multi_map<Key, Type, Compare>::value_type;
using difference_type = typename iterator_impl::difference_type; using difference_type = typename iterator_impl::difference_type;
using pointer = pointer_impl; using pointer = pointer_impl;
using const_pointer = typename flat_multi_map<Key, Type>::const_pointer; using const_pointer = typename flat_multi_map<Key, Type, Compare>::const_pointer;
using reference = reference_impl; using reference = reference_impl;
using const_reference = typename flat_multi_map<Key, Type>::const_reference; using const_reference = typename flat_multi_map<Key, Type, Compare>::const_reference;
flat_multi_map_iterator_base_impl(iterator_impl impl = iterator_impl()) flat_multi_map_iterator_base_impl(iterator_impl impl = iterator_impl())
: _impl(impl) { : _impl(impl) {
@ -123,64 +131,129 @@ public:
private: private:
iterator_impl _impl; iterator_impl _impl;
friend class flat_multi_map<Key, Type>; friend class flat_multi_map<Key, Type, Compare>;
}; };
template <typename Key, typename Type> template <typename Key, typename Type, typename Compare>
class flat_multi_map { class flat_multi_map {
using self = flat_multi_map<Key, Type>;
class key_const_wrap { class key_const_wrap {
public: public:
key_const_wrap(const Key &value) : _value(value) { constexpr key_const_wrap(const Key &value) : _value(value) {
} }
key_const_wrap(Key &&value) : _value(std::move(value)) { constexpr key_const_wrap(Key &&value) : _value(std::move(value)) {
} }
inline operator const Key&() const { inline constexpr operator const Key&() const {
return _value; return _value;
} }
friend inline bool operator<(const Key &a, const key_const_wrap &b) {
return a < ((const Key&)b);
}
friend inline bool operator<(const key_const_wrap &a, const Key &b) {
return ((const Key&)a) < b;
}
friend inline bool operator<(
const key_const_wrap &a,
const key_const_wrap &b) {
return ((const Key&)a) < ((const Key&)b);
}
private: private:
Key _value; Key _value;
}; };
using pair_type = std::pair<key_const_wrap, Type>; using pair_type = std::pair<key_const_wrap, Type>;
class compare {
public:
template <
typename OtherType1,
typename OtherType2,
typename = std::enable_if_t<
!std::is_same_v<std::decay_t<OtherType1>, key_const_wrap> &&
!std::is_same_v<std::decay_t<OtherType1>, pair_type> &&
!std::is_same_v<std::decay_t<OtherType2>, key_const_wrap> &&
!std::is_same_v<std::decay_t<OtherType2>, pair_type>>>
inline constexpr auto operator()(
OtherType1 &&a,
OtherType2 &b) const {
return Compare()(
std::forward<OtherType1>(a),
std::forward<OtherType2>(b));
}
inline constexpr auto operator()(
const key_const_wrap &a,
const key_const_wrap &b) const {
return operator()(
static_cast<const Key&>(a),
static_cast<const Key&>(b));
}
template <
typename OtherType,
typename = std::enable_if_t<
!std::is_same_v<std::decay_t<OtherType>, key_const_wrap> &&
!std::is_same_v<std::decay_t<OtherType>, pair_type>>>
inline constexpr auto operator()(
const key_const_wrap &a,
OtherType &&b) const {
return operator()(
static_cast<const Key&>(a),
std::forward<OtherType>(b));
}
template <
typename OtherType,
typename = std::enable_if_t<
!std::is_same_v<std::decay_t<OtherType>, key_const_wrap> &&
!std::is_same_v<std::decay_t<OtherType>, pair_type>>>
inline constexpr auto operator()(
OtherType &&a,
const key_const_wrap &b) const {
return operator()(
std::forward<OtherType>(a),
static_cast<const Key&>(b));
}
inline constexpr auto operator()(
const pair_type &a,
const pair_type &b) const {
return operator()(a.first, b.first);
}
template <
typename OtherType,
typename = std::enable_if_t<
!std::is_same_v<std::decay_t<OtherType>, pair_type>>>
inline constexpr auto operator()(
const pair_type &a,
OtherType &&b) const {
return operator()(a.first, std::forward<OtherType>(b));
}
template <
typename OtherType,
typename = std::enable_if_t<
!std::is_same_v<std::decay_t<OtherType>, pair_type>>>
inline constexpr auto operator()(
OtherType &&a,
const pair_type &b) const {
return operator()(std::forward<OtherType>(a), b.first);
}
};
using impl = std::deque<pair_type>; using impl = std::deque<pair_type>;
using iterator_base = flat_multi_map_iterator_base_impl< using iterator_base = flat_multi_map_iterator_base_impl<
Key, Key,
Type, Type,
Compare,
typename impl::iterator, typename impl::iterator,
pair_type*, pair_type*,
pair_type&>; pair_type&>;
using const_iterator_base = flat_multi_map_iterator_base_impl< using const_iterator_base = flat_multi_map_iterator_base_impl<
Key, Key,
Type, Type,
Compare,
typename impl::const_iterator, typename impl::const_iterator,
const pair_type*, const pair_type*,
const pair_type&>; const pair_type&>;
using reverse_iterator_base = flat_multi_map_iterator_base_impl< using reverse_iterator_base = flat_multi_map_iterator_base_impl<
Key, Key,
Type, Type,
Compare,
typename impl::reverse_iterator, typename impl::reverse_iterator,
pair_type*, pair_type*,
pair_type&>; pair_type&>;
using const_reverse_iterator_base = flat_multi_map_iterator_base_impl< using const_reverse_iterator_base = flat_multi_map_iterator_base_impl<
Key, Key,
Type, Type,
Compare,
typename impl::const_reverse_iterator, typename impl::const_reverse_iterator,
const pair_type*, const pair_type*,
const pair_type&>; const pair_type&>;
@ -292,10 +365,10 @@ public:
} }
iterator insert(const value_type &value) { iterator insert(const value_type &value) {
if (empty() || (value.first < front().first)) { if (empty() || compare()(value.first, front().first)) {
_impl.push_front(value); _impl.push_front(value);
return begin(); return begin();
} else if (!(value.first < back().first)) { } else if (!compare()(value.first, back().first)) {
_impl.push_back(value); _impl.push_back(value);
return (end() - 1); return (end() - 1);
} }
@ -303,10 +376,10 @@ public:
return _impl.insert(where, value); return _impl.insert(where, value);
} }
iterator insert(value_type &&value) { iterator insert(value_type &&value) {
if (empty() || (value.first < front().first)) { if (empty() || compare()(value.first, front().first)) {
_impl.push_front(std::move(value)); _impl.push_front(std::move(value));
return begin(); return begin();
} else if (!(value.first < back().first)) { } else if (!compare()(value.first, back().first)) {
_impl.push_back(std::move(value)); _impl.push_back(std::move(value));
return (end() - 1); return (end() - 1);
} }
@ -319,18 +392,22 @@ public:
} }
bool removeOne(const Key &key) { bool removeOne(const Key &key) {
if (empty() || (key < front().first) || (back().first < key)) { if (empty()
|| compare()(key, front().first)
|| compare()(back().first, key)) {
return false; return false;
} }
auto where = getLowerBound(key); auto where = getLowerBound(key);
if (key < where->first) { if (compare()(key, where->first)) {
return false; return false;
} }
_impl.erase(where); _impl.erase(where);
return true; return true;
} }
int removeAll(const Key &key) { int removeAll(const Key &key) {
if (empty() || (key < front().first) || (back().first < key)) { if (empty()
|| compare()(key, front().first)
|| compare()(back().first, key)) {
return 0; return 0;
} }
auto range = getEqualRange(key); auto range = getEqualRange(key);
@ -349,26 +426,32 @@ public:
} }
iterator findFirst(const Key &key) { iterator findFirst(const Key &key) {
if (empty() || (key < front().first) || (back().first < key)) { if (empty()
|| compare()(key, front().first)
|| compare()(back().first, key)) {
return end(); return end();
} }
auto where = getLowerBound(key); auto where = getLowerBound(key);
return (key < where->first) ? _impl.end() : where; return compare()(key, where->first) ? _impl.end() : where;
} }
const_iterator findFirst(const Key &key) const { const_iterator findFirst(const Key &key) const {
if (empty() || (key < front().first) || (back().first < key)) { if (empty()
|| compare()(key, front().first)
|| compare()(back().first, key)) {
return end(); return end();
} }
auto where = getLowerBound(key); auto where = getLowerBound(key);
return (key < where->first) ? _impl.end() : where; return compare()(key, where->first) ? _impl.end() : where;
} }
bool contains(const Key &key) const { bool contains(const Key &key) const {
return findFirst(key) != end(); return findFirst(key) != end();
} }
int count(const Key &key) const { int count(const Key &key) const {
if (empty() || (key < front().first) || (back().first < key)) { if (empty()
|| compare()(key, front().first)
|| compare()(back().first, key)) {
return 0; return 0;
} }
auto range = getEqualRange(key); auto range = getEqualRange(key);
@ -377,46 +460,39 @@ public:
private: private:
impl _impl; impl _impl;
friend class flat_map<Key, Type>; friend class flat_map<Key, Type, Compare>;
struct Comparator {
inline bool operator()(const pair_type &a, const Key &b) {
return a.first < b;
}
inline bool operator()(const Key &a, const pair_type &b) {
return a < b.first;
}
};
typename impl::iterator getLowerBound(const Key &key) { typename impl::iterator getLowerBound(const Key &key) {
return base::lower_bound(_impl, key, Comparator()); return base::lower_bound(_impl, key, compare());
} }
typename impl::const_iterator getLowerBound(const Key &key) const { typename impl::const_iterator getLowerBound(const Key &key) const {
return base::lower_bound(_impl, key, Comparator()); return base::lower_bound(_impl, key, compare());
} }
typename impl::iterator getUpperBound(const Key &key) { typename impl::iterator getUpperBound(const Key &key) {
return base::upper_bound(_impl, key, Comparator()); return base::upper_bound(_impl, key, compare());
} }
typename impl::const_iterator getUpperBound(const Key &key) const { typename impl::const_iterator getUpperBound(const Key &key) const {
return base::upper_bound(_impl, key, Comparator()); return base::upper_bound(_impl, key, compare());
} }
std::pair< std::pair<
typename impl::iterator, typename impl::iterator,
typename impl::iterator typename impl::iterator
> getEqualRange(const Key &key) { > getEqualRange(const Key &key) {
return base::equal_range(_impl, key, Comparator()); return base::equal_range(_impl, key, compare());
} }
std::pair< std::pair<
typename impl::const_iterator, typename impl::const_iterator,
typename impl::const_iterator typename impl::const_iterator
> getEqualRange(const Key &key) const { > getEqualRange(const Key &key) const {
return base::equal_range(_impl, key, Comparator()); return base::equal_range(_impl, key, compare());
} }
}; };
template <typename Key, typename Type> template <typename Key, typename Type, typename Compare>
class flat_map : private flat_multi_map<Key, Type> { class flat_map : private flat_multi_map<Key, Type, Compare> {
using parent = flat_multi_map<Key, Type>; using parent = flat_multi_map<Key, Type, Compare>;
using compare = typename parent::compare;
using pair_type = typename parent::pair_type; using pair_type = typename parent::pair_type;
public: public:
@ -450,29 +526,29 @@ public:
using parent::contains; using parent::contains;
iterator insert(const value_type &value) { iterator insert(const value_type &value) {
if (this->empty() || (value.first < this->front().first)) { if (this->empty() || compare()(value.first, this->front().first)) {
this->_impl.push_front(value); this->_impl.push_front(value);
return this->begin(); return this->begin();
} else if (this->back().first < value.first) { } else if (compare()(this->back().first, value.first)) {
this->_impl.push_back(value); this->_impl.push_back(value);
return (this->end() - 1); return (this->end() - 1);
} }
auto where = this->getLowerBound(value.first); auto where = this->getLowerBound(value.first);
if (value.first < where->first) { if (compare()(value.first, where->first)) {
return this->_impl.insert(where, value); return this->_impl.insert(where, value);
} }
return this->end(); return this->end();
} }
iterator insert(value_type &&value) { iterator insert(value_type &&value) {
if (this->empty() || (value.first < this->front().first)) { if (this->empty() || compare()(value.first, this->front().first)) {
this->_impl.push_front(std::move(value)); this->_impl.push_front(std::move(value));
return this->begin(); return this->begin();
} else if (this->back().first < value.first) { } else if (compare()(this->back().first, value.first)) {
this->_impl.push_back(std::move(value)); this->_impl.push_back(std::move(value));
return (this->end() - 1); return (this->end() - 1);
} }
auto where = this->getLowerBound(value.first); auto where = this->getLowerBound(value.first);
if (value.first < where->first) { if (compare()(value.first, where->first)) {
return this->_impl.insert(where, std::move(value)); return this->_impl.insert(where, std::move(value));
} }
return this->end(); return this->end();
@ -494,15 +570,15 @@ public:
} }
Type &operator[](const Key &key) { Type &operator[](const Key &key) {
if (this->empty() || (key < this->front().first)) { if (this->empty() || compare()(key, this->front().first)) {
this->_impl.push_front({ key, Type() }); this->_impl.push_front({ key, Type() });
return this->front().second; return this->front().second;
} else if (this->back().first < key) { } else if (compare()(this->back().first, key)) {
this->_impl.push_back({ key, Type() }); this->_impl.push_back({ key, Type() });
return this->back().second; return this->back().second;
} }
auto where = this->getLowerBound(key); auto where = this->getLowerBound(key);
if (key < where->first) { if (compare()(key, where->first)) {
return this->_impl.insert(where, { key, Type() })->second; return this->_impl.insert(where, { key, Type() })->second;
} }
return where->second; return where->second;

View File

@ -25,24 +25,24 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
namespace base { namespace base {
template <typename Type> template <typename Type, typename Compare = std::less<>>
class flat_set; class flat_set;
template <typename Type> template <typename Type, typename Compare = std::less<>>
class flat_multi_set; class flat_multi_set;
template <typename Type, typename iterator_impl> template <typename Type, typename Compare, typename iterator_impl>
class flat_multi_set_iterator_base_impl; class flat_multi_set_iterator_base_impl;
template <typename Type, typename iterator_impl> template <typename Type, typename Compare, typename iterator_impl>
class flat_multi_set_iterator_base_impl { class flat_multi_set_iterator_base_impl {
public: public:
using iterator_category = typename iterator_impl::iterator_category; using iterator_category = typename iterator_impl::iterator_category;
using value_type = typename flat_multi_set<Type>::value_type; using value_type = typename flat_multi_set<Type, Compare>::value_type;
using difference_type = typename iterator_impl::difference_type; using difference_type = typename iterator_impl::difference_type;
using pointer = typename flat_multi_set<Type>::pointer; using pointer = typename flat_multi_set<Type, Compare>::pointer;
using reference = typename flat_multi_set<Type>::reference; using reference = typename flat_multi_set<Type, Compare>::reference;
flat_multi_set_iterator_base_impl(iterator_impl impl = iterator_impl()) flat_multi_set_iterator_base_impl(iterator_impl impl = iterator_impl())
: _impl(impl) { : _impl(impl) {
@ -101,8 +101,8 @@ public:
private: private:
iterator_impl _impl; iterator_impl _impl;
friend class flat_multi_set<Type>; friend class flat_multi_set<Type, Compare>;
friend class flat_set<Type>; friend class flat_set<Type, Compare>;
Type &wrapped() { Type &wrapped() {
return _impl->wrapped(); return _impl->wrapped();
@ -110,50 +110,92 @@ private:
}; };
template <typename Type> template <typename Type, typename Compare>
class flat_multi_set { class flat_multi_set {
using self = flat_multi_set<Type>;
class const_wrap { class const_wrap {
public: public:
const_wrap(const Type &value) : _value(value) { constexpr const_wrap(const Type &value)
: _value(value) {
} }
const_wrap(Type &&value) : _value(std::move(value)) { constexpr const_wrap(Type &&value)
: _value(std::move(value)) {
} }
inline operator const Type&() const { inline constexpr operator const Type&() const {
return _value; return _value;
} }
Type &wrapped() { constexpr Type &wrapped() {
return _value; return _value;
} }
friend inline bool operator<(const Type &a, const const_wrap &b) {
return a < ((const Type&)b);
}
friend inline bool operator<(const const_wrap &a, const Type &b) {
return ((const Type&)a) < b;
}
friend inline bool operator<(const const_wrap &a, const const_wrap &b) {
return ((const Type&)a) < ((const Type&)b);
}
private: private:
Type _value; Type _value;
}; };
class compare {
public:
template <
typename OtherType1,
typename OtherType2,
typename = std::enable_if_t<
!std::is_same_v<std::decay_t<OtherType1>, const_wrap> &&
!std::is_same_v<std::decay_t<OtherType2>, const_wrap>>>
inline constexpr auto operator()(
OtherType1 &&a,
OtherType2 &b) const {
return Compare()(
std::forward<OtherType1>(a),
std::forward<OtherType2>(b));
}
inline constexpr auto operator()(
const const_wrap &a,
const const_wrap &b) const {
return operator()(
static_cast<const Type&>(a),
static_cast<const Type&>(b));
}
template <
typename OtherType,
typename = std::enable_if_t<
!std::is_same_v<std::decay_t<OtherType>, const_wrap>>>
inline constexpr auto operator()(
const const_wrap &a,
OtherType &&b) const {
return operator()(
static_cast<const Type&>(a),
std::forward<OtherType>(b));
}
template <
typename OtherType,
typename = std::enable_if_t<
!std::is_same_v<std::decay_t<OtherType>, const_wrap>>>
inline constexpr auto operator()(
OtherType &&a,
const const_wrap &b) const {
return operator()(
std::forward<OtherType>(a),
static_cast<const Type&>(b));
}
};
using impl = std::deque<const_wrap>; using impl = std::deque<const_wrap>;
using iterator_base = flat_multi_set_iterator_base_impl< using iterator_base = flat_multi_set_iterator_base_impl<
Type, Type,
Compare,
typename impl::iterator>; typename impl::iterator>;
using const_iterator_base = flat_multi_set_iterator_base_impl< using const_iterator_base = flat_multi_set_iterator_base_impl<
Type, Type,
Compare,
typename impl::const_iterator>; typename impl::const_iterator>;
using reverse_iterator_base = flat_multi_set_iterator_base_impl< using reverse_iterator_base = flat_multi_set_iterator_base_impl<
Type, Type,
Compare,
typename impl::reverse_iterator>; typename impl::reverse_iterator>;
using const_reverse_iterator_base = flat_multi_set_iterator_base_impl< using const_reverse_iterator_base = flat_multi_set_iterator_base_impl<
Type, Type,
Compare,
typename impl::const_reverse_iterator>; typename impl::const_reverse_iterator>;
public: public:
@ -209,7 +251,7 @@ public:
typename Iterator, typename Iterator,
typename = typename std::iterator_traits<Iterator>::iterator_category> typename = typename std::iterator_traits<Iterator>::iterator_category>
flat_multi_set(Iterator first, Iterator last) : _impl(first, last) { flat_multi_set(Iterator first, Iterator last) : _impl(first, last) {
base::sort(_impl); base::sort(_impl, compare());
} }
flat_multi_set(std::initializer_list<Type> iter) flat_multi_set(std::initializer_list<Type> iter)
@ -271,10 +313,10 @@ public:
} }
iterator insert(const Type &value) { iterator insert(const Type &value) {
if (empty() || (value < front())) { if (empty() || compare()(value, front())) {
_impl.push_front(value); _impl.push_front(value);
return begin(); return begin();
} else if (!(value < back())) { } else if (!compare()(value, back())) {
_impl.push_back(value); _impl.push_back(value);
return (end() - 1); return (end() - 1);
} }
@ -282,10 +324,10 @@ public:
return _impl.insert(where, value); return _impl.insert(where, value);
} }
iterator insert(Type &&value) { iterator insert(Type &&value) {
if (empty() || (value < front())) { if (empty() || compare()(value, front())) {
_impl.push_front(std::move(value)); _impl.push_front(std::move(value));
return begin(); return begin();
} else if (!(value < back())) { } else if (!compare()(value, back())) {
_impl.push_back(std::move(value)); _impl.push_back(std::move(value));
return (end() - 1); return (end() - 1);
} }
@ -298,18 +340,22 @@ public:
} }
bool removeOne(const Type &value) { bool removeOne(const Type &value) {
if (empty() || (value < front()) || (back() < value)) { if (empty()
|| compare()(value, front())
|| compare()(back(), value)) {
return false; return false;
} }
auto where = getLowerBound(value); auto where = getLowerBound(value);
if (value < *where) { if (compare()(value, *where)) {
return false; return false;
} }
_impl.erase(where); _impl.erase(where);
return true; return true;
} }
int removeAll(const Type &value) { int removeAll(const Type &value) {
if (empty() || (value < front()) || (back() < value)) { if (empty()
|| compare()(value, front())
|| compare()(back(), value)) {
return 0; return 0;
} }
auto range = getEqualRange(value); auto range = getEqualRange(value);
@ -328,26 +374,32 @@ public:
} }
iterator findFirst(const Type &value) { iterator findFirst(const Type &value) {
if (empty() || (value < front()) || (back() < value)) { if (empty()
|| compare()(value, front())
|| compare()(back(), value)) {
return end(); return end();
} }
auto where = getLowerBound(value); auto where = getLowerBound(value);
return (value < *where) ? _impl.end() : where; return compare()(value, *where) ? _impl.end() : where;
} }
const_iterator findFirst(const Type &value) const { const_iterator findFirst(const Type &value) const {
if (empty() || (value < front()) || (back() < value)) { if (empty()
|| compare()(value, front())
|| compare()(back(), value)) {
return end(); return end();
} }
auto where = getLowerBound(value); auto where = getLowerBound(value);
return (value < *where) ? _impl.end() : where; return compare()(value, *where) ? _impl.end() : where;
} }
bool contains(const Type &value) const { bool contains(const Type &value) const {
return findFirst(value) != end(); return findFirst(value) != end();
} }
int count(const Type &value) const { int count(const Type &value) const {
if (empty() || (value < front()) || (back() < value)) { if (empty()
|| compare()(value, front())
|| compare()(back(), value)) {
return 0; return 0;
} }
auto range = getEqualRange(value); auto range = getEqualRange(value);
@ -358,7 +410,7 @@ public:
auto modify(iterator which, Action action) { auto modify(iterator which, Action action) {
auto result = action(which.wrapped()); auto result = action(which.wrapped());
for (auto i = which + 1, e = end(); i != e; ++i) { for (auto i = which + 1, e = end(); i != e; ++i) {
if (*i < *which) { if (compare()(*i, *which)) {
std::swap(i.wrapped(), which.wrapped()); std::swap(i.wrapped(), which.wrapped());
} else { } else {
break; break;
@ -366,7 +418,7 @@ public:
} }
for (auto i = which, b = begin(); i != b;) { for (auto i = which, b = begin(); i != b;) {
--i; --i;
if (*which < *i) { if (compare()(*which, *i)) {
std::swap(i.wrapped(), which.wrapped()); std::swap(i.wrapped(), which.wrapped());
} else { } else {
break; break;
@ -380,10 +432,10 @@ public:
typename = typename std::iterator_traits<Iterator>::iterator_category> typename = typename std::iterator_traits<Iterator>::iterator_category>
void merge(Iterator first, Iterator last) { void merge(Iterator first, Iterator last) {
_impl.insert(_impl.end(), first, last); _impl.insert(_impl.end(), first, last);
base::sort(_impl); base::sort(_impl, compare());
} }
void merge(const flat_multi_set<Type> &other) { void merge(const flat_multi_set<Type, Compare> &other) {
merge(other.begin(), other.end()); merge(other.begin(), other.end());
} }
@ -393,38 +445,39 @@ public:
private: private:
impl _impl; impl _impl;
friend class flat_set<Type>; friend class flat_set<Type, Compare>;
typename impl::iterator getLowerBound(const Type &value) { typename impl::iterator getLowerBound(const Type &value) {
return base::lower_bound(_impl, value); return base::lower_bound(_impl, value, compare());
} }
typename impl::const_iterator getLowerBound(const Type &value) const { typename impl::const_iterator getLowerBound(const Type &value) const {
return base::lower_bound(_impl, value); return base::lower_bound(_impl, value, compare());
} }
typename impl::iterator getUpperBound(const Type &value) { typename impl::iterator getUpperBound(const Type &value) {
return base::upper_bound(_impl, value); return base::upper_bound(_impl, value, compare());
} }
typename impl::const_iterator getUpperBound(const Type &value) const { typename impl::const_iterator getUpperBound(const Type &value) const {
return base::upper_bound(_impl, value); return base::upper_bound(_impl, value, compare());
} }
std::pair< std::pair<
typename impl::iterator, typename impl::iterator,
typename impl::iterator typename impl::iterator
> getEqualRange(const Type &value) { > getEqualRange(const Type &value) {
return base::equal_range(_impl, value); return base::equal_range(_impl, value, compare());
} }
std::pair< std::pair<
typename impl::const_iterator, typename impl::const_iterator,
typename impl::const_iterator typename impl::const_iterator
> getEqualRange(const Type &value) const { > getEqualRange(const Type &value) const {
return base::equal_range(_impl, value); return base::equal_range(_impl, value, compare());
} }
}; };
template <typename Type> template <typename Type, typename Compare>
class flat_set : private flat_multi_set<Type> { class flat_set : private flat_multi_set<Type, Compare> {
using parent = flat_multi_set<Type>; using parent = flat_multi_set<Type, Compare>;
using compare = typename parent::compare;
public: public:
using iterator = typename parent::iterator; using iterator = typename parent::iterator;
@ -469,29 +522,29 @@ public:
using parent::erase; using parent::erase;
iterator insert(const Type &value) { iterator insert(const Type &value) {
if (this->empty() || (value < this->front())) { if (this->empty() || compare()(value, this->front())) {
this->_impl.push_front(value); this->_impl.push_front(value);
return this->begin(); return this->begin();
} else if (this->back() < value) { } else if (compare()(this->back(), value)) {
this->_impl.push_back(value); this->_impl.push_back(value);
return (this->end() - 1); return (this->end() - 1);
} }
auto where = this->getLowerBound(value); auto where = this->getLowerBound(value);
if (value < *where) { if (compare()(value, *where)) {
return this->_impl.insert(where, value); return this->_impl.insert(where, value);
} }
return this->end(); return this->end();
} }
iterator insert(Type &&value) { iterator insert(Type &&value) {
if (this->empty() || (value < this->front())) { if (this->empty() || compare()(value, this->front())) {
this->_impl.push_front(std::move(value)); this->_impl.push_front(std::move(value));
return this->begin(); return this->begin();
} else if (this->back() < value) { } else if (compare()(this->back(), value)) {
this->_impl.push_back(std::move(value)); this->_impl.push_back(std::move(value));
return (this->end() - 1); return (this->end() - 1);
} }
auto where = this->getLowerBound(value); auto where = this->getLowerBound(value);
if (value < *where) { if (compare()(value, *where)) {
return this->_impl.insert(where, std::move(value)); return this->_impl.insert(where, std::move(value));
} }
return this->end(); return this->end();
@ -516,9 +569,9 @@ public:
void modify(iterator which, Action action) { void modify(iterator which, Action action) {
action(which.wrapped()); action(which.wrapped());
for (auto i = iterator(which + 1), e = end(); i != e; ++i) { for (auto i = iterator(which + 1), e = end(); i != e; ++i) {
if (*i < *which) { if (compare()(*i, *which)) {
std::swap(i.wrapped(), which.wrapped()); std::swap(i.wrapped(), which.wrapped());
} else if (!(*which < *i)) { } else if (!compare()(*which, *i)) {
erase(which); erase(which);
return; return;
} else{ } else{
@ -527,9 +580,9 @@ public:
} }
for (auto i = which, b = begin(); i != b;) { for (auto i = which, b = begin(); i != b;) {
--i; --i;
if (*which < *i) { if (compare()(*which, *i)) {
std::swap(i.wrapped(), which.wrapped()); std::swap(i.wrapped(), which.wrapped());
} else if (!(*i < *which)) { } else if (!compare()(*i, *which)) {
erase(which); erase(which);
return; return;
} else { } else {
@ -546,7 +599,7 @@ public:
finalize(); finalize();
} }
void merge(const flat_multi_set<Type> &other) { void merge(const flat_multi_set<Type, Compare> &other) {
merge(other.begin(), other.end()); merge(other.begin(), other.end());
} }
@ -560,7 +613,7 @@ private:
std::unique( std::unique(
this->_impl.begin(), this->_impl.begin(),
this->_impl.end(), this->_impl.end(),
[](auto &&a, auto &&b) { return !(a < b); }), [](auto &&a, auto &&b) { return !compare()(a, b); }),
this->_impl.end()); this->_impl.end());
} }

View File

@ -29,6 +29,8 @@ TEST_CASE("flat_sets should keep items sorted", "[flat_set]") {
v.insert(4); v.insert(4);
v.insert(2); v.insert(2);
REQUIRE(v.contains(4));
auto checkSorted = [&] { auto checkSorted = [&] {
auto prev = v.begin(); auto prev = v.begin();
REQUIRE(prev != v.end()); REQUIRE(prev != v.end());