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