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
25#include <xxhash.h>
26
27#include <cstddef>
28#include <new>
29#include <type_traits>
30
31namespace beast {
32
34{
35private:
36 // requires 64-bit std::size_t
37 static_assert(sizeof(std::size_t) == 8, "");
38
39 XXH3_state_t* state_;
40
41 static XXH3_state_t*
43 {
44 auto ret = XXH3_createState();
45 if (ret == nullptr)
46 throw std::bad_alloc();
47 return ret;
48 }
49
50public:
52
53 static constexpr auto const endian = boost::endian::order::native;
54
55 xxhasher(xxhasher const&) = delete;
57 operator=(xxhasher const&) = delete;
58
60 {
62 XXH3_64bits_reset(state_);
63 }
64
65 ~xxhasher() noexcept
66 {
67 XXH3_freeState(state_);
68 }
69
70 template <
71 class Seed,
73 explicit xxhasher(Seed seed)
74 {
76 XXH3_64bits_reset_withSeed(state_, seed);
77 }
78
79 template <
80 class Seed,
82 xxhasher(Seed seed, Seed)
83 {
85 XXH3_64bits_reset_withSeed(state_, seed);
86 }
87
88 void
89 operator()(void const* key, std::size_t len) noexcept
90 {
91 XXH3_64bits_update(state_, key, len);
92 }
93
94 explicit
95 operator std::size_t() noexcept
96 {
97 return XXH3_64bits_digest(state_);
98 }
99};
100
101} // namespace beast
102
103#endif
XXH3_state_t * state_
Definition: xxhasher.h:39
xxhasher(Seed seed, Seed)
Definition: xxhasher.h:82
static constexpr auto const endian
Definition: xxhasher.h:53
xxhasher & operator=(xxhasher const &)=delete
static XXH3_state_t * allocState()
Definition: xxhasher.h:42
~xxhasher() noexcept
Definition: xxhasher.h:65
void operator()(void const *key, std::size_t len) noexcept
Definition: xxhasher.h:89
xxhasher(xxhasher const &)=delete
xxhasher(Seed seed)
Definition: xxhasher.h:73