Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / include / private / SkChecksum.h
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8 #ifndef SkChecksum_DEFINED
9 #define SkChecksum_DEFINED
10
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"
16
17 #include <string>
18 #include <string_view>
19
20 class SkChecksum : SkNoncopyable {
21 public:
22     /**
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.
25      *
26      * This is the Murmur3 finalizer.
27      */
28     static uint32_t Mix(uint32_t hash) {
29         hash ^= hash >> 16;
30         hash *= 0x85ebca6b;
31         hash ^= hash >> 13;
32         hash *= 0xc2b2ae35;
33         hash ^= hash >> 16;
34         return hash;
35     }
36
37     /**
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.
40      *
41      *  This version is 2-lines cheaper than Mix, but seems to be sufficient for the font cache.
42      */
43     static uint32_t CheapMix(uint32_t hash) {
44         hash ^= hash >> 16;
45         hash *= 0x85ebca6b;
46         hash ^= hash >> 16;
47         return hash;
48     }
49 };
50
51 // SkGoodHash should usually be your first choice in hashing data.
52 // It should be both reasonably fast and high quality.
53 struct SkGoodHash {
54     template <typename K>
55     std::enable_if_t<sizeof(K) == 4, uint32_t> operator()(const K& k) const {
56         return SkChecksum::Mix(*(const uint32_t*)&k);
57     }
58
59     template <typename 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);
62     }
63
64     uint32_t operator()(const SkString& k) const {
65         return SkOpts::hash_fn(k.c_str(), k.size(), 0);
66     }
67
68     uint32_t operator()(const std::string& k) const {
69         return SkOpts::hash_fn(k.c_str(), k.size(), 0);
70     }
71
72     uint32_t operator()(std::string_view k) const {
73         return SkOpts::hash_fn(k.data(), k.size(), 0);
74     }
75 };
76
77 #endif