rippled
Loading...
Searching...
No Matches
xxhasher.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of Beast: https://github.com/vinniefalco/Beast
4 Copyright 2014, Vinnie Falco <vinnie.falco@gmail.com>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef BEAST_HASH_XXHASHER_H_INCLUDED
21#define BEAST_HASH_XXHASHER_H_INCLUDED
22
23#include <boost/endian/conversion.hpp>
24#include <xxhash.h>
25
26#include <cstddef>
27#include <new>
28#include <type_traits>
29
30namespace beast {
31
33{
34private:
35 // requires 64-bit std::size_t
36 static_assert(sizeof(std::size_t) == 8, "");
37
38 XXH3_state_t* state_;
39
40 static XXH3_state_t*
42 {
43 auto ret = XXH3_createState();
44 if (ret == nullptr)
45 throw std::bad_alloc();
46 return ret;
47 }
48
49public:
51
52 static constexpr auto const endian = boost::endian::order::native;
53
54 xxhasher(xxhasher const&) = delete;
56 operator=(xxhasher const&) = delete;
57
59 {
61 XXH3_64bits_reset(state_);
62 }
63
64 ~xxhasher() noexcept
65 {
66 XXH3_freeState(state_);
67 }
68
69 template <
70 class Seed,
72 explicit xxhasher(Seed seed)
73 {
75 XXH3_64bits_reset_withSeed(state_, seed);
76 }
77
78 template <
79 class Seed,
81 xxhasher(Seed seed, Seed)
82 {
84 XXH3_64bits_reset_withSeed(state_, seed);
85 }
86
87 void
88 operator()(void const* key, std::size_t len) noexcept
89 {
90 XXH3_64bits_update(state_, key, len);
91 }
92
93 explicit
94 operator std::size_t() noexcept
95 {
96 return XXH3_64bits_digest(state_);
97 }
98};
99
100} // namespace beast
101
102#endif
XXH3_state_t * state_
Definition: xxhasher.h:38
xxhasher(Seed seed, Seed)
Definition: xxhasher.h:81
static constexpr auto const endian
Definition: xxhasher.h:52
xxhasher & operator=(xxhasher const &)=delete
static XXH3_state_t * allocState()
Definition: xxhasher.h:41
~xxhasher() noexcept
Definition: xxhasher.h:64
void operator()(void const *key, std::size_t len) noexcept
Definition: xxhasher.h:88
xxhasher(xxhasher const &)=delete
xxhasher(Seed seed)
Definition: xxhasher.h:72