2012-06-27 20:03:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkChecksum_DEFINED
|
|
|
|
#define SkChecksum_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkNoncopyable.h"
|
2020-06-02 15:34:24 +00:00
|
|
|
#include "include/private/SkOpts_spi.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/private/SkTLogic.h"
|
2012-06-27 20:03:16 +00:00
|
|
|
|
2022-03-03 16:03:48 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
2012-07-03 19:44:20 +00:00
|
|
|
class SkChecksum : SkNoncopyable {
|
|
|
|
public:
|
2014-07-10 13:03:46 +00:00
|
|
|
/**
|
|
|
|
* uint32_t -> uint32_t hash, useful for when you're about to trucate this hash but you
|
|
|
|
* suspect its low bits aren't well mixed.
|
|
|
|
*
|
|
|
|
* This is the Murmur3 finalizer.
|
|
|
|
*/
|
|
|
|
static uint32_t Mix(uint32_t hash) {
|
|
|
|
hash ^= hash >> 16;
|
|
|
|
hash *= 0x85ebca6b;
|
|
|
|
hash ^= hash >> 13;
|
|
|
|
hash *= 0xc2b2ae35;
|
|
|
|
hash ^= hash >> 16;
|
2015-01-28 21:28:53 +00:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* uint32_t -> uint32_t hash, useful for when you're about to trucate this hash but you
|
|
|
|
* suspect its low bits aren't well mixed.
|
|
|
|
*
|
|
|
|
* This version is 2-lines cheaper than Mix, but seems to be sufficient for the font cache.
|
|
|
|
*/
|
|
|
|
static uint32_t CheapMix(uint32_t hash) {
|
|
|
|
hash ^= hash >> 16;
|
|
|
|
hash *= 0x85ebca6b;
|
|
|
|
hash ^= hash >> 16;
|
2014-07-10 13:03:46 +00:00
|
|
|
return hash;
|
|
|
|
}
|
2012-07-03 19:44:20 +00:00
|
|
|
};
|
|
|
|
|
2015-03-20 20:48:42 +00:00
|
|
|
// SkGoodHash should usually be your first choice in hashing data.
|
|
|
|
// It should be both reasonably fast and high quality.
|
2015-10-15 19:23:01 +00:00
|
|
|
struct SkGoodHash {
|
|
|
|
template <typename K>
|
2020-04-30 11:45:25 +00:00
|
|
|
std::enable_if_t<sizeof(K) == 4, uint32_t> operator()(const K& k) const {
|
2015-03-20 20:48:42 +00:00
|
|
|
return SkChecksum::Mix(*(const uint32_t*)&k);
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:23:01 +00:00
|
|
|
template <typename K>
|
2020-04-30 11:45:25 +00:00
|
|
|
std::enable_if_t<sizeof(K) != 4, uint32_t> operator()(const K& k) const {
|
2016-08-08 16:06:27 +00:00
|
|
|
return SkOpts::hash_fn(&k, sizeof(K), 0);
|
2015-10-15 19:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t operator()(const SkString& k) const {
|
2016-08-08 16:06:27 +00:00
|
|
|
return SkOpts::hash_fn(k.c_str(), k.size(), 0);
|
2015-10-15 19:23:01 +00:00
|
|
|
}
|
2022-03-03 16:03:48 +00:00
|
|
|
|
|
|
|
uint32_t operator()(const std::string& k) const {
|
|
|
|
return SkOpts::hash_fn(k.c_str(), k.size(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t operator()(std::string_view k) const {
|
|
|
|
return SkOpts::hash_fn(k.data(), k.size(), 0);
|
|
|
|
}
|
2015-10-15 19:23:01 +00:00
|
|
|
};
|
2015-03-20 20:48:42 +00:00
|
|
|
|
2012-06-28 00:29:23 +00:00
|
|
|
#endif
|