city.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // https://code.google.com/p/cityhash/
  16. //
  17. // This file provides a few functions for hashing strings. All of them are
  18. // high-quality functions in the sense that they pass standard tests such
  19. // as Austin Appleby's SMHasher. They are also fast.
  20. //
  21. // For 64-bit x86 code, on short strings, we don't know of anything faster than
  22. // CityHash64 that is of comparable quality. We believe our nearest competitor
  23. // is Murmur3. For 64-bit x86 code, CityHash64 is an excellent choice for hash
  24. // tables and most other hashing (excluding cryptography).
  25. //
  26. // For 32-bit x86 code, we don't know of anything faster than CityHash32 that
  27. // is of comparable quality. We believe our nearest competitor is Murmur3A.
  28. // (On 64-bit CPUs, it is typically faster to use the other CityHash variants.)
  29. //
  30. // Functions in the CityHash family are not suitable for cryptography.
  31. //
  32. // Please see CityHash's README file for more details on our performance
  33. // measurements and so on.
  34. //
  35. // WARNING: This code has been only lightly tested on big-endian platforms!
  36. // It is known to work well on little-endian platforms that have a small penalty
  37. // for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs.
  38. // It should work on all 32-bit and 64-bit platforms that allow unaligned reads;
  39. // bug reports are welcome.
  40. //
  41. // By the way, for some hash functions, given strings a and b, the hash
  42. // of a+b is easily derived from the hashes of a and b. This property
  43. // doesn't hold for any hash functions in this file.
  44. #ifndef ABSL_HASH_INTERNAL_CITY_H_
  45. #define ABSL_HASH_INTERNAL_CITY_H_
  46. #include <stdint.h>
  47. #include <stdlib.h> // for size_t.
  48. #include <utility>
  49. #include "absl/base/config.h"
  50. namespace absl {
  51. ABSL_NAMESPACE_BEGIN
  52. namespace hash_internal {
  53. // Hash function for a byte array.
  54. uint64_t CityHash64(const char *s, size_t len);
  55. // Hash function for a byte array. For convenience, a 64-bit seed is also
  56. // hashed into the result.
  57. uint64_t CityHash64WithSeed(const char *s, size_t len, uint64_t seed);
  58. // Hash function for a byte array. For convenience, two seeds are also
  59. // hashed into the result.
  60. uint64_t CityHash64WithSeeds(const char *s, size_t len, uint64_t seed0,
  61. uint64_t seed1);
  62. // Hash function for a byte array. Most useful in 32-bit binaries.
  63. uint32_t CityHash32(const char *s, size_t len);
  64. } // namespace hash_internal
  65. ABSL_NAMESPACE_END
  66. } // namespace absl
  67. #endif // ABSL_HASH_INTERNAL_CITY_H_