json/include/nlohmann/detail/hash.hpp

118 lines
3.4 KiB
C++
Raw Permalink Normal View History

2020-07-14 12:31:19 +00:00
#pragma once
2020-07-15 11:45:16 +00:00
#include <cstddef> // size_t, uint8_t
2020-07-14 12:31:19 +00:00
#include <functional> // hash
namespace nlohmann
{
namespace detail
{
2020-07-15 11:45:16 +00:00
// boost::hash_combine
inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
2020-07-14 12:31:19 +00:00
{
seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
return seed;
}
2020-07-15 11:45:16 +00:00
/*!
@brief hash a JSON value
The hash function tries to rely on std::hash where possible. Furthermore, the
type of the JSON value is taken into account to have different hash values for
null, 0, 0U, and false, etc.
@tparam BasicJsonType basic_json specialization
@param j JSON value to hash
@return hash value of j
*/
2020-07-14 12:31:19 +00:00
template<typename BasicJsonType>
std::size_t hash(const BasicJsonType& j)
{
2020-07-15 07:27:01 +00:00
using string_t = typename BasicJsonType::string_t;
using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t;
2020-07-15 11:45:16 +00:00
const auto type = static_cast<std::size_t>(j.type());
2020-07-14 12:31:19 +00:00
switch (j.type())
{
case BasicJsonType::value_t::null:
2020-07-14 12:34:55 +00:00
case BasicJsonType::value_t::discarded:
2020-07-15 11:45:16 +00:00
{
return combine(type, 0);
}
2020-07-14 12:31:19 +00:00
case BasicJsonType::value_t::object:
{
2020-07-15 11:45:16 +00:00
auto seed = combine(type, j.size());
2020-07-14 12:31:19 +00:00
for (const auto& element : j.items())
{
2020-07-15 07:27:01 +00:00
const auto h = std::hash<string_t> {}(element.key());
2020-07-14 12:31:19 +00:00
seed = combine(seed, h);
seed = combine(seed, hash(element.value()));
}
return seed;
}
case BasicJsonType::value_t::array:
{
2020-07-15 11:45:16 +00:00
auto seed = combine(type, j.size());
2020-07-14 12:31:19 +00:00
for (const auto& element : j)
{
seed = combine(seed, hash(element));
}
return seed;
}
case BasicJsonType::value_t::string:
{
2020-07-15 07:27:01 +00:00
const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
2020-07-15 11:45:16 +00:00
return combine(type, h);
2020-07-14 12:31:19 +00:00
}
case BasicJsonType::value_t::boolean:
{
const auto h = std::hash<bool> {}(j.template get<bool>());
2020-07-15 11:45:16 +00:00
return combine(type, h);
2020-07-14 12:31:19 +00:00
}
case BasicJsonType::value_t::number_integer:
{
2020-07-15 07:27:01 +00:00
const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
2020-07-15 11:45:16 +00:00
return combine(type, h);
2020-07-14 12:31:19 +00:00
}
case nlohmann::detail::value_t::number_unsigned:
{
2020-07-15 07:27:01 +00:00
const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
2020-07-15 11:45:16 +00:00
return combine(type, h);
2020-07-14 12:31:19 +00:00
}
case nlohmann::detail::value_t::number_float:
{
2020-07-15 07:27:01 +00:00
const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
2020-07-15 11:45:16 +00:00
return combine(type, h);
2020-07-14 12:31:19 +00:00
}
case nlohmann::detail::value_t::binary:
{
2020-07-15 11:45:16 +00:00
auto seed = combine(type, j.get_binary().size());
2020-07-15 07:27:01 +00:00
const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
seed = combine(seed, h);
2020-07-14 12:31:19 +00:00
seed = combine(seed, j.get_binary().subtype());
for (const auto byte : j.get_binary())
{
seed = combine(seed, std::hash<std::uint8_t> {}(byte));
}
return seed;
}
2020-07-15 07:27:01 +00:00
default: // LCOV_EXCL_LINE
JSON_ASSERT(false); // LCOV_EXCL_LINE
}
2020-07-14 12:31:19 +00:00
}
} // namespace detail
} // namespace nlohmann