From 8c5dd00443aa814c585467c6e7549dbb28670876 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Sat, 2 Dec 2023 13:25:28 -0800 Subject: [PATCH] [Glacier] Add debug methods for HashMap and RefPtr. --- lib/glacier/container/hash_map.h | 25 +++++++++++++++++++++++++ lib/glacier/memory/ref_ptr.h | 8 ++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/glacier/container/hash_map.h b/lib/glacier/container/hash_map.h index e8a9eec..1390ab6 100644 --- a/lib/glacier/container/hash_map.h +++ b/lib/glacier/container/hash_map.h @@ -6,6 +6,7 @@ #include "glacier/container/linked_list.h" #include "glacier/container/pair.h" #include "glacier/status/error.h" +#include "glacier/string/str_format.h" #include "glacier/string/string.h" #include "glacier/util/hash.h" @@ -53,6 +54,8 @@ class HashMap { void Resize(uint64_t new_size); + void DebugIntoStr(StringBuilder& builder) const; + private: Array>> data_; uint64_t size_ = 0; @@ -211,4 +214,26 @@ void HashMap::ResizeIfNecessary() { } } +template +void StrFormatValue(StringBuilder& builder, const HashMap& value, + StringView opts) { + value.DebugIntoStr(builder); +} + +template +void HashMap::DebugIntoStr(StringBuilder& builder) const { + for (uint64_t i = 0; i < data_.size(); i++) { + if (data_[i].size() == 0) { + continue; + } + StrFormatValue(builder, i, ""); + builder.PushBack(": "); + auto& ll = data_[i]; + for (auto& item : ll) { + StrFormatInternal(builder, "{},", item.first()); + } + builder.PushBack('\n'); + } +} + } // namespace glcr diff --git a/lib/glacier/memory/ref_ptr.h b/lib/glacier/memory/ref_ptr.h index c4dbdb2..290d768 100644 --- a/lib/glacier/memory/ref_ptr.h +++ b/lib/glacier/memory/ref_ptr.h @@ -1,5 +1,7 @@ #pragma once +#include "glacier/string/str_format.h" + namespace glcr { template @@ -100,4 +102,10 @@ RefPtr StaticCastRefPtr(const RefPtr& ref) { return RefPtr(static_cast(ref.get()), RefPtr::DontAdopt); } +template +void StrFormatValue(StringBuilder& builder, const RefPtr& value, + StringView opts) { + StrFormatValue(builder, (uint64_t)value.get(), opts); +} + } // namespace glcr