2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
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
|
|
|
|
|
2020-02-16 01:01:18 +00:00
|
|
|
#include <AK/Forward.h>
|
2020-02-10 10:55:34 +00:00
|
|
|
#include <AK/HashFunctions.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
2019-06-29 17:14:03 +00:00
|
|
|
struct GenericTraits {
|
2019-08-14 09:47:38 +00:00
|
|
|
using PeekType = T;
|
2019-08-07 13:05:10 +00:00
|
|
|
static constexpr bool is_trivial() { return false; }
|
2020-12-23 18:35:01 +00:00
|
|
|
static constexpr bool equals(const T& a, const T& b) { return a == b; }
|
2019-06-29 17:14:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct Traits : public GenericTraits<T> {
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2019-06-29 17:14:03 +00:00
|
|
|
struct Traits<int> : public GenericTraits<int> {
|
2019-08-07 09:53:21 +00:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2019-01-31 16:31:23 +00:00
|
|
|
static unsigned hash(int i) { return int_hash(i); }
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2020-12-13 23:53:47 +00:00
|
|
|
template<>
|
|
|
|
struct Traits<char> : public GenericTraits<char> {
|
|
|
|
static constexpr bool is_trivial() { return true; }
|
|
|
|
static unsigned hash(char c) { return int_hash(c); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Traits<i16> : public GenericTraits<i16> {
|
|
|
|
static constexpr bool is_trivial() { return true; }
|
|
|
|
static unsigned hash(i16 i) { return int_hash(i); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct Traits<i64> : public GenericTraits<i64> {
|
|
|
|
static constexpr bool is_trivial() { return true; }
|
|
|
|
static unsigned hash(i64 i) { return u64_hash(i); }
|
|
|
|
};
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
template<>
|
2019-06-29 17:14:03 +00:00
|
|
|
struct Traits<unsigned> : public GenericTraits<unsigned> {
|
2019-08-07 09:53:21 +00:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2019-01-31 16:31:23 +00:00
|
|
|
static unsigned hash(unsigned u) { return int_hash(u); }
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2020-11-29 08:46:37 +00:00
|
|
|
template<>
|
|
|
|
struct Traits<u8> : public GenericTraits<u8> {
|
|
|
|
static constexpr bool is_trivial() { return true; }
|
|
|
|
static unsigned hash(u8 u) { return int_hash(u); }
|
|
|
|
};
|
|
|
|
|
2019-03-13 15:05:56 +00:00
|
|
|
template<>
|
2019-07-03 19:17:35 +00:00
|
|
|
struct Traits<u16> : public GenericTraits<u16> {
|
2019-08-07 09:53:21 +00:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2019-07-03 19:17:35 +00:00
|
|
|
static unsigned hash(u16 u) { return int_hash(u); }
|
2019-03-13 15:05:56 +00:00
|
|
|
};
|
|
|
|
|
2020-01-03 06:56:33 +00:00
|
|
|
template<>
|
|
|
|
struct Traits<u64> : public GenericTraits<u64> {
|
|
|
|
static constexpr bool is_trivial() { return true; }
|
|
|
|
static unsigned hash(u64 u) { return u64_hash(u); }
|
|
|
|
};
|
|
|
|
|
2018-10-11 14:52:30 +00:00
|
|
|
template<typename T>
|
2019-08-14 09:47:38 +00:00
|
|
|
struct Traits<T*> : public GenericTraits<T*> {
|
2018-10-27 07:33:24 +00:00
|
|
|
static unsigned hash(const T* p)
|
|
|
|
{
|
2019-06-22 19:21:57 +00:00
|
|
|
return int_hash((unsigned)(__PTRDIFF_TYPE__)p);
|
2018-10-27 07:33:24 +00:00
|
|
|
}
|
2019-08-07 09:53:21 +00:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
2019-06-29 17:14:03 +00:00
|
|
|
static bool equals(const T* a, const T* b) { return a == b; }
|
2018-10-11 14:52:30 +00:00
|
|
|
};
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
2020-02-16 01:01:18 +00:00
|
|
|
|
|
|
|
using AK::GenericTraits;
|
|
|
|
using AK::Traits;
|