source: trunk/src/core/hash.cpp @ 2104

Last change on this file since 2104 was 2104, checked in by sam, 7 years ago

core: allow hashing of String objects.

  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16
17namespace lol
18{
19
20/*
21 * Hash implementations
22 */
23
24static class HashData
25{
26public:
27    HashData()
28    {
29        /* Initialise CRC32 table */
30        for (int i = 0; i < 256; i++)
31        {
32            uint32_t tmp = i;
33            for (int j = 8; j--; )
34                tmp = (tmp >> 1) ^ ((tmp & 1) ? 0xedb88320 : 0);
35            crc32_table[i] = tmp;
36        }
37    }
38
39    uint32_t crc32_table[256];
40}
41const data;
42
43/*
44 * Public Hash classes
45 */
46
47uint32_t Hash<int8_t>::operator ()(int8_t x)
48{
49    uint32_t ret = 0xffffffffu;
50    ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8);
51    return ret ^ 0xffffffffu;
52}
53
54uint32_t Hash<uint8_t>::operator ()(uint8_t x)
55{
56    uint32_t ret = 0xffffffffu;
57    ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8);
58    return ret ^ 0xffffffffu;
59}
60
61uint32_t Hash<int16_t>::operator ()(int16_t x)
62{
63    uint32_t ret = 0xffffffffu;
64    ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8);
65    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8);
66    return ret ^ 0xffffffffu;
67}
68
69uint32_t Hash<uint16_t>::operator ()(uint16_t x)
70{
71    uint32_t ret = 0xffffffffu;
72    ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8);
73    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8);
74    return ret ^ 0xffffffffu;
75}
76
77uint32_t Hash<int32_t>::operator ()(int32_t x)
78{
79    uint32_t ret = 0xffffffffu;
80    ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8);
81    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8);
82    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 16))] ^ (ret >> 8);
83    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 24))] ^ (ret >> 8);
84    return ret ^ 0xffffffffu;
85}
86
87uint32_t Hash<uint32_t>::operator ()(uint32_t x)
88{
89    uint32_t ret = 0xffffffffu;
90    ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8);
91    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8);
92    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 16))] ^ (ret >> 8);
93    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 24))] ^ (ret >> 8);
94    return ret ^ 0xffffffffu;
95}
96
97uint32_t Hash<int64_t>::operator ()(int64_t x)
98{
99    uint32_t ret = 0xffffffffu;
100    ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8);
101    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8);
102    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 16))] ^ (ret >> 8);
103    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 24))] ^ (ret >> 8);
104    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 32))] ^ (ret >> 8);
105    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 40))] ^ (ret >> 8);
106    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 48))] ^ (ret >> 8);
107    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 56))] ^ (ret >> 8);
108    return ret ^ 0xffffffffu;
109}
110
111uint32_t Hash<uint64_t>::operator ()(uint64_t x)
112{
113    uint32_t ret = 0xffffffffu;
114    ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8);
115    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8);
116    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 16))] ^ (ret >> 8);
117    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 24))] ^ (ret >> 8);
118    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 32))] ^ (ret >> 8);
119    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 40))] ^ (ret >> 8);
120    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 48))] ^ (ret >> 8);
121    ret = data.crc32_table[(uint8_t)(ret ^ (x >> 56))] ^ (ret >> 8);
122    return ret ^ 0xffffffffu;
123}
124
125static uint32_t HashCharString(char const *s)
126{
127    uint32_t ret = 0xffffffffu, ch;
128
129    while ((ch = (uint8_t)*s++))
130        ret = data.crc32_table[(uint8_t)(ret ^ ch)] ^ (ret >> 8);
131
132    return ret ^ 0xffffffffu;
133}
134
135uint32_t Hash<char const *>::operator ()(char const *s)
136{
137    return HashCharString(s);
138}
139
140uint32_t Hash<String>::operator ()(String const &s)
141{
142    return HashCharString(&s[0]);
143}
144
145} /* namespace lol */
146
Note: See TracBrowser for help on using the repository browser.