2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-04-10 17:17:47 +00:00
|
|
|
#include <AK/BitCast.h>
|
2021-08-19 12:46:05 +00:00
|
|
|
#include <AK/Concepts.h>
|
2020-02-16 01:01:18 +00:00
|
|
|
#include <AK/Forward.h>
|
2020-02-10 10:55:34 +00:00
|
|
|
#include <AK/HashFunctions.h>
|
2021-11-07 13:37:05 +00:00
|
|
|
#include <AK/StringHash.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
2023-11-08 19:29:12 +00:00
|
|
|
struct DefaultTraits {
|
2022-04-03 13:34:57 +00:00
|
|
|
using PeekType = T&;
|
|
|
|
using ConstPeekType = T const&;
|
2019-08-07 13:05:10 +00:00
|
|
|
static constexpr bool is_trivial() { return false; }
|
2023-01-14 20:13:29 +00:00
|
|
|
static constexpr bool is_trivially_serializable() { return false; }
|
2022-10-16 22:06:11 +00:00
|
|
|
static constexpr bool equals(T const& a, T const& b) { return a == b; }
|
2022-01-29 18:01:35 +00:00
|
|
|
template<Concepts::HashCompatible<T> U>
|
AK+Kernel: Unify Traits<T>::equals()'s argument order on different types
There was a small mishmash of argument order, as seen on the table:
| Traits<T>::equals(U, T) | Traits<T>::equals(T, U)
============= | ======================= | =======================
uses equals() | HashMap | Vector, HashTable
defines equals() | *String[^1] | ByteBuffer
[^1]: String, DeprecatedString, their Fly-type equivalents and KString.
This mostly meant that you couldn't use a StringView for finding a value
in Vector<String>.
I'm changing the order of arguments to make the trait type itself first
(`Traits<T>::equals(T, U)`), as I think it's more expected and makes us
more consistent with the rest of the functions that put the stored type
first (like StringUtils functions and binary_serach). I've also renamed
the variable name "other" in find functions to "entry" to give more
importance to the value.
With this change, each of the following lines will now compile
successfully:
Vector<String>().contains_slow("WHF!"sv);
HashTable<String>().contains("WHF!"sv);
HashMap<ByteBuffer, int>().contains("WHF!"sv.bytes());
2023-08-21 14:38:11 +00:00
|
|
|
static bool equals(T const& self, U const& other) { return self == other; }
|
2019-06-29 17:14:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2023-11-08 19:29:12 +00:00
|
|
|
struct Traits : public DefaultTraits<T> {
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2023-11-08 20:12:54 +00:00
|
|
|
template<typename T>
|
|
|
|
struct Traits<T const> : public Traits<T> {
|
|
|
|
using PeekType = typename Traits<T>::ConstPeekType;
|
|
|
|
};
|
|
|
|
|
2022-11-14 18:20:59 +00:00
|
|
|
template<Integral T>
|
2023-11-08 19:29:12 +00:00
|
|
|
struct Traits<T> : public DefaultTraits<T> {
|
2020-01-03 06:56:33 +00:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2023-01-14 20:13:29 +00:00
|
|
|
static constexpr bool is_trivially_serializable() { return true; }
|
2023-09-20 22:14:35 +00:00
|
|
|
static unsigned hash(T value)
|
2021-07-12 18:08:03 +00:00
|
|
|
{
|
2024-03-24 15:40:51 +00:00
|
|
|
if constexpr (sizeof(T) < 8)
|
|
|
|
return int_hash(value);
|
|
|
|
else
|
|
|
|
return u64_hash(value);
|
2021-07-12 18:08:03 +00:00
|
|
|
}
|
2020-01-03 06:56:33 +00:00
|
|
|
};
|
|
|
|
|
2022-11-14 18:20:59 +00:00
|
|
|
template<FloatingPoint T>
|
2023-11-08 19:29:12 +00:00
|
|
|
struct Traits<T> : public DefaultTraits<T> {
|
2022-04-10 10:27:08 +00:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2023-01-14 20:13:29 +00:00
|
|
|
static constexpr bool is_trivially_serializable() { return true; }
|
2023-09-20 22:14:35 +00:00
|
|
|
static unsigned hash(T value)
|
2022-04-10 10:27:08 +00:00
|
|
|
{
|
2024-03-24 15:40:51 +00:00
|
|
|
if constexpr (sizeof(T) < 8)
|
|
|
|
return int_hash(bit_cast<u32>(value));
|
|
|
|
else
|
|
|
|
return u64_hash(bit_cast<u64>(value));
|
2022-04-10 10:27:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-11 14:52:30 +00:00
|
|
|
template<typename T>
|
2023-11-08 19:29:12 +00:00
|
|
|
requires(IsPointer<T> && !Detail::IsPointerOfType<char, T>) struct Traits<T> : public DefaultTraits<T> {
|
2024-03-24 15:40:51 +00:00
|
|
|
static unsigned hash(T p) { return ptr_hash(bit_cast<FlatPtr>(p)); }
|
2019-08-07 09:53:21 +00:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2018-10-11 14:52:30 +00:00
|
|
|
};
|
|
|
|
|
2021-08-19 12:46:05 +00:00
|
|
|
template<Enum T>
|
2023-11-08 19:29:12 +00:00
|
|
|
struct Traits<T> : public DefaultTraits<T> {
|
2021-08-19 12:46:05 +00:00
|
|
|
static unsigned hash(T value) { return Traits<UnderlyingType<T>>::hash(to_underlying(value)); }
|
|
|
|
static constexpr bool is_trivial() { return Traits<UnderlyingType<T>>::is_trivial(); }
|
2023-01-14 20:13:29 +00:00
|
|
|
static constexpr bool is_trivially_serializable() { return Traits<UnderlyingType<T>>::is_trivially_serializable(); }
|
2021-08-19 12:46:05 +00:00
|
|
|
};
|
|
|
|
|
2021-11-07 13:37:05 +00:00
|
|
|
template<typename T>
|
2023-11-08 19:29:12 +00:00
|
|
|
requires(Detail::IsPointerOfType<char, T>) struct Traits<T> : public DefaultTraits<T> {
|
2021-11-07 13:37:05 +00:00
|
|
|
static unsigned hash(T const value) { return string_hash(value, strlen(value)); }
|
|
|
|
static constexpr bool equals(T const a, T const b) { return strcmp(a, b); }
|
|
|
|
static constexpr bool is_trivial() { return true; }
|
|
|
|
};
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2020-02-16 01:01:18 +00:00
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2023-11-08 19:29:12 +00:00
|
|
|
using AK::DefaultTraits;
|
2020-02-16 01:01:18 +00:00
|
|
|
using AK::Traits;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|