// mishmash.cpp // // Copyright (C) 2019-2020 MicroNeil Research Corporation. // // This software is released under the MIT license. See LICENSE.TXT. // // Mishmash is a non-cryptographic hash optimized for short strings. #include "mishmash.hpp" namespace codedweller { uint64_t masher::engine( const unsigned char* buffer, size_t length, uint64_t accumulator, const masher::PrimeSet& primes) noexcept { for(size_t index = 0; index < length; index++) { unsigned char byte = buffer[index]; uint64_t accumulator1 = primes.select(accumulator) + byte; uint64_t accumulator2 = ~accumulator * primes.select(byte); uint64_t accumulator3 = accumulator >> (32 + ((byte & 0x1F) ^ (byte >> 5))); accumulator = accumulator1 + accumulator2 + accumulator3; } return accumulator; } uint32_t mishmash(const unsigned char* buffer, size_t length) noexcept { uint64_t accumulator = masher::engine(buffer, length); return static_cast(accumulator & 0x00000000ffffffff); } uint32_t mishmash(const std::string& s) noexcept { return mishmash((const unsigned char*) s.c_str(), s.length()); } uint32_t mishmash(const std::vector& v) noexcept { return mishmash(v.data(), v.size()); } } // end of namespace codedweller