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

78 lines
3.4 KiB
C++
Raw Normal View History

#pragma once
2021-11-11 20:12:50 +01:00
#include "Array.h"
2022-11-29 16:34:42 +01:00
#include "Pair.h"
2023-07-13 21:01:20 +02:00
#include "Serialization/Serialization.h"
2024-06-09 12:20:04 +02:00
#include "Tree.h"
#include <utility>
2023-07-13 21:01:20 +02:00
2024-06-09 12:20:04 +02:00
namespace Seele {
template <typename KeyType, typename PairType> struct _KeyFun {
const KeyType& operator()(const PairType& pair) const { return pair.key; }
2023-12-22 19:46:07 +01:00
};
2025-03-20 20:15:38 +01:00
template <typename K, typename V, typename Compare = std::less<K>, typename Allocator = std::pmr::polymorphic_allocator<Pair<K, V>>>
2024-06-09 12:20:04 +02:00
struct Map : public Tree<K, Pair<K, V>, _KeyFun<K, Pair<K, V>>, Compare, Allocator> {
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
2024-06-09 12:20:04 +02:00
constexpr Map() noexcept : Super() {}
2022-04-15 11:19:30 +02:00
constexpr explicit Map(const Compare& comp, const Allocator& alloc = Allocator()) noexcept(noexcept(Allocator()))
2024-06-09 12:20:04 +02:00
: Super(comp, alloc) {}
constexpr explicit Map(const Allocator& alloc) noexcept(noexcept(Compare())) : Super(alloc) {}
2024-07-12 13:33:52 +02:00
constexpr Map(std::initializer_list<Pair<K, V>> init) : Super(init) {}
2025-03-20 20:15:38 +01:00
constexpr mapped_type& operator[](const key_type& key) {
auto [it, _] = Super::insert(Pair<K, V>(key));
2023-12-22 19:46:07 +01:00
return it->value;
2021-11-01 20:25:16 +01:00
}
2024-06-09 12:20:04 +02:00
constexpr const mapped_type& operator[](const key_type& key) const { return Super::find(key)->value; }
constexpr mapped_type& at(const key_type& key) {
2023-12-22 19:46:07 +01:00
iterator elem = Super::find(key);
2024-06-09 12:20:04 +02:00
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
}
2024-06-09 12:20:04 +02:00
constexpr const mapped_type& at(const key_type& key) const { return Super::find(key)->value; }
constexpr iterator find(const key_type& key) { return Super::find(key); }
constexpr iterator erase(const key_type& key) { return Super::remove(key); }
constexpr bool exists(const key_type& key) const { return Super::find(key) != Super::end(); }
constexpr bool exists(const key_type& key) { return Super::find(key) != Super::end(); }
constexpr bool contains(const key_type& key) const { return exists(key); }
constexpr bool contains(const key_type& key) { return exists(key); }
constexpr Pair<iterator, bool> insert(const value_type& val) { return Super::insert(val); }
void save(ArchiveBuffer& buffer) const {
2023-12-22 19:46:07 +01:00
size_t s = Super::size();
buffer.writeBytes(&s, sizeof(uint64));
2024-06-09 12:20:04 +02:00
for (const auto& [k, v] : *this) {
2023-07-13 21:01:20 +02:00
Serialization::save(buffer, k);
Serialization::save(buffer, v);
}
}
2024-06-09 12:20:04 +02:00
void load(ArchiveBuffer& buffer) {
2023-07-13 21:01:20 +02:00
uint64 len = 0;
buffer.readBytes(&len, sizeof(uint64));
2024-06-09 12:20:04 +02:00
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