MurmurHash2.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. MurmurHash2 is a very fast noncryptographic 32/64-bit hash
  27. Algorithm by Austin Appleby <aappleby@gmail.com>
  28. http://murmurhash.googlepages.com/
  29. */
  30. #ifndef CAT_MURMUR_HASH_2_HPP
  31. #define CAT_MURMUR_HASH_2_HPP
  32. #include <cat/Platform.hpp>
  33. namespace cat {
  34. // NOTE: Result is NOT endian-neutral. Use getLE().
  35. u32 MurmurHash32(const void *key, int bytes, u32 seed);
  36. u64 MurmurHash64(const void *key, int bytes, u64 seed);
  37. class IncrementalMurmurHash32
  38. {
  39. u32 _hash, _tail, _count, _size;
  40. static const u32 M = 0x5bd1e995;
  41. static const u32 R = 24;
  42. public:
  43. void Begin(u32 seed = 0);
  44. void Add(const void *data, int bytes);
  45. u32 End();
  46. };
  47. class IncrementalMurmurHash64
  48. {
  49. u32 _count;
  50. u64 _hash, _tail, _size;
  51. static const u64 M = 0xc6a4a7935bd1e995ULL;
  52. static const u64 R = 47;
  53. public:
  54. void Begin(u64 seed = 0);
  55. void Add(const void *data, int bytes);
  56. u64 End();
  57. };
  58. } // namespace cat
  59. #endif // CAT_MURMUR_HASH_2_HPP
粤ICP备19079148号