瀏覽代碼

improved collision rate and extended interface

master
madscientist 4 年之前
父節點
當前提交
fcce969c41
共有 2 個文件被更改,包括 13 次插入8 次删除
  1. 12
    8
      mishmash.cpp
  2. 1
    0
      mishmash.hpp

+ 12
- 8
mishmash.cpp 查看文件

@@ -10,7 +10,7 @@

namespace codedweller {

uint32_t primes[256] {
const uint32_t primes[256] {
1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061,
1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109,
1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181,
@@ -49,18 +49,22 @@ uint32_t primes[256] {
};


inline size_t mod256(size_t n) noexcept { return (n & 0xff); }
inline size_t selectedPrime(size_t n) noexcept { return primes[n &0xff]; }

uint32_t mishmash(const unsigned char* buffer, size_t length) noexcept {
uint64_t accumulator = selectedPrime(length);
uint64_t mishmash(const unsigned char* buffer, size_t length, uint64_t accumulator) noexcept {
for(size_t index = 0; index < length; index++) {
unsigned char byte = buffer[index];
accumulator += selectedPrime(index + accumulator);
accumulator *= selectedPrime(byte + accumulator);
accumulator += selectedPrime(byte + accumulator);
accumulator *= selectedPrime(accumulator);
accumulator += accumulator >> 32;
accumulator &= 0x00000fffffffffff;
accumulator &= 0x000fffffffffffff;
}
return static_cast<uint32_t>(accumulator);
return accumulator;
}

uint32_t mishmash(const unsigned char* buffer, size_t length) noexcept {
uint64_t accumulator = mishmash(buffer, length, 0);
return static_cast<uint32_t>(accumulator & 0x00000000ffffffff);
}

uint32_t mishmash(const std::string& s) noexcept {

+ 1
- 0
mishmash.hpp 查看文件

@@ -11,6 +11,7 @@

namespace codedweller {

uint64_t mishmash(const unsigned char* buffer, size_t length, uint64_t accumulator) noexcept;
uint32_t mishmash(const unsigned char* buffer, size_t length) noexcept;
uint32_t mishmash(const std::string &s) noexcept;
uint32_t mishmash(const std::vector<unsigned char>& v) noexcept;

Loading…
取消
儲存