refactor: switch CompoundTag from unordered_map to flat_map

This commit is contained in:
MatthewBeshay
2026-04-04 09:42:27 +11:00
parent 57641ebcca
commit f11473776c
+6 -6
View File
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include <flat_map>
#include <memory> #include <memory>
#include <unordered_map>
#include "ByteArrayTag.h" #include "ByteArrayTag.h"
#include "ByteTag.h" #include "ByteTag.h"
@@ -16,14 +16,14 @@
class CompoundTag : public Tag { class CompoundTag : public Tag {
private: private:
std::unordered_map<std::wstring, std::unique_ptr<Tag>> tags; std::flat_map<std::wstring, std::unique_ptr<Tag>> tags;
public: public:
CompoundTag() : Tag(L"") {} CompoundTag() : Tag(L"") {}
CompoundTag(const std::wstring& name) : Tag(name) {} CompoundTag(const std::wstring& name) : Tag(name) {}
void write(DataOutput* dos) { void write(DataOutput* dos) {
for (auto& [key, value] : tags) { for (auto&& [key, value] : tags) {
Tag::writeNamedTag(value.get(), dos); Tag::writeNamedTag(value.get(), dos);
} }
dos->writeByte(Tag::TAG_End); dos->writeByte(Tag::TAG_End);
@@ -49,7 +49,7 @@ public:
std::vector<Tag*> getAllTags() { std::vector<Tag*> getAllTags() {
std::vector<Tag*> ret; std::vector<Tag*> ret;
ret.reserve(tags.size()); ret.reserve(tags.size());
for (auto& [key, value] : tags) { for (auto&& [key, value] : tags) {
ret.push_back(value.get()); ret.push_back(value.get());
} }
return ret; return ret;
@@ -228,7 +228,7 @@ public:
Tag* copy() { Tag* copy() {
CompoundTag* tag = new CompoundTag(getName()); CompoundTag* tag = new CompoundTag(getName());
for (auto& [key, value] : tags) { for (auto&& [key, value] : tags) {
tag->put(key, value->copy()); tag->put(key, value->copy());
} }
return tag; return tag;
@@ -239,7 +239,7 @@ public:
CompoundTag* o = (CompoundTag*)obj; CompoundTag* o = (CompoundTag*)obj;
if (tags.size() == o->tags.size()) { if (tags.size() == o->tags.size()) {
for (auto& [key, value] : tags) { for (auto&& [key, value] : tags) {
auto itFind = o->tags.find(key); auto itFind = o->tags.find(key);
if (itFind == o->tags.end() || if (itFind == o->tags.end() ||
!value->equals(itFind->second.get())) { !value->equals(itFind->second.get())) {