2017-06-06 23:04:49 +08:00
|
|
|
#pragma once
|
|
|
|
|
2017-10-25 19:01:53 +08:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2017-06-06 23:04:49 +08:00
|
|
|
// TODO: implement better hash function
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class calculates hash of the data dynamically.
|
|
|
|
*/
|
|
|
|
class Hasher {
|
2017-10-25 19:01:53 +08:00
|
|
|
/** Prime number used in calculating hash. */
|
|
|
|
static constexpr uint64_t kPrime = 3137;
|
|
|
|
|
2017-06-06 23:04:49 +08:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Updates hash from given data.
|
2017-10-25 19:01:53 +08:00
|
|
|
*
|
|
|
|
* @param data - Data from which hash will be updated.
|
|
|
|
* @param n - Length of the data.
|
2017-06-06 23:04:49 +08:00
|
|
|
*/
|
|
|
|
void Update(const uint8_t *data, size_t n) {
|
2017-10-25 19:01:53 +08:00
|
|
|
for (size_t i = 0; i < n; ++i) hash_ = hash_ * kPrime + data[i] + 1;
|
2017-06-06 23:04:49 +08:00
|
|
|
}
|
2017-10-25 19:01:53 +08:00
|
|
|
|
|
|
|
/** Returns current hash value. */
|
2017-06-06 23:04:49 +08:00
|
|
|
uint64_t hash() const { return hash_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint64_t hash_ = 0;
|
|
|
|
};
|