Skip to content

Commit 484823e

Browse files
itamar8910awesomekling
authored andcommitted
AK: Add a non-const overload to HapMap::get()
Additionally, the const version of get() returns Optional<ConstPeekType> for smart pointers. For example, if the value in the HashMap is OwnPtr<u32>, HashMap::get() const returns Optional<const u32*>.
1 parent b816bd0 commit 484823e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

AK/HashMap.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,23 @@ class HashMap {
9595

9696
void ensure_capacity(size_t capacity) { m_table.ensure_capacity(capacity); }
9797

98-
Optional<typename Traits<V>::PeekType> get(const K& key) const
98+
Optional<typename Traits<V>::PeekType> get(const K& key) const requires(!IsPointer<typename Traits<V>::PeekType>)
99+
{
100+
auto it = find(key);
101+
if (it == end())
102+
return {};
103+
return (*it).value;
104+
}
105+
106+
Optional<typename Traits<V>::ConstPeekType> get(const K& key) const requires(IsPointer<typename Traits<V>::PeekType>)
107+
{
108+
auto it = find(key);
109+
if (it == end())
110+
return {};
111+
return (*it).value;
112+
}
113+
114+
Optional<typename Traits<V>::PeekType> get(const K& key) requires(!IsConst<typename Traits<V>::PeekType>)
99115
{
100116
auto it = find(key);
101117
if (it == end())

0 commit comments

Comments
 (0)