Skip to content

Commit

Permalink
AK: Add ptr_hash to use int_hash or u64_hash depending on pointer size
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs authored and awesomekling committed Feb 25, 2020
1 parent 32e6453 commit 7e6ac54
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions AK/HashFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ inline unsigned u64_hash(u64 key)
u32 last = key >> 32;
return pair_int_hash(first, last);
}

inline unsigned ptr_hash(uintptr_t ptr)
{
if constexpr(sizeof(ptr) == 8)
return u64_hash((u64)ptr);
else
return int_hash((u32)ptr);
}
2 changes: 1 addition & 1 deletion AK/OwnPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ inline void swap(OwnPtr<T>& a, OwnPtr<U>& b)
template<typename T>
struct Traits<OwnPtr<T>> : public GenericTraits<OwnPtr<T>> {
using PeekType = const T*;
static unsigned hash(const OwnPtr<T>& p) { return int_hash((u32)p.ptr()); }
static unsigned hash(const OwnPtr<T>& p) { return ptr_hash(p.ptr()); }
static bool equals(const OwnPtr<T>& a, const OwnPtr<T>& b) { return a.ptr() == b.ptr(); }
};

Expand Down
2 changes: 1 addition & 1 deletion AK/RefPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ inline const LogStream& operator<<(const LogStream& stream, const RefPtr<T>& val
template<typename T>
struct Traits<RefPtr<T>> : public GenericTraits<RefPtr<T>> {
using PeekType = const T*;
static unsigned hash(const RefPtr<T>& p) { return int_hash((u32)p.ptr()); }
static unsigned hash(const RefPtr<T>& p) { return ptr_hash(p.ptr()); }
static bool equals(const RefPtr<T>& a, const RefPtr<T>& b) { return a.ptr() == b.ptr(); }
};

Expand Down

0 comments on commit 7e6ac54

Please sign in to comment.