Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_hash.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_HASH_H
7 #define TB_HASH_H
8 
9 #include "tb_types.h"
10 
11 namespace tb {
12 
13 // On C++ compilers that support it, use const expr for hash so that
14 // TBID comparisions turn into simple uint32_t comparisions compiletime.
15 // Disabled for TB_RUNTIME_DEBUG_INFO builds, so TBID string debugging
16 // is available.
17 //
18 // FNV constants
19 static constexpr uint32_t basis = 2166136261U;
20 static constexpr uint32_t prime = 16777619U;
21 
22 // compile-time hash helper function
23 constexpr uint32_t TBGetHash_one(char c, const char* remain, uint32_t value)
24 {
25  return c == 0 ? value : TBGetHash_one(remain[0], remain + 1, (value ^ c) * prime);
26 }
27 
28 // compile-time hash
29 constexpr uint32_t TBGetHash(const char* str)
30 {
31  return (str && *str) ? TBGetHash_one(str[0], str + 1, basis) : 0;
32 }
33 
34 #define TBIDC(str) tb::TBGetHash(str)
35 
36 } // namespace tb
37 
38 #endif // TB_HASH_H
39