2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkChecksum_DEFINED
9 #define SkChecksum_DEFINED
11 #include "include/core/SkString.h"
12 #include "include/core/SkTypes.h"
13 #include "include/private/SkNoncopyable.h"
14 #include "include/private/SkOpts_spi.h"
15 #include "include/private/SkTLogic.h"
18 #include <string_view>
20 class SkChecksum : SkNoncopyable {
23 * uint32_t -> uint32_t hash, useful for when you're about to trucate this hash but you
24 * suspect its low bits aren't well mixed.
26 * This is the Murmur3 finalizer.
28 static uint32_t Mix(uint32_t hash) {
38 * uint32_t -> uint32_t hash, useful for when you're about to trucate this hash but you
39 * suspect its low bits aren't well mixed.
41 * This version is 2-lines cheaper than Mix, but seems to be sufficient for the font cache.
43 static uint32_t CheapMix(uint32_t hash) {
51 // SkGoodHash should usually be your first choice in hashing data.
52 // It should be both reasonably fast and high quality.
55 std::enable_if_t<sizeof(K) == 4, uint32_t> operator()(const K& k) const {
56 return SkChecksum::Mix(*(const uint32_t*)&k);
60 std::enable_if_t<sizeof(K) != 4, uint32_t> operator()(const K& k) const {
61 return SkOpts::hash_fn(&k, sizeof(K), 0);
64 uint32_t operator()(const SkString& k) const {
65 return SkOpts::hash_fn(k.c_str(), k.size(), 0);
68 uint32_t operator()(const std::string& k) const {
69 return SkOpts::hash_fn(k.c_str(), k.size(), 0);
72 uint32_t operator()(std::string_view k) const {
73 return SkOpts::hash_fn(k.data(), k.size(), 0);