Files
Seele/src/Engine/Containers/Map.h
T

129 lines
3.6 KiB
C++
Raw Normal View History

#pragma once
2021-03-31 12:18:16 +02:00
#include <utility>
2021-11-11 20:12:50 +01:00
#include "Array.h"
2022-11-29 16:34:42 +01:00
#include "Pair.h"
2023-12-22 19:46:07 +01:00
#include "Tree.h"
2023-07-13 21:01:20 +02:00
#include "Serialization/Serialization.h"
namespace Seele
{
2023-12-22 19:46:07 +01:00
template<typename KeyType, typename PairType>
struct _KeyFun
{
const KeyType& operator()(const PairType& pair) const
{
return pair.key;
}
};
2021-11-01 20:25:16 +01:00
template <typename K,
typename V,
typename Compare = std::less<K>,
2023-12-22 19:46:07 +01:00
typename Allocator = std::pmr::polymorphic_allocator<Pair<const K, V>>>
struct Map : public Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocator>
2020-04-12 15:47:19 +02:00
{
2023-12-22 19:46:07 +01:00
using Super = Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocator>;
using key_type = Super::key_type;
2021-11-01 20:25:16 +01:00
using mapped_type = V;
2023-12-22 19:46:07 +01:00
using value_type = Super::value_type;
using allocator_type = Super::allocator_type;
using size_type = Super::size_type;
using difference_type = Super::difference_type;
using reference = Super::reference;
using const_reference = Super::reference;
using pointer = Super::pointer;
using const_pointer = Super::const_pointer;
using iterator = Super::iterator;
using const_iterator = Super::const_iterator;
using reverse_iterator = Super::reverse_iterator;
using const_reverse_iterator = Super::const_reverse_iterator;
2021-11-01 20:25:16 +01:00
2022-04-15 11:19:30 +02:00
constexpr Map() noexcept
2023-12-22 19:46:07 +01:00
: Super()
2021-11-01 20:25:16 +01:00
{
}
2022-04-15 11:19:30 +02:00
constexpr explicit Map(const Compare& comp, const Allocator& alloc = Allocator()) noexcept(noexcept(Allocator()))
2023-12-22 19:46:07 +01:00
: Super(comp, alloc)
2021-11-01 20:25:16 +01:00
{
}
2023-10-29 09:20:23 +01:00
constexpr explicit Map(const Allocator& alloc) noexcept(noexcept(Compare()))
2023-12-22 19:46:07 +01:00
: Super(alloc)
2021-11-01 20:25:16 +01:00
{
}
2022-04-15 11:19:30 +02:00
constexpr mapped_type& operator[](const key_type& key)
2021-11-01 20:25:16 +01:00
{
2023-12-22 19:46:07 +01:00
auto [it, inserted] = Super::insert(Pair<K, V>(key, V()));
return it->value;
2021-11-01 20:25:16 +01:00
}
2023-11-05 10:36:01 +01:00
constexpr const mapped_type& operator[](const key_type& key) const
{
2023-12-22 19:46:07 +01:00
return Super::find(key)->value;
2023-11-05 10:36:01 +01:00
}
2023-02-13 14:56:13 +01:00
constexpr mapped_type& at(const key_type& key)
{
2023-12-22 19:46:07 +01:00
iterator elem = Super::find(key);
if (elem == Super::end())
2023-02-13 14:56:13 +01:00
{
throw std::logic_error("Key not found");
}
2023-12-22 19:46:07 +01:00
return elem->value;
2023-02-13 14:56:13 +01:00
}
2023-02-24 22:09:07 +01:00
constexpr const mapped_type& at(const key_type& key) const
{
2023-12-22 19:46:07 +01:00
return Super::find(key)->value;
2023-02-24 22:09:07 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr iterator find(const key_type& key)
2021-11-01 20:25:16 +01:00
{
2023-12-22 19:46:07 +01:00
return Super::find(key);
2021-11-01 20:25:16 +01:00
}
2022-04-15 11:19:30 +02:00
constexpr iterator erase(const key_type& key)
2021-11-01 20:25:16 +01:00
{
2023-12-22 19:46:07 +01:00
return Super::remove(key);
2021-11-01 20:25:16 +01:00
}
2024-04-26 11:42:28 +02:00
constexpr bool exists(const key_type& key) const
{
return Super::find(key) != Super::end();
}
2022-04-15 11:19:30 +02:00
constexpr bool exists(const key_type& key)
2021-11-01 20:25:16 +01:00
{
2023-12-22 19:46:07 +01:00
return Super::find(key) != Super::end();
2022-04-15 11:19:30 +02:00
}
2024-04-26 11:42:28 +02:00
constexpr bool contains(const key_type& key) const
{
return exists(key);
}
2023-02-13 14:56:13 +01:00
constexpr bool contains(const key_type& key)
{
return exists(key);
}
2023-12-22 19:46:07 +01:00
constexpr Pair<iterator, bool> insert(const value_type& val)
2023-02-13 14:56:13 +01:00
{
2023-12-22 19:46:07 +01:00
return Super::insert(val);
2021-11-01 20:25:16 +01:00
}
2023-07-13 21:01:20 +02:00
void save(ArchiveBuffer& buffer) const
{
2023-12-22 19:46:07 +01:00
size_t s = Super::size();
buffer.writeBytes(&s, sizeof(uint64));
2023-07-13 21:01:20 +02:00
for(const auto& [k, v] : *this)
{
Serialization::save(buffer, k);
Serialization::save(buffer, v);
}
}
void load(ArchiveBuffer& buffer)
{
uint64 len = 0;
buffer.readBytes(&len, sizeof(uint64));
for(uint64 i = 0; i < len; ++i)
{
2023-11-05 10:36:01 +01:00
K k = K();
V v = V();
2023-07-13 21:01:20 +02:00
Serialization::load(buffer, k);
Serialization::load(buffer, v);
2023-11-05 10:36:01 +01:00
this->operator[](std::move(k)) = std::move(v);
2023-07-13 21:01:20 +02:00
}
}
2020-04-12 15:47:19 +02:00
};
2023-10-29 09:20:23 +01:00
} // namespace Seele