Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

mishmash.hpp 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // mishmash.hpp//
  2. // Copyright (C) 2019-2020 MicroNeil Research Corporation.
  3. //
  4. // This software is released under the MIT license. See LICENSE.TXT.
  5. //
  6. // Mishamash is a non-cryptographic hash optimized for short strings.
  7. #pragma once
  8. #include <string>
  9. #include <vector>
  10. namespace codedweller {
  11. namespace masher {
  12. struct PrimeSet {
  13. uint32_t primes[256];
  14. uint32_t select(size_t n) const { return primes[n & 0xff]; }
  15. };
  16. const PrimeSet Primes {
  17. 4294961843, 4294961861, 4294961863, 4294961873, 4294961893, 4294961897, 4294961921, 4294961927,
  18. 4294961941, 4294961959, 4294961963, 4294962019, 4294962047, 4294962079, 4294962137, 4294962151,
  19. 4294962211, 4294962223, 4294962233, 4294962271, 4294962277, 4294962299, 4294962313, 4294962341,
  20. 4294962349, 4294962367, 4294962377, 4294962389, 4294962391, 4294962401, 4294962409, 4294962449,
  21. 4294962473, 4294962499, 4294962533, 4294962541, 4294962589, 4294962619, 4294962629, 4294962641,
  22. 4294962653, 4294962689, 4294962691, 4294962703, 4294962719, 4294962731, 4294962751, 4294962757,
  23. 4294962779, 4294962809, 4294962817, 4294962827, 4294962853, 4294962887, 4294962899, 4294962911,
  24. 4294962929, 4294962953, 4294963039, 4294963051, 4294963093, 4294963097, 4294963111, 4294963117,
  25. 4294963171, 4294963237, 4294963291, 4294963313, 4294963333, 4294963349, 4294963369, 4294963427,
  26. 4294963429, 4294963459, 4294963499, 4294963523, 4294963537, 4294963553, 4294963571, 4294963583,
  27. 4294963619, 4294963637, 4294963639, 4294963643, 4294963667, 4294963681, 4294963723, 4294963747,
  28. 4294963781, 4294963787, 4294963847, 4294963853, 4294963891, 4294963901, 4294963921, 4294963943,
  29. 4294963957, 4294963987, 4294963993, 4294964017, 4294964027, 4294964029, 4294964039, 4294964081,
  30. 4294964123, 4294964131, 4294964159, 4294964173, 4294964203, 4294964207, 4294964209, 4294964213,
  31. 4294964221, 4294964239, 4294964249, 4294964257, 4294964263, 4294964281, 4294964287, 4294964309,
  32. 4294964327, 4294964341, 4294964381, 4294964419, 4294964437, 4294964441, 4294964461, 4294964489,
  33. 4294964491, 4294964521, 4294964537, 4294964543, 4294964561, 4294964579, 4294964599, 4294964621,
  34. 4294964633, 4294964683, 4294964689, 4294964749, 4294964771, 4294964789, 4294964809, 4294964827,
  35. 4294964833, 4294964879, 4294964887, 4294964893, 4294964897, 4294964899, 4294964903, 4294964923,
  36. 4294964929, 4294964939, 4294964959, 4294964969, 4294964977, 4294964981, 4294965019, 4294965131,
  37. 4294965137, 4294965151, 4294965161, 4294965193, 4294965203, 4294965229, 4294965251, 4294965263,
  38. 4294965307, 4294965313, 4294965331, 4294965347, 4294965361, 4294965383, 4294965413, 4294965457,
  39. 4294965461, 4294965487, 4294965529, 4294965581, 4294965601, 4294965613, 4294965617, 4294965641,
  40. 4294965659, 4294965671, 4294965673, 4294965679, 4294965683, 4294965691, 4294965721, 4294965733,
  41. 4294965737, 4294965757, 4294965767, 4294965793, 4294965821, 4294965839, 4294965841, 4294965847,
  42. 4294965887, 4294965911, 4294965937, 4294965949, 4294965967, 4294965971, 4294965977, 4294966001,
  43. 4294966007, 4294966043, 4294966073, 4294966087, 4294966099, 4294966121, 4294966129, 4294966153,
  44. 4294966163, 4294966177, 4294966187, 4294966217, 4294966231, 4294966237, 4294966243, 4294966297,
  45. 4294966337, 4294966367, 4294966373, 4294966427, 4294966441, 4294966447, 4294966477, 4294966553,
  46. 4294966583, 4294966591, 4294966619, 4294966639, 4294966651, 4294966657, 4294966661, 4294966667,
  47. 4294966769, 4294966813, 4294966829, 4294966877, 4294966909, 4294966927, 4294966943, 4294966981,
  48. 4294966997, 4294967029, 4294967087, 4294967111, 4294967143, 4294967161, 4294967189, 4294967197
  49. };
  50. uint64_t engine(
  51. const unsigned char* buffer, size_t length,
  52. uint64_t accumulator = 0,
  53. const PrimeSet& primes = Primes
  54. ) noexcept;
  55. }
  56. uint32_t mishmash(const unsigned char* buffer, size_t length) noexcept;
  57. uint32_t mishmash(const std::string &s) noexcept;
  58. uint32_t mishmash(const std::vector<unsigned char>& v) noexcept;
  59. } // End namespace codedweller