2016-11-17 18:35:06 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2016-11-17 18:35:06 +11:00
|
|
|
*
|
2018-01-19 11:30:09 +11:00
|
|
|
* Copyright 2016-2018 Danny Robson <danny@nerdcruft.net>
|
2016-11-17 18:35:06 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CRUFT_UTIL_HASH_XXHASH_HPP
|
|
|
|
#define CRUFT_UTIL_HASH_XXHASH_HPP
|
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
#include "../view.hpp"
|
|
|
|
|
2016-11-17 18:35:06 +11:00
|
|
|
#include <cstdint>
|
|
|
|
#include <type_traits>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::hash {
|
2018-01-18 16:29:06 +11:00
|
|
|
template <typename WordT>
|
2016-11-17 18:35:06 +11:00
|
|
|
class xxhash {
|
|
|
|
public:
|
2018-01-18 16:29:06 +11:00
|
|
|
static_assert (std::is_same<WordT,std::uint32_t>::value || std::is_same<WordT,std::uint64_t>::value);
|
|
|
|
using digest_t = WordT;
|
|
|
|
using word_t = WordT;
|
|
|
|
static constexpr int block_bytes = 4 * sizeof (word_t);
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-18 16:29:06 +11:00
|
|
|
static constexpr word_t DEFAULT_SEED = 0;
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-01-18 16:29:06 +11:00
|
|
|
xxhash (word_t seed = DEFAULT_SEED);
|
2016-11-17 18:35:06 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
digest_t operator() (const cruft::view<const uint8_t*> data);
|
2016-11-17 18:35:06 +11:00
|
|
|
|
|
|
|
private:
|
2018-01-18 16:29:06 +11:00
|
|
|
word_t m_seed;
|
2016-11-17 18:35:06 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
using xxhash32 = xxhash<uint32_t>;
|
|
|
|
using xxhash64 = xxhash<uint64_t>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|