hashfuncs.h
1 /*************************************************************************/
2 /* hashfuncs.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef HASHFUNCS_H
30 #define HASHFUNCS_H
31 
32 
33 #include "typedefs.h"
34 
45 static inline uint32_t hash_djb2(const char *p_cstr) {
46 
47  const unsigned char* chr=(const unsigned char*)p_cstr;
48  uint32_t hash = 5381;
49  uint32_t c;
50 
51  while ((c = *chr++))
52  hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
53 
54  return hash;
55 }
56 
57 static inline uint32_t hash_djb2_buffer(const uint8_t *p_buff, int p_len,uint32_t p_prev=5381) {
58 
59  uint32_t hash = p_prev;
60 
61  for(int i=0;i<p_len;i++)
62  hash = ((hash << 5) + hash) + p_buff[i]; /* hash * 33 + c */
63 
64  return hash;
65 }
66 
67 static inline uint32_t hash_djb2_one_32(uint32_t p_in,uint32_t p_prev=5381) {
68 
69  return ((p_prev<<5)+p_prev)+p_in;
70 }
71 
72 static inline uint32_t hash_djb2_one_float(float p_in,uint32_t p_prev=5381) {
73  union {
74  float f;
75  uint32_t i;
76  } u;
77  u.f=p_in;
78 
79  return ((p_prev<<5)+p_prev)+u.i;
80 }
81 
82 template<class T>
83 static inline uint32_t make_uint32_t(T p_in) {
84 
85  union {
86  T t;
87  uint32_t _u32;
88  } _u;
89  _u._u32=0;
90  _u.t=p_in;
91  return _u._u32;
92 }
93 
94 
95 static inline uint64_t hash_djb2_one_64(uint64_t p_in,uint64_t p_prev=5381) {
96 
97  return ((p_prev<<5)+p_prev)+p_in;
98 }
99 
100 
101 template<class T>
102 static inline uint64_t make_uint64_t(T p_in) {
103 
104  union {
105  T t;
106  uint64_t _u64;
107  } _u;
108  _u._u64=0; // in case p_in is smaller
109 
110  _u.t=p_in;
111  return _u._u64;
112 }
113 
114 
115 
116 #endif