CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/cityhash/city.h
Views: 1401
// Copyright (c) 2011 Google, Inc.1//2// Permission is hereby granted, free of charge, to any person obtaining a copy3// of this software and associated documentation files (the "Software"), to deal4// in the Software without restriction, including without limitation the rights5// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell6// copies of the Software, and to permit persons to whom the Software is7// furnished to do so, subject to the following conditions:8//9// The above copyright notice and this permission notice shall be included in10// all copies or substantial portions of the Software.11//12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,17// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN18// THE SOFTWARE.19//20// CityHash, by Geoff Pike and Jyrki Alakuijala21//22// http://code.google.com/p/cityhash/23//24// This file provides a few functions for hashing strings. All of them are25// high-quality functions in the sense that they pass standard tests such26// as Austin Appleby's SMHasher. They are also fast.27//28// For 64-bit x86 code, on short strings, we don't know of anything faster than29// CityHash64 that is of comparable quality. We believe our nearest competitor30// is Murmur3. For 64-bit x86 code, CityHash64 is an excellent choice for hash31// tables and most other hashing (excluding cryptography).32//33// For 64-bit x86 code, on long strings, the picture is more complicated.34// On many recent Intel CPUs, such as Nehalem, Westmere, Sandy Bridge, etc.,35// CityHashCrc128 appears to be faster than all competitors of comparable36// quality. CityHash128 is also good but not quite as fast. We believe our37// nearest competitor is Bob Jenkins' Spooky. We don't have great data for38// other 64-bit CPUs, but for long strings we know that Spooky is slightly39// faster than CityHash on some relatively recent AMD x86-64 CPUs, for example.40// Note that CityHashCrc128 is declared in citycrc.h.41//42// For 32-bit x86 code, we don't know of anything faster than CityHash32 that43// is of comparable quality. We believe our nearest competitor is Murmur3A.44// (On 64-bit CPUs, it is typically faster to use the other CityHash variants.)45//46// Functions in the CityHash family are not suitable for cryptography.47//48// Please see CityHash's README file for more details on our performance49// measurements and so on.50//51// WARNING: This code has been only lightly tested on big-endian platforms!52// It is known to work well on little-endian platforms that have a small penalty53// for unaligned reads, such as current Intel and AMD moderate-to-high-end CPUs.54// It should work on all 32-bit and 64-bit platforms that allow unaligned reads;55// bug reports are welcome.56//57// By the way, for some hash functions, given strings a and b, the hash58// of a+b is easily derived from the hashes of a and b. This property59// doesn't hold for any hash functions in this file.6061#ifndef CITY_HASH_H_62#define CITY_HASH_H_6364#include <stdlib.h> // for size_t.65#include <stdint.h>66#include <utility>6768typedef uint8_t uint8;69typedef uint32_t uint32;70typedef uint64_t uint64;71typedef std::pair<uint64, uint64> uint128;7273inline uint64 Uint128Low64(const uint128& x) { return x.first; }74inline uint64 Uint128High64(const uint128& x) { return x.second; }7576// Hash function for a byte array.77uint64 CityHash64(const char *buf, size_t len);7879// Hash function for a byte array. For convenience, a 64-bit seed is also80// hashed into the result.81uint64 CityHash64WithSeed(const char *buf, size_t len, uint64 seed);8283// Hash function for a byte array. For convenience, two seeds are also84// hashed into the result.85uint64 CityHash64WithSeeds(const char *buf, size_t len,86uint64 seed0, uint64 seed1);8788// Hash function for a byte array.89uint128 CityHash128(const char *s, size_t len);9091// Hash function for a byte array. For convenience, a 128-bit seed is also92// hashed into the result.93uint128 CityHash128WithSeed(const char *s, size_t len, uint128 seed);9495// Hash function for a byte array. Most useful in 32-bit binaries.96uint32 CityHash32(const char *buf, size_t len);9798// Hash 128 input bits down to 64 bits of output.99// This is intended to be a reasonably good hash function.100inline uint64 Hash128to64(const uint128& x) {101// Murmur-inspired hashing.102const uint64 kMul = 0x9ddfea08eb382d69ULL;103uint64 a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul;104a ^= (a >> 47);105uint64 b = (Uint128High64(x) ^ a) * kMul;106b ^= (b >> 47);107b *= kMul;108return b;109}110111#endif // CITY_HASH_H_112113114