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.1KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. accumulator += primes.select(accumulator) + byte;
  17. accumulator *= primes.select(byte);
  18. accumulator += accumulator >> 32;
  19. }
  20. return accumulator;
  21. }
  22. uint32_t mishmash(const unsigned char* buffer, size_t length) noexcept {
  23. uint64_t accumulator = masher::engine(buffer, length);
  24. return static_cast<uint32_t>(accumulator & 0x00000000ffffffff);
  25. }
  26. uint32_t mishmash(const std::string& s) noexcept {
  27. return mishmash((const unsigned char*) s.c_str(), s.length());
  28. }
  29. uint32_t mishmash(const std::vector<unsigned char>& v) noexcept {
  30. return mishmash(v.data(), v.size());
  31. }
  32. } // end of namespace codedweller