map/fixed: add custom comparator support

This commit is contained in:
Danny Robson 2019-04-15 13:34:26 +10:00
parent 6fba251a38
commit 1cd107d27e

View File

@ -13,13 +13,19 @@
#include <array>
#include <utility>
#include <stdexcept>
#include <functional>
#include <cstddef>
namespace cruft::map {
/// A flat map structure with a static capacity store and dynamic size.
template <std::size_t SizeV, typename KeyT, typename ValueT>
template <
std::size_t SizeV,
typename KeyT,
typename ValueT,
typename ComparatorT = std::equal_to<>
>
class fixed {
public:
static constexpr auto elements = SizeV;
@ -64,8 +70,9 @@ namespace cruft::map {
///////////////////////////////////////////////////////////////////////
mapped_type& at (KeyT const &key) &
{
ComparatorT cmp {};
for (auto &i: *this)
if (i.first == key)
if (cmp (i.first, key))
return i.second;
throw std::out_of_range ("Element out of range");
@ -75,8 +82,9 @@ namespace cruft::map {
//---------------------------------------------------------------------
mapped_type const& at (KeyT const &key) const&
{
ComparatorT cmp {};
for (auto &i: *this)
if (i.first == key)
if (cmp (i.first, key))
return i.second;
throw std::out_of_range ("Element out of range");
@ -86,8 +94,9 @@ namespace cruft::map {
//---------------------------------------------------------------------
std::pair<iterator,bool> insert (value_type const &keyval)
{
ComparatorT cmp {};
for (auto &i: *this) {
if (i.first == keyval.first) {
if (cmp (i.first, keyval.first)) {
return { &i, false };
}
}
@ -106,8 +115,9 @@ namespace cruft::map {
//---------------------------------------------------------------------
mapped_type& operator[] (KeyT const &key)
{
ComparatorT cmp {};
for (auto &i: *this)
if (i.first == key)
if (cmp (i.first, key))
return i.second;
if (m_size >= capacity ())