From 484823e9d524ae0030552d02debdffbb14fa7f7e Mon Sep 17 00:00:00 2001 From: Itamar Date: Sat, 8 May 2021 12:12:32 +0300 Subject: [PATCH] AK: Add a non-const overload to HapMap::get() Additionally, the const version of get() returns Optional for smart pointers. For example, if the value in the HashMap is OwnPtr, HashMap::get() const returns Optional. --- AK/HashMap.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/AK/HashMap.h b/AK/HashMap.h index b2fa38125e..dc8f74ac33 100644 --- a/AK/HashMap.h +++ b/AK/HashMap.h @@ -95,7 +95,23 @@ public: void ensure_capacity(size_t capacity) { m_table.ensure_capacity(capacity); } - Optional::PeekType> get(const K& key) const + Optional::PeekType> get(const K& key) const requires(!IsPointer::PeekType>) + { + auto it = find(key); + if (it == end()) + return {}; + return (*it).value; + } + + Optional::ConstPeekType> get(const K& key) const requires(IsPointer::PeekType>) + { + auto it = find(key); + if (it == end()) + return {}; + return (*it).value; + } + + Optional::PeekType> get(const K& key) requires(!IsConst::PeekType>) { auto it = find(key); if (it == end())