forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxx_hasher.hh
56 lines (44 loc) · 1.2 KB
/
xx_hasher.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Copyright (C) 2018-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include "bytes_fwd.hh"
#include "utils/serialization.hh"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#include <xxhash.h>
#pragma GCC diagnostic pop
#include <array>
class xx_hasher {
static constexpr size_t digest_size = 16;
XXH64_state_t _state;
public:
explicit xx_hasher(uint64_t seed = 0) noexcept {
XXH64_reset(&_state, seed);
}
void update(const char* ptr, size_t length) noexcept {
XXH64_update(&_state, ptr, length);
}
bytes finalize() {
bytes digest{bytes::initialized_later(), digest_size};
serialize_to(digest.begin());
return digest;
}
std::array<uint8_t, digest_size> finalize_array() {
std::array<uint8_t, digest_size> digest;
serialize_to(digest.begin());
return digest;
}
uint64_t finalize_uint64() {
return XXH64_digest(&_state);
}
private:
template<typename OutIterator>
void serialize_to(OutIterator&& out) {
serialize_int64(out, 0);
serialize_int64(out, finalize_uint64());
}
};