Files
poco/Foundation/include/Poco/Hash.h
Matej Kenda 8a4a2955d5 Use nullptr in C++ code (solves #4348) (#5043)
* chore(CppParser): 0, NULL --> nullptr

* chore(Crypto): 0, NULL --> nullptr

* chore(DNSSD): 0, NULL --> nullptr

* chore(Encodings): 0, NULL --> nullptr

* chore(CppUnit): Correct indentation.

* chore(Foundation): 0, NULL --> nullptr

* chore(CMake): Always warn about wrong nullptr usage when compiling with GCC or CLang

* chore(Net): 0, NULL --> nullptr

* chore(Foundation): 0, NULL --> nullptr

* chore(Data): 0, NULL --> nullptr

* chore(macOS): 0, NULL --> nullptr

* chore(XML): 0, NULL --> nullptr

* chore(Zip): 0, NULL --> nullptr

* chore(Util): 0, NULL --> nullptr

* chore(Net/NetSSL): 0, NULL --> nullptr

* chore(Bonjour): 0, NULL --> nullptr

* chore(MongoDB, Redis): 0, NULL --> nullptr

* chore(Poco): 0, NULL --> nullptr

* chore(Win32): 0, NULL --> nullptr

* chore(CMake): Only warn about nullptr when verbose warnings are enabled.

* Potential fix for code scanning alert no. 1634: Guarded Free

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>

* chore(Net): Fix warning reported by gitlab.

* chore(gitlab CI): attempt to clean to gain disk space on the runner.

* chore(gitlab CI): Run build with  --parallel 4, correct docker cleanup.

---------

Co-authored-by: Aleksandar Fabijanic <aleks-f@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
2025-10-30 15:20:53 +01:00

201 lines
3.3 KiB
C++

//
// Hash.h
//
// Library: Foundation
// Package: Hashing
// Module: Hash
//
// Definition of the Hash class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// hashRange Copyright 2005-2014 Daniel James.
// (Extracted from Boost 1.75.0 lib and adapted for poco on 2021-03-31)
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Foundation_Hash_INCLUDED
#define Foundation_Hash_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/Types.h"
#include <cstddef>
#if defined(_MSC_VER)
# define POCO_HASH_ROTL32(x, r) _rotl(x,r)
#else
# define POCO_HASH_ROTL32(x, r) (x << r) | (x >> (32 - r))
#endif
namespace Poco {
std::size_t Foundation_API hash(Int8 n);
std::size_t Foundation_API hash(UInt8 n);
std::size_t Foundation_API hash(Int16 n);
std::size_t Foundation_API hash(UInt16 n);
std::size_t Foundation_API hash(Int32 n);
std::size_t Foundation_API hash(UInt32 n);
std::size_t Foundation_API hash(Int64 n);
std::size_t Foundation_API hash(UInt64 n);
std::size_t Foundation_API hash(const std::string& str);
template <class T>
struct Hash
/// A generic hash function.
{
std::size_t operator () (T value) const
/// Returns the hash for the given value.
{
return Poco::hash(value);
}
};
//
// inlines
//
inline std::size_t hash(Int8 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(UInt8 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(Int16 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(UInt16 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(Int32 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(UInt32 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(Int64 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(UInt64 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
namespace Impl {
template <typename SizeT>
inline void hashCombine(SizeT& seed, SizeT value)
{
seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
inline void hashCombine(Poco::UInt32& h1, Poco::UInt32 k1)
{
const uint32_t c1 = 0xcc9e2d51;
const uint32_t c2 = 0x1b873593;
k1 *= c1;
k1 = POCO_HASH_ROTL32(k1,15);
k1 *= c2;
h1 ^= k1;
h1 = POCO_HASH_ROTL32(h1,13);
h1 = h1*5+0xe6546b64;
}
#if defined(POCO_PTR_IS_64_BIT) && !(defined(__GNUC__) && ULONG_MAX == 0xffffffff)
inline void hashCombine(Poco::UInt64& h, Poco::UInt64 k)
{
const Poco::UInt64 m = UINT64_C(0xc6a4a7935bd1e995);
const int r = 47;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
// Completely arbitrary number, to
// prevent zeros from hashing to 0.
h += 0xe6546b64;
}
#endif // POCO_PTR_IS_64_BIT
} // namespace Impl
template <class T>
inline void hashCombine(std::size_t& seed, T const& v)
{
Hash<T> hasher;
Impl::hashCombine(seed, hasher(v));
}
template <class It>
inline std::size_t hashRange(It first, It last)
{
std::size_t seed = 0;
for(; first != last; ++first)
{
hashCombine<typename std::iterator_traits<It>::value_type>(seed, *first);
}
return seed;
}
template <class It>
inline void hashRange(std::size_t& seed, It first, It last)
{
for(; first != last; ++first)
{
hashCombine<typename std::iterator_traits<It>::value_type>(seed, *first);
}
}
} // namespace Poco
#endif // Foundation_Hash_INCLUDED