json/include/nlohmann/detail/hash.hpp

130 lines
3.9 KiB
C++
Raw Permalink Normal View History

// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
2023-11-29 00:36:31 +03:00
// | | |__ | | | | | | version 3.11.3
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
2020-07-14 15:31:19 +03:00
#pragma once
2023-11-29 17:02:51 +03:00
#include <cstddef> // size_t
#include <cstdint> // uint8_t
#include <functional> // hash
2020-07-14 15:31:19 +03:00
#include <nlohmann/detail/abi_macros.hpp>
2021-08-18 14:33:35 +03:00
#include <nlohmann/detail/value_t.hpp>
2020-12-26 19:56:16 +03:00
NLOHMANN_JSON_NAMESPACE_BEGIN
2023-11-29 17:02:51 +03:00
namespace detail
{
2020-07-14 15:31:19 +03:00
2020-07-15 14:45:16 +03:00
// boost::hash_combine
inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
2020-07-14 15:31:19 +03:00
{
seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
return seed;
}
2020-07-15 14:45:16 +03: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 15:31:19 +03:00
template<typename BasicJsonType>
std::size_t hash(const BasicJsonType& j)
{
2020-07-15 10:27:01 +03: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 14:45:16 +03:00
const auto type = static_cast<std::size_t>(j.type());
2020-07-14 15:31:19 +03:00
switch (j.type())
{
case BasicJsonType::value_t::null:
2020-07-14 15:34:55 +03:00
case BasicJsonType::value_t::discarded:
2020-07-15 14:45:16 +03:00
{
return combine(type, 0);
}
2020-07-14 15:31:19 +03:00
case BasicJsonType::value_t::object:
{
2020-07-15 14:45:16 +03:00
auto seed = combine(type, j.size());
2020-07-14 15:31:19 +03:00
for (const auto& element : j.items())
{
2023-11-29 17:02:51 +03:00
const auto h = std::hash<string_t>{}(element.key());
2020-07-14 15:31:19 +03:00
seed = combine(seed, h);
seed = combine(seed, hash(element.value()));
}
return seed;
}
case BasicJsonType::value_t::array:
{
2020-07-15 14:45:16 +03:00
auto seed = combine(type, j.size());
2020-07-14 15:31:19 +03:00
for (const auto& element : j)
{
seed = combine(seed, hash(element));
}
return seed;
}
case BasicJsonType::value_t::string:
{
2023-11-29 17:02:51 +03:00
const auto h = std::hash<string_t>{}(j.template get_ref<const string_t&>());
2020-07-15 14:45:16 +03:00
return combine(type, h);
2020-07-14 15:31:19 +03:00
}
case BasicJsonType::value_t::boolean:
{
2023-11-29 17:02:51 +03:00
const auto h = std::hash<bool>{}(j.template get<bool>());
2020-07-15 14:45:16 +03:00
return combine(type, h);
2020-07-14 15:31:19 +03:00
}
case BasicJsonType::value_t::number_integer:
{
2023-11-29 17:02:51 +03:00
const auto h = std::hash<number_integer_t>{}(j.template get<number_integer_t>());
2020-07-15 14:45:16 +03:00
return combine(type, h);
2020-07-14 15:31:19 +03:00
}
case BasicJsonType::value_t::number_unsigned:
2020-07-14 15:31:19 +03:00
{
2023-11-29 17:02:51 +03:00
const auto h = std::hash<number_unsigned_t>{}(j.template get<number_unsigned_t>());
2020-07-15 14:45:16 +03:00
return combine(type, h);
2020-07-14 15:31:19 +03:00
}
case BasicJsonType::value_t::number_float:
2020-07-14 15:31:19 +03:00
{
2023-11-29 17:02:51 +03:00
const auto h = std::hash<number_float_t>{}(j.template get<number_float_t>());
2020-07-15 14:45:16 +03:00
return combine(type, h);
2020-07-14 15:31:19 +03:00
}
case BasicJsonType::value_t::binary:
2020-07-14 15:31:19 +03:00
{
2020-07-15 14:45:16 +03:00
auto seed = combine(type, j.get_binary().size());
2023-11-29 17:02:51 +03:00
const auto h = std::hash<bool>{}(j.get_binary().has_subtype());
2020-07-15 10:27:01 +03:00
seed = combine(seed, h);
2021-08-14 14:40:52 +03:00
seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
2020-07-14 15:31:19 +03:00
for (const auto byte : j.get_binary())
{
2023-11-29 17:02:51 +03:00
seed = combine(seed, std::hash<std::uint8_t>{}(byte));
2020-07-14 15:31:19 +03:00
}
return seed;
}
2023-11-29 17:02:51 +03:00
default: // LCOV_EXCL_LINE
JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
return 0; // LCOV_EXCL_LINE
2020-07-15 10:27:01 +03:00
}
2020-07-14 15:31:19 +03:00
}
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END