-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStreamHasher.h
114 lines (99 loc) · 2.87 KB
/
StreamHasher.h
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* ai-utils -- C++ Core utilities
*
* @file
* @brief Definition of class HasherStreamBuf and StreamHasher.
*
* @Copyright (C) 2019 Carlo Wood.
*
* pub dsa3072/C155A4EEE4E527A2 2018-08-16 Carlo Wood (CarloWood on Libera) <carlo@alinoe.com>
* fingerprint: 8020 B266 6305 EE2F D53E 6827 C155 A4EE E4E5 27A2
*
* This file is part of ai-utils.
*
* ai-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ai-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ai-utils. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
// Calculate the hash of a stream of characters.
//
// Example usage:
//
// utils::StreamHasher hasher;
// utils::RandomStreamBuf random_streambuf(1000, 'A', 'Z');
// hasher << &random_streambuf;
// ASSERT(hasher.digest() == 0xf373022bdeab5158);
#include <ostream>
#include <array>
#include <algorithm>
#include <tuple>
#include <boost/functional/hash.hpp>
namespace utils {
class HasherStreamBuf : public std::streambuf
{
private:
size_t m_hash;
std::array<char, 64> m_buf; // The resulting hash value is a function of the size of this array!
static constexpr size_t bufsize = std::tuple_size_v<decltype(m_buf)>;
void add_and_reset_put_area()
{
if (pptr() > pbase())
boost::hash_combine(m_hash, boost::hash_range(pbase(), pptr()));
setp(&m_buf[0], &m_buf[bufsize]);
}
protected:
int_type overflow(int_type c) override
{
if (c != EOF)
{
if (pptr() == epptr())
add_and_reset_put_area();
*pptr() = c;
pbump(1);
}
return 0;
}
public:
HasherStreamBuf() : m_hash(0) { setp(&m_buf[0], &m_buf[bufsize]); }
size_t hash()
{
add_and_reset_put_area();
return m_hash;
}
struct size_hash_pair_t {
size_t size;
size_t hash;
};
// For streams with characters in the range ['A', 'Z'].
static constexpr std::array<size_hash_pair_t, 9> size_hash_pairs = {{
{ 1, 0xaedc04cfa2e5b999 },
{ 10, 0xa32fa7216c0d9b4b },
{ 100, 0xd82a1e09aeb1383 },
{ 1000, 0xf373022bdeab5158 },
{ 10000, 0x345360b3a8fa2b73 },
{ 100000, 0x4755f0873f1b649 },
{ 1000000, 0x1e6fca364672c0d },
{ 10000000, 0xd8ef85366ad39522 },
{ 100000000, 0x737dddb5f390f1aa }
}};
};
class StreamHasher : public std::ostream
{
private:
HasherStreamBuf m_streambuf;
public:
StreamHasher() { rdbuf(&m_streambuf); }
~StreamHasher() { }
size_t digest() { return m_streambuf.hash(); }
};
} // namespace utils