2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
#include "util/hash.h"
|
|
|
|
|
2020-04-29 19:59:39 +00:00
|
|
|
#include <cstring>
|
2019-05-02 11:01:00 -07:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "util/coding.h"
|
|
|
|
|
2013-02-06 18:03:32 -08:00
|
|
|
// The FALLTHROUGH_INTENDED macro can be used to annotate implicit fall-through
|
|
|
|
// between switch labels. The real definition should be provided externally.
|
|
|
|
// This one is a fallback version for unsupported compilers.
|
|
|
|
#ifndef FALLTHROUGH_INTENDED
|
2019-05-02 11:01:00 -07:00
|
|
|
#define FALLTHROUGH_INTENDED \
|
|
|
|
do { \
|
|
|
|
} while (0)
|
2013-02-06 18:03:32 -08:00
|
|
|
#endif
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
uint32_t Hash(const char* data, size_t n, uint32_t seed) {
|
|
|
|
// Similar to murmur hash
|
|
|
|
const uint32_t m = 0xc6a4a793;
|
|
|
|
const uint32_t r = 24;
|
|
|
|
const char* limit = data + n;
|
|
|
|
uint32_t h = seed ^ (n * m);
|
|
|
|
|
|
|
|
// Pick up four bytes at a time
|
|
|
|
while (data + 4 <= limit) {
|
|
|
|
uint32_t w = DecodeFixed32(data);
|
|
|
|
data += 4;
|
|
|
|
h += w;
|
|
|
|
h *= m;
|
|
|
|
h ^= (h >> 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick up remaining bytes
|
|
|
|
switch (limit - data) {
|
|
|
|
case 3:
|
2019-05-28 10:17:03 -07:00
|
|
|
h += static_cast<uint8_t>(data[2]) << 16;
|
2013-02-06 18:03:32 -08:00
|
|
|
FALLTHROUGH_INTENDED;
|
2011-03-18 22:37:00 +00:00
|
|
|
case 2:
|
2019-05-28 10:17:03 -07:00
|
|
|
h += static_cast<uint8_t>(data[1]) << 8;
|
2013-02-06 18:03:32 -08:00
|
|
|
FALLTHROUGH_INTENDED;
|
2011-03-18 22:37:00 +00:00
|
|
|
case 1:
|
2019-05-28 10:17:03 -07:00
|
|
|
h += static_cast<uint8_t>(data[0]);
|
2011-03-18 22:37:00 +00:00
|
|
|
h *= m;
|
|
|
|
h ^= (h >> r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|