2011-03-19 06:37:00 +08: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-03 02:01:00 +08:00
|
|
|
#include "util/hash.h"
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
#include <string.h>
|
2019-05-03 02:01:00 +08:00
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "util/coding.h"
|
|
|
|
|
2013-02-07 10: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-03 02:01:00 +08:00
|
|
|
#define FALLTHROUGH_INTENDED \
|
|
|
|
do { \
|
|
|
|
} while (0)
|
2013-02-07 10:03:32 +08:00
|
|
|
#endif
|
|
|
|
|
2011-03-19 06:37:00 +08: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-29 01:17:03 +08:00
|
|
|
h += static_cast<uint8_t>(data[2]) << 16;
|
2013-02-07 10:03:32 +08:00
|
|
|
FALLTHROUGH_INTENDED;
|
2011-03-19 06:37:00 +08:00
|
|
|
case 2:
|
2019-05-29 01:17:03 +08:00
|
|
|
h += static_cast<uint8_t>(data[1]) << 8;
|
2013-02-07 10:03:32 +08:00
|
|
|
FALLTHROUGH_INTENDED;
|
2011-03-19 06:37:00 +08:00
|
|
|
case 1:
|
2019-05-29 01:17:03 +08:00
|
|
|
h += static_cast<uint8_t>(data[0]);
|
2011-03-19 06:37:00 +08:00
|
|
|
h *= m;
|
|
|
|
h ^= (h >> r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace leveldb
|