Extract hashing stuff out of Language.h into LanguageHash.h

This commit is contained in:
Alexander McCord 2024-06-08 08:54:45 -07:00
parent f84f7e7e4e
commit e48de639e1
3 changed files with 57 additions and 56 deletions

View file

@ -2,6 +2,7 @@
#pragma once
#include "Luau/Id.h"
#include "Luau/LanguageHash.h"
#include "Luau/Slice.h"
#include <array>
@ -45,22 +46,6 @@
namespace Luau::EqSat
{
template<typename T, typename = void>
struct LanguageHash
{
size_t operator()(const T&) const
{
// See available specializations at the bottom of this file.
static_assert(false, "missing languageHash specialization");
}
};
template <typename T>
std::size_t languageHash(const T& lang)
{
return LanguageHash<T>{}(lang);
}
template<typename Phantom, typename T>
struct Atom
{
@ -401,44 +386,4 @@ public:
};
};
inline void hashCombine(size_t& seed, size_t hash)
{
// Golden Ratio constant used for better hash scattering
// See https://softwareengineering.stackexchange.com/a/402543
seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template<typename T>
struct LanguageHash<T, std::void_t<decltype(std::hash<T>{}(std::declval<T>()))>>
{
size_t operator()(const T& t) const
{
return std::hash<T>{}(t);
}
};
template<typename T, size_t I>
struct LanguageHash<std::array<T, I>>
{
size_t operator()(const std::array<T, I>& array) const
{
size_t seed = 0;
for (const T& t : array)
hashCombine(seed, languageHash(t));
return seed;
}
};
template<typename T>
struct LanguageHash<std::vector<T>>
{
size_t operator()(const std::vector<T>& vector) const
{
size_t seed = 0;
for (const T& t : vector)
hashCombine(seed, languageHash(t));
return seed;
}
};
} // namespace Luau::EqSat

View file

@ -0,0 +1,55 @@
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once
#include <functional>
namespace Luau::EqSat
{
template<typename T>
struct LanguageHash
{
size_t operator()(const T& t, decltype(std::hash<T>{}(std::declval<T>()))* = 0) const
{
return std::hash<T>{}(t);
}
};
template <typename T>
size_t languageHash(const T& lang)
{
return LanguageHash<T>{}(lang);
}
inline void hashCombine(size_t& seed, size_t hash)
{
// Golden Ratio constant used for better hash scattering
// See https://softwareengineering.stackexchange.com/a/402543
seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template<typename T, size_t I>
struct LanguageHash<std::array<T, I>>
{
size_t operator()(const std::array<T, I>& array) const
{
size_t seed = 0;
for (const T& t : array)
hashCombine(seed, languageHash(t));
return seed;
}
};
template<typename T>
struct LanguageHash<std::vector<T>>
{
size_t operator()(const std::vector<T>& vector) const
{
size_t seed = 0;
for (const T& t : vector)
hashCombine(seed, languageHash(t));
return seed;
}
};
} // namespace Luau::EqSat

View file

@ -300,6 +300,7 @@ target_sources(Luau.EqSat PRIVATE
EqSat/include/Luau/EGraph.h
EqSat/include/Luau/Id.h
EqSat/include/Luau/Language.h
EqSat/include/Luau/LanguageHash.h
EqSat/include/Luau/Slice.h
EqSat/include/Luau/UnionFind.h