2019-03-28 14:27:34 +11:00
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* Copyright 2019 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-05-17 12:26:08 +10:00
|
|
|
#include "../debug/assert.hpp"
|
2019-03-28 14:27:34 +11:00
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <utility>
|
|
|
|
#include <stdexcept>
|
2019-04-15 13:34:26 +10:00
|
|
|
#include <functional>
|
2019-03-28 14:27:34 +11:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
|
|
|
|
namespace cruft::map {
|
|
|
|
/// A flat map structure with a static capacity store and dynamic size.
|
2019-04-15 13:34:26 +10:00
|
|
|
template <
|
|
|
|
std::size_t SizeV,
|
|
|
|
typename KeyT,
|
|
|
|
typename ValueT,
|
|
|
|
typename ComparatorT = std::equal_to<>
|
|
|
|
>
|
2019-03-28 14:27:34 +11:00
|
|
|
class fixed {
|
|
|
|
public:
|
|
|
|
static constexpr auto elements = SizeV;
|
|
|
|
|
|
|
|
using key_type = KeyT;
|
|
|
|
using mapped_type = ValueT;
|
|
|
|
using value_type = std::pair<KeyT,ValueT>;
|
|
|
|
|
|
|
|
using iterator = value_type*;
|
2019-06-04 17:42:59 +10:00
|
|
|
using const_iterator = value_type const*;
|
2019-03-28 14:27:34 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
fixed () = default;
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
fixed (std::initializer_list<value_type> keyvals)
|
|
|
|
{
|
|
|
|
if (keyvals.size () > capacity ())
|
|
|
|
throw std::bad_alloc ();
|
|
|
|
|
|
|
|
for (auto const &kv: keyvals) {
|
|
|
|
auto const &[pos,success] = insert (kv);
|
|
|
|
CHECK (success);
|
|
|
|
(void)pos;
|
|
|
|
(void)success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fixed (fixed&&) noexcept;
|
|
|
|
fixed& operator= (fixed&&) noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
fixed (fixed const&);
|
|
|
|
fixed& operator= (fixed const&);
|
|
|
|
|
|
|
|
|
|
|
|
~fixed () { clear (); }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2019-06-04 17:42:59 +10:00
|
|
|
iterator find (KeyT const &key)&
|
2019-03-28 14:27:34 +11:00
|
|
|
{
|
2019-04-15 13:34:26 +10:00
|
|
|
ComparatorT cmp {};
|
2019-06-04 17:42:59 +10:00
|
|
|
for (auto cursor = begin (), last = end (); cursor != last; ++cursor)
|
|
|
|
if (cmp (cursor->first, key))
|
|
|
|
return cursor;
|
|
|
|
return end ();
|
|
|
|
}
|
2019-03-28 14:27:34 +11:00
|
|
|
|
2019-06-04 17:42:59 +10:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
const_iterator find (KeyT const &key) const&
|
|
|
|
{
|
|
|
|
ComparatorT cmp {};
|
|
|
|
for (auto cursor = begin (), last = end (); cursor != last; ++cursor)
|
|
|
|
if (cmp (cursor->first, key))
|
|
|
|
return cursor;
|
|
|
|
return end ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
mapped_type& at (KeyT const &key) &
|
|
|
|
{
|
|
|
|
if (auto pos = find (key); pos != end ())
|
|
|
|
return pos->second;
|
2019-03-28 14:27:34 +11:00
|
|
|
throw std::out_of_range ("Element out of range");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
mapped_type const& at (KeyT const &key) const&
|
|
|
|
{
|
2019-06-04 17:42:59 +10:00
|
|
|
if (auto pos = find (key); pos != end ())
|
|
|
|
return pos->second;
|
2019-03-28 14:27:34 +11:00
|
|
|
throw std::out_of_range ("Element out of range");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
std::pair<iterator,bool> insert (value_type const &keyval)
|
|
|
|
{
|
2019-06-04 17:42:59 +10:00
|
|
|
if (auto pos = find (keyval.first); pos != end ())
|
|
|
|
return { pos, false };
|
2019-03-28 14:27:34 +11:00
|
|
|
|
|
|
|
if (m_size >= capacity ())
|
|
|
|
throw std::bad_alloc ();
|
|
|
|
|
|
|
|
auto ptr = m_store.data + m_size;
|
|
|
|
new (ptr) value_type (keyval);
|
|
|
|
++m_size;
|
|
|
|
|
|
|
|
return { ptr, true };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-17 10:50:09 +10:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
mapped_type const& operator[] (KeyT const &key) const
|
|
|
|
{
|
2019-06-04 17:42:59 +10:00
|
|
|
return at (key);
|
2019-05-17 10:50:09 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-28 14:27:34 +11:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
mapped_type& operator[] (KeyT const &key)
|
|
|
|
{
|
2019-06-04 17:42:59 +10:00
|
|
|
// If it's present, then return a reference to the value
|
|
|
|
if (auto pos = find (key); pos != end ())
|
|
|
|
return pos->second;
|
2019-03-28 14:27:34 +11:00
|
|
|
|
2019-06-04 17:42:59 +10:00
|
|
|
// Make space for a new entry, initialise it, and return that.
|
2019-03-28 14:27:34 +11:00
|
|
|
if (m_size >= capacity ())
|
|
|
|
throw std::bad_alloc ();
|
|
|
|
|
|
|
|
auto ptr = m_store.data + m_size;
|
|
|
|
new (ptr) value_type ({ key, {} });
|
|
|
|
++m_size;
|
|
|
|
|
|
|
|
return ptr->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
void clear (void)
|
|
|
|
{
|
|
|
|
for (auto i: *this)
|
|
|
|
i.~value_type ();
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2019-06-04 17:42:59 +10:00
|
|
|
iterator begin (void)& { return std::begin (m_store.data); }
|
|
|
|
iterator end (void)& { return begin () + m_size; }
|
2019-03-28 14:27:34 +11:00
|
|
|
|
2019-06-04 17:42:59 +10:00
|
|
|
const_iterator begin (void) const& { return std::begin (m_store.data); }
|
|
|
|
const_iterator end (void) const& { return begin () + m_size; }
|
2019-03-28 14:27:34 +11:00
|
|
|
|
2019-06-04 17:42:59 +10:00
|
|
|
const_iterator cbegin (void) const& { return begin (); }
|
|
|
|
const_iterator cend (void) const& { return end (); }
|
2019-03-28 14:27:34 +11:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
auto size (void) const { return m_size; }
|
|
|
|
static auto capacity (void) { return elements; }
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::size_t m_size = 0;
|
|
|
|
|
|
|
|
union storage {
|
|
|
|
storage () {};
|
|
|
|
~storage () {};
|
|
|
|
|
|
|
|
char defer;
|
|
|
|
value_type data[elements];
|
|
|
|
} m_store;
|
|
|
|
};
|
|
|
|
}
|