| 1 | // Copyright 2016 The RE2 Authors. All Rights Reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | #ifndef UTIL_MIX_H_ |
| 6 | #define UTIL_MIX_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <limits> |
| 10 | |
| 11 | namespace re2 { |
| 12 | |
| 13 | // Silence "truncation of constant value" warning for kMul in 32-bit mode. |
| 14 | // Since this is a header file, push and then pop to limit the scope. |
| 15 | #ifdef _MSC_VER |
| 16 | #pragma warning(push) |
| 17 | #pragma warning(disable: 4309) |
| 18 | #endif |
| 19 | |
| 20 | class HashMix { |
| 21 | public: |
| 22 | HashMix() : hash_(1) {} |
| 23 | explicit HashMix(size_t val) : hash_(val + 83) {} |
| 24 | void Mix(size_t val) { |
| 25 | static const size_t kMul = static_cast<size_t>(0xdc3eb94af8ab4c93ULL); |
| 26 | hash_ *= kMul; |
| 27 | hash_ = ((hash_ << 19) | |
| 28 | (hash_ >> (std::numeric_limits<size_t>::digits - 19))) + val; |
| 29 | } |
| 30 | size_t get() const { return hash_; } |
| 31 | private: |
| 32 | size_t hash_; |
| 33 | }; |
| 34 | |
| 35 | #ifdef _MSC_VER |
| 36 | #pragma warning(pop) |
| 37 | #endif |
| 38 | |
| 39 | } // namespace re2 |
| 40 | |
| 41 | #endif // UTIL_MIX_H_ |
| 42 | |