source: trunk/src/hash.cpp @ 1621

Last change on this file since 1621 was 686, checked in by sam, 9 years ago

Put everything in the "lol" namespace. Better late than never.

  • Property svn:keywords set to Id
File size: 1.2 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 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 implementation class
22 */
23
24static class HashData
25{
26    friend class Hash;
27
28public:
29    HashData()
30    {
31        /* Initialise CRC32 table */
32        for (int i = 0; i < 256; i++)
33        {
34            uint32_t tmp = i;
35            for (int j = 8; j--; )
36                tmp = (tmp >> 1) ^ ((tmp & 1) ? 0xedb88320 : 0);
37            crc32_table[i] = tmp;
38        }
39    }
40
41private:
42    uint32_t crc32_table[256];
43}
44hashdata;
45
46static HashData * const data = &hashdata;
47
48/*
49 * Public Hash class
50 */
51
52uint32_t Hash::Crc32(char const *str)
53{
54    uint32_t ret = 0xffffffff, ch;
55
56    while ((ch = (uint8_t)*str++))
57        ret = data->crc32_table[(uint8_t)(ret ^ ch)] ^ (ret >> 8);
58
59    return ret ^ 0xffffffff;
60}
61
62} /* namespace lol */
63
Note: See TracBrowser for help on using the repository browser.