作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

45 rader
980 B

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include <string.h>
  5. #include "util/coding.h"
  6. #include "util/hash.h"
  7. namespace leveldb {
  8. uint32_t Hash(const char* data, size_t n, uint32_t seed) {
  9. // Similar to murmur hash
  10. const uint32_t m = 0xc6a4a793;
  11. const uint32_t r = 24;
  12. const char* limit = data + n;
  13. uint32_t h = seed ^ (n * m);
  14. // Pick up four bytes at a time
  15. while (data + 4 <= limit) {
  16. uint32_t w = DecodeFixed32(data);
  17. data += 4;
  18. h += w;
  19. h *= m;
  20. h ^= (h >> 16);
  21. }
  22. // Pick up remaining bytes
  23. switch (limit - data) {
  24. case 3:
  25. h += data[2] << 16;
  26. // fall through
  27. case 2:
  28. h += data[1] << 8;
  29. // fall through
  30. case 1:
  31. h += data[0];
  32. h *= m;
  33. h ^= (h >> r);
  34. break;
  35. }
  36. return h;
  37. }
  38. } // namespace leveldb