JD2022-TU1/main/extern/dtest/dtcore/inc/dttraits.h

36 lines
840 B
C++

#pragma once
#include "dtcore/inc/dtdefinitions.h"
namespace dt
{
template<typename T> struct Traits
{
static U32 Hash(const T& rhs);
static void Assign(T& target, const T& source);
static int Compare(const T& lhs, const T& rhs);
};
template<typename T> U32 Traits<T>::Hash(const T& rhs)
{
U32 hash = 5381;
const U8* p = reinterpret_cast<const U8*>(&rhs);
for (unsigned int i = 0; i < sizeof(T); ++i)
{
hash = ((hash << 5) + hash) + p[i];
}
return hash;
}
template<typename T> void Traits<T>::Assign(T& target, const T& source)
{
return target = source;
}
template<typename T> int Traits<T>::Compare(const T& lhs, const T& rhs)
{
return lhs == rhs ? 0 : (lhs < rhs ? -1 : 1);
}
}