mix.h 969 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. #ifndef UTIL_MIX_H_
  5. #define UTIL_MIX_H_
  6. #include <stddef.h>
  7. #include <limits>
  8. namespace re2 {
  9. // Silence "truncation of constant value" warning for kMul in 32-bit mode.
  10. // Since this is a header file, push and then pop to limit the scope.
  11. #ifdef _MSC_VER
  12. #pragma warning(push)
  13. #pragma warning(disable: 4309)
  14. #endif
  15. class HashMix {
  16. public:
  17. HashMix() : hash_(1) {}
  18. explicit HashMix(size_t val) : hash_(val + 83) {}
  19. void Mix(size_t val) {
  20. static const size_t kMul = static_cast<size_t>(0xdc3eb94af8ab4c93ULL);
  21. hash_ *= kMul;
  22. hash_ = ((hash_ << 19) |
  23. (hash_ >> (std::numeric_limits<size_t>::digits - 19))) + val;
  24. }
  25. size_t get() const { return hash_; }
  26. private:
  27. size_t hash_;
  28. };
  29. #ifdef _MSC_VER
  30. #pragma warning(pop)
  31. #endif
  32. } // namespace re2
  33. #endif // UTIL_MIX_H_