[Glacier] Add debug methods for HashMap and RefPtr.

This commit is contained in:
Drew Galbraith 2023-12-02 13:25:28 -08:00
parent d9a4be6555
commit 8c5dd00443
2 changed files with 33 additions and 0 deletions

View File

@ -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<LinkedList<Pair<K, V>>> data_;
uint64_t size_ = 0;
@ -211,4 +214,26 @@ void HashMap<K, V, H>::ResizeIfNecessary() {
}
}
template <typename K, typename V>
void StrFormatValue(StringBuilder& builder, const HashMap<K, V>& value,
StringView opts) {
value.DebugIntoStr(builder);
}
template <typename K, typename V, class H>
void HashMap<K, V, H>::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

View File

@ -1,5 +1,7 @@
#pragma once
#include "glacier/string/str_format.h"
namespace glcr {
template <typename T>
@ -100,4 +102,10 @@ RefPtr<T> StaticCastRefPtr(const RefPtr<U>& ref) {
return RefPtr(static_cast<T*>(ref.get()), RefPtr<T>::DontAdopt);
}
template <typename T>
void StrFormatValue(StringBuilder& builder, const RefPtr<T>& value,
StringView opts) {
StrFormatValue(builder, (uint64_t)value.get(), opts);
}
} // namespace glcr