TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ hash()

template<typename BasicJsonType >
std::size_t nlohmann::detail::hash ( const BasicJsonType &  j)

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.

Template Parameters
BasicJsonTypebasic_json specialization
Parameters
jJSON value to hash
Returns
hash value of j

Definition at line 5240 of file json.hpp.

5241{
5242 using string_t = typename BasicJsonType::string_t;
5243 using number_integer_t = typename BasicJsonType::number_integer_t;
5244 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
5245 using number_float_t = typename BasicJsonType::number_float_t;
5246 const auto type = static_cast<std::size_t>(j.type());
5247
5248 switch (j.type())
5249 {
5250 case BasicJsonType::value_t::null:
5251 case BasicJsonType::value_t::discarded:
5252 {
5253 return combine(type, 0);
5254 }
5255
5256 case BasicJsonType::value_t::object:
5257 {
5258 auto seed = combine(type, j.size());
5259
5260 for (const auto &element : j.items())
5261 {
5262 const auto h = std::hash<string_t> {}(element.key());
5263 seed = combine(seed, h);
5264 seed = combine(seed, hash(element.value()));
5265 }
5266
5267 return seed;
5268 }
5269
5270 case BasicJsonType::value_t::array:
5271 {
5272 auto seed = combine(type, j.size());
5273
5274 for (const auto &element : j)
5275 {
5276 seed = combine(seed, hash(element));
5277 }
5278
5279 return seed;
5280 }
5281
5282 case BasicJsonType::value_t::string:
5283 {
5284 const auto h = std::hash<string_t> {}(j.template get_ref<const string_t &>());
5285 return combine(type, h);
5286 }
5287
5288 case BasicJsonType::value_t::boolean:
5289 {
5290 const auto h = std::hash<bool> {}(j.template get<bool>());
5291 return combine(type, h);
5292 }
5293
5294 case BasicJsonType::value_t::number_integer:
5295 {
5296 const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
5297 return combine(type, h);
5298 }
5299
5300 case BasicJsonType::value_t::number_unsigned:
5301 {
5302 const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
5303 return combine(type, h);
5304 }
5305
5306 case BasicJsonType::value_t::number_float:
5307 {
5308 const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
5309 return combine(type, h);
5310 }
5311
5312 case BasicJsonType::value_t::binary:
5313 {
5314 auto seed = combine(type, j.get_binary().size());
5315 const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
5316 seed = combine(seed, h);
5317 seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
5318
5319 for (const auto byte : j.get_binary())
5320 {
5321 seed = combine(seed, std::hash<std::uint8_t> {}(byte));
5322 }
5323
5324 return seed;
5325 }
5326
5327 default: // LCOV_EXCL_LINE
5328 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
5329 return 0; // LCOV_EXCL_LINE
5330 }
5331}
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: json.hpp:5240