diff options
-rw-r--r-- | AK/Traits.h | 14 | ||||
-rw-r--r-- | Tests/AK/TestHashTable.cpp | 24 |
2 files changed, 38 insertions, 0 deletions
diff --git a/AK/Traits.h b/AK/Traits.h index fc67ce4b9d..539174b7d0 100644 --- a/AK/Traits.h +++ b/AK/Traits.h @@ -40,6 +40,20 @@ requires(IsIntegral<T>) struct Traits<T> : public GenericTraits<T> { } }; +#ifndef KERNEL +template<typename T> +requires(IsFloatingPoint<T>) struct Traits<T> : public GenericTraits<T> { + static constexpr bool is_trivial() { return true; } + static constexpr unsigned hash(T value) + { + if constexpr (sizeof(T) < 8) + return int_hash(bit_cast<u32>(value)); + else + return u64_hash(bit_cast<u64>(value)); + } +}; +#endif + template<typename T> requires(IsPointer<T> && !Detail::IsPointerOfType<char, T>) struct Traits<T> : public GenericTraits<T> { static unsigned hash(T p) { return ptr_hash((FlatPtr)p); } diff --git a/Tests/AK/TestHashTable.cpp b/Tests/AK/TestHashTable.cpp index 02c94c6b62..51e864cf4f 100644 --- a/Tests/AK/TestHashTable.cpp +++ b/Tests/AK/TestHashTable.cpp @@ -256,6 +256,30 @@ TEST_CASE(non_trivial_type_table) EXPECT_EQ(table.remove_all_matching([&](auto&) { return true; }), false); } +TEST_CASE(floats) +{ + HashTable<float> table; + table.set(0); + table.set(1.0f); + table.set(2.0f); + EXPECT_EQ(table.size(), 3u); + EXPECT(table.contains(0)); + EXPECT(table.contains(1.0f)); + EXPECT(table.contains(2.0f)); +} + +TEST_CASE(doubles) +{ + HashTable<double> table; + table.set(0); + table.set(1.0); + table.set(2.0); + EXPECT_EQ(table.size(), 3u); + EXPECT(table.contains(0)); + EXPECT(table.contains(1.0)); + EXPECT(table.contains(2.0)); +} + // Inserting and removing a bunch of elements will "thrash" the table, leading to a lot of "deleted" markers. BENCHMARK_CASE(benchmark_thrashing) { |