forked from HowardHinnant/hash_append
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement identity_hash (resolves HowardHinnant#4)
identity_hash is useful for wrappers that cache the hash keys of their wrapped objects.
- Loading branch information
1 parent
94868f0
commit de0bb14
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright Rein Halbersma 2016 | ||
// Distributed under the [Boost Software License, Version 1.0](http://www.boost.org/users/license.html). | ||
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
#ifndef IDENTITY_HASH_H | ||
#define IDENTITY_HASH_H | ||
|
||
#include "endian.h" // endian | ||
#include <cstddef> // size_t | ||
|
||
// namespace acme is used to demonstrate example code. It is not proposed. | ||
|
||
namespace acme { | ||
|
||
class identity_hash | ||
{ | ||
std::size_t m_state; | ||
public: | ||
static constexpr xstd::endian endian = xstd::endian::native; | ||
using result_type = std::size_t; | ||
|
||
constexpr void operator()(void const* key, std::size_t /* len */) // Throws: Nothing. | ||
{ | ||
// Pre-condition: callers must be of the form: identity_hash{}(&x, sizeof(x)). | ||
// In C++1z, we could annotate the len parameter with [[maybe_unused]] | ||
// and check the pre-condition in debug mode with assert(len == sizeof(result_type)). | ||
// In C++14, this requires disabling unused-parameter warnings. | ||
m_state = *static_cast<result_type const*>(key); | ||
} | ||
|
||
explicit constexpr operator result_type() const noexcept | ||
{ | ||
return m_state; | ||
} | ||
}; | ||
|
||
} // namespace acme | ||
|
||
#endif // IDENTITY_HASH_H |