summaryrefslogtreecommitdiff
path: root/include/minikin/Hasher.h
blob: dcfdd0b951cca6f251ce8dd7c21b684f6b795f8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MINIKIN_HASHER_H
#define MINIKIN_HASHER_H

#include <cstdint>
#include <string>

#include "minikin/FontFeature.h"
#include "minikin/Macros.h"

namespace minikin {

// Provides a Jenkins hash implementation.
class Hasher {
public:
    Hasher() : mHash(0u) {}

    IGNORE_INTEGER_OVERFLOW inline Hasher& update(uint32_t data) {
        mHash += data;
        mHash += (mHash << 10);
        mHash ^= (mHash >> 6);
        return *this;
    }

    inline Hasher& update(int32_t data) {
        update(static_cast<uint32_t>(data));
        return *this;
    }

    inline Hasher& update(uint64_t data) {
        update(static_cast<uint32_t>(data));
        update(static_cast<uint32_t>(data >> 32));
        return *this;
    }

    inline Hasher& update(float data) {
        union {
            float f;
            uint32_t i;
        } bits;
        bits.f = data;
        return update(bits.i);
    }

    inline Hasher& update(const std::vector<FontFeature>& features) {
        uint32_t size = features.size();
        update(size);
        for (const FontFeature& feature : features) {
            update(feature);
        }
        return *this;
    }

    inline Hasher& update(const FontFeature& feature) {
        update(feature.tag);
        update(feature.value);
        return *this;
    }

    inline Hasher& updateShorts(const uint16_t* data, uint32_t length) {
        update(length);
        uint32_t i;
        for (i = 0; i < (length & -2); i += 2) {
            update((uint32_t)data[i] | ((uint32_t)data[i + 1] << 16));
        }
        if (length & 1) {
            update((uint32_t)data[i]);
        }
        return *this;
    }

    inline Hasher& updateString(const std::string& str) {
        uint32_t size = str.size();
        update(size);
        uint32_t i;
        for (i = 0; i < (size & -4); i += 4) {
            update((uint32_t)str[i] | ((uint32_t)str[i + 1] << 8) | ((uint32_t)str[i + 2] << 16) |
                   ((uint32_t)str[i + 3] << 24));
        }
        if (size & 3) {
            uint32_t data = str[i];
            data |= ((size & 3) > 1) ? ((uint32_t)str[i + 1] << 8) : 0;
            data |= ((size & 3) > 2) ? ((uint32_t)str[i + 2] << 16) : 0;
            update(data);
        }
        return *this;
    }

    IGNORE_INTEGER_OVERFLOW inline uint32_t hash() {
        uint32_t hash = mHash;
        hash += (hash << 3);
        hash ^= (hash >> 11);
        hash += (hash << 15);
        return hash;
    }

private:
    uint32_t mHash;
};

}  // namespace minikin

#endif  // MINIKIN_HASHER_H