mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
e575ee4462
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());
86 lines
2.6 KiB
C++
86 lines
2.6 KiB
C++
/*
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/BitCast.h>
|
|
#include <AK/Concepts.h>
|
|
#include <AK/Forward.h>
|
|
#include <AK/HashFunctions.h>
|
|
#include <AK/StringHash.h>
|
|
|
|
namespace AK {
|
|
|
|
template<typename T>
|
|
struct GenericTraits {
|
|
using PeekType = T&;
|
|
using ConstPeekType = T const&;
|
|
static constexpr bool is_trivial() { return false; }
|
|
static constexpr bool is_trivially_serializable() { return false; }
|
|
static constexpr bool equals(T const& a, T const& b) { return a == b; }
|
|
template<Concepts::HashCompatible<T> U>
|
|
static bool equals(T const& self, U const& other) { return self == other; }
|
|
};
|
|
|
|
template<typename T>
|
|
struct Traits : public GenericTraits<T> {
|
|
};
|
|
|
|
template<Integral T>
|
|
struct Traits<T> : public GenericTraits<T> {
|
|
static constexpr bool is_trivial() { return true; }
|
|
static constexpr bool is_trivially_serializable() { return true; }
|
|
static constexpr unsigned hash(T value)
|
|
{
|
|
if constexpr (sizeof(T) < 8)
|
|
return int_hash(value);
|
|
else
|
|
return u64_hash(value);
|
|
}
|
|
};
|
|
|
|
#ifndef KERNEL
|
|
template<FloatingPoint T>
|
|
struct Traits<T> : public GenericTraits<T> {
|
|
static constexpr bool is_trivial() { return true; }
|
|
static constexpr bool is_trivially_serializable() { 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(p); }
|
|
static constexpr bool is_trivial() { return true; }
|
|
};
|
|
|
|
template<Enum T>
|
|
struct Traits<T> : public GenericTraits<T> {
|
|
static unsigned hash(T value) { return Traits<UnderlyingType<T>>::hash(to_underlying(value)); }
|
|
static constexpr bool is_trivial() { return Traits<UnderlyingType<T>>::is_trivial(); }
|
|
static constexpr bool is_trivially_serializable() { return Traits<UnderlyingType<T>>::is_trivially_serializable(); }
|
|
};
|
|
|
|
template<typename T>
|
|
requires(Detail::IsPointerOfType<char, T>) struct Traits<T> : public GenericTraits<T> {
|
|
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; }
|
|
};
|
|
|
|
}
|
|
|
|
#if USING_AK_GLOBALLY
|
|
using AK::GenericTraits;
|
|
using AK::Traits;
|
|
#endif
|