2020-01-18 03:38:21 -05:00
|
|
|
/*
|
2024-10-04 07:19:50 -04:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 03:38:21 -05:00
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 03:38:21 -05:00
|
|
|
*/
|
|
|
|
|
2018-10-10 05:53:07 -04:00
|
|
|
#pragma once
|
|
|
|
|
2022-04-10 13:17:47 -04:00
|
|
|
#include <AK/BitCast.h>
|
2021-08-19 08:46:05 -04:00
|
|
|
#include <AK/Concepts.h>
|
2020-02-15 20:01:18 -05:00
|
|
|
#include <AK/Forward.h>
|
2020-02-10 05:55:34 -05:00
|
|
|
#include <AK/HashFunctions.h>
|
2021-11-07 08:37:05 -05:00
|
|
|
#include <AK/StringHash.h>
|
2018-10-10 05:53:07 -04:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
2023-11-08 14:29:12 -05:00
|
|
|
struct DefaultTraits {
|
2022-04-03 09:34:57 -04:00
|
|
|
using PeekType = T&;
|
|
|
|
using ConstPeekType = T const&;
|
2019-08-07 09:05:10 -04:00
|
|
|
static constexpr bool is_trivial() { return false; }
|
2023-01-14 15:13:29 -05:00
|
|
|
static constexpr bool is_trivially_serializable() { return false; }
|
2022-10-16 18:06:11 -04:00
|
|
|
static constexpr bool equals(T const& a, T const& b) { return a == b; }
|
2022-01-29 13:01:35 -05: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 10:38:11 -04:00
|
|
|
static bool equals(T const& self, U const& other) { return self == other; }
|
2019-06-29 13:14:03 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2023-11-08 14:29:12 -05:00
|
|
|
struct Traits : public DefaultTraits<T> {
|
2018-10-10 05:53:07 -04:00
|
|
|
};
|
|
|
|
|
2023-11-08 15:12:54 -05:00
|
|
|
template<typename T>
|
|
|
|
struct Traits<T const> : public Traits<T> {
|
|
|
|
using PeekType = typename Traits<T>::ConstPeekType;
|
|
|
|
};
|
|
|
|
|
2022-11-14 13:20:59 -05:00
|
|
|
template<Integral T>
|
2023-11-08 14:29:12 -05:00
|
|
|
struct Traits<T> : public DefaultTraits<T> {
|
2020-01-03 01:56:33 -05:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2023-01-14 15:13:29 -05:00
|
|
|
static constexpr bool is_trivially_serializable() { return true; }
|
2023-09-20 18:14:35 -04:00
|
|
|
static unsigned hash(T value)
|
2021-07-12 14:08:03 -04:00
|
|
|
{
|
2024-03-24 11:40:51 -04:00
|
|
|
if constexpr (sizeof(T) < 8)
|
|
|
|
return int_hash(value);
|
|
|
|
else
|
|
|
|
return u64_hash(value);
|
2021-07-12 14:08:03 -04:00
|
|
|
}
|
2020-01-03 01:56:33 -05:00
|
|
|
};
|
|
|
|
|
2022-11-14 13:20:59 -05:00
|
|
|
template<FloatingPoint T>
|
2023-11-08 14:29:12 -05:00
|
|
|
struct Traits<T> : public DefaultTraits<T> {
|
2022-04-10 06:27:08 -04:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2023-01-14 15:13:29 -05:00
|
|
|
static constexpr bool is_trivially_serializable() { return true; }
|
2023-09-20 18:14:35 -04:00
|
|
|
static unsigned hash(T value)
|
2022-04-10 06:27:08 -04:00
|
|
|
{
|
2024-03-24 11:40:51 -04:00
|
|
|
if constexpr (sizeof(T) < 8)
|
|
|
|
return int_hash(bit_cast<u32>(value));
|
|
|
|
else
|
|
|
|
return u64_hash(bit_cast<u64>(value));
|
2022-04-10 06:27:08 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-11 10:52:30 -04:00
|
|
|
template<typename T>
|
2023-11-08 14:29:12 -05:00
|
|
|
requires(IsPointer<T> && !Detail::IsPointerOfType<char, T>) struct Traits<T> : public DefaultTraits<T> {
|
2024-03-24 11:40:51 -04:00
|
|
|
static unsigned hash(T p) { return ptr_hash(bit_cast<FlatPtr>(p)); }
|
2019-08-07 05:53:21 -04:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2018-10-11 10:52:30 -04:00
|
|
|
};
|
|
|
|
|
2021-08-19 08:46:05 -04:00
|
|
|
template<Enum T>
|
2023-11-08 14:29:12 -05:00
|
|
|
struct Traits<T> : public DefaultTraits<T> {
|
2021-08-19 08:46:05 -04: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 15:13:29 -05:00
|
|
|
static constexpr bool is_trivially_serializable() { return Traits<UnderlyingType<T>>::is_trivially_serializable(); }
|
2021-08-19 08:46:05 -04:00
|
|
|
};
|
|
|
|
|
2021-11-07 08:37:05 -05:00
|
|
|
template<typename T>
|
2023-11-08 14:29:12 -05:00
|
|
|
requires(Detail::IsPointerOfType<char, T>) struct Traits<T> : public DefaultTraits<T> {
|
2021-11-07 08:37:05 -05: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 05:53:07 -04:00
|
|
|
}
|
2020-02-15 20:01:18 -05:00
|
|
|
|
2022-11-26 06:18:30 -05:00
|
|
|
#if USING_AK_GLOBALLY
|
2023-11-08 14:29:12 -05:00
|
|
|
using AK::DefaultTraits;
|
2020-02-15 20:01:18 -05:00
|
|
|
using AK::Traits;
|
2022-11-26 06:18:30 -05:00
|
|
|
#endif
|