You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mishmash.cpp 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // mishmash.cpp
  2. //
  3. // Copyright (C) 2019-2020 MicroNeil Research Corporation.
  4. //
  5. // This software is released under the MIT license. See LICENSE.TXT.
  6. //
  7. // Mishmash is a non-cryptographic hash optimized for short strings.
  8. #include "mishmash.hpp"
  9. namespace codedweller {
  10. uint64_t masher::engine(
  11. const unsigned char* buffer, size_t length,
  12. uint64_t accumulator,
  13. const masher::PrimeSet& primes) noexcept {
  14. for(size_t index = 0; index < length; index++) {
  15. unsigned char byte = buffer[index];
  16. uint64_t accumulator1 = primes.select(accumulator) + byte;
  17. uint64_t accumulator2 = ~accumulator * primes.select(byte);
  18. uint64_t accumulator3 = accumulator >> (32 + ((byte & 0x1F) ^ (byte >> 5)));
  19. accumulator = accumulator1 + accumulator2 + accumulator3;
  20. }
  21. return accumulator;
  22. }
  23. uint32_t mishmash(const unsigned char* buffer, size_t length) noexcept {
  24. uint64_t accumulator = masher::engine(buffer, length);
  25. return static_cast<uint32_t>(accumulator & 0x00000000ffffffff);
  26. }
  27. uint32_t mishmash(const std::string& s) noexcept {
  28. return mishmash((const unsigned char*) s.c_str(), s.length());
  29. }
  30. uint32_t mishmash(const std::vector<unsigned char>& v) noexcept {
  31. return mishmash(v.data(), v.size());
  32. }
  33. } // end of namespace codedweller