hashfuncs.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*************************************************************************/
  2. /* hashfuncs.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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. #include "typedefs.h"
  32. /**
  33. * Hashing functions
  34. */
  35. /**
  36. * DJB2 Hash function
  37. * @param C String
  38. * @return 32-bits hashcode
  39. */
  40. static inline uint32_t hash_djb2(const char *p_cstr) {
  41. const unsigned char* chr=(const unsigned char*)p_cstr;
  42. uint32_t hash = 5381;
  43. uint32_t c;
  44. while ((c = *chr++))
  45. hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  46. return hash;
  47. }
  48. static inline uint32_t hash_djb2_buffer(const uint8_t *p_buff, int p_len,uint32_t p_prev=5381) {
  49. uint32_t hash = p_prev;
  50. for(int i=0;i<p_len;i++)
  51. hash = ((hash << 5) + hash) + p_buff[i]; /* hash * 33 + c */
  52. return hash;
  53. }
  54. static inline uint32_t hash_djb2_one_32(uint32_t p_in,uint32_t p_prev=5381) {
  55. return ((p_prev<<5)+p_prev)+p_in;
  56. }
  57. static inline uint32_t hash_djb2_one_float(float p_in,uint32_t p_prev=5381) {
  58. union {
  59. float f;
  60. uint32_t i;
  61. } u;
  62. u.f=p_in;
  63. return ((p_prev<<5)+p_prev)+u.i;
  64. }
  65. template<class T>
  66. static inline uint32_t make_uint32_t(T p_in) {
  67. union {
  68. T t;
  69. uint32_t _u32;
  70. } _u;
  71. _u._u32=0;
  72. _u.t=p_in;
  73. return _u._u32;
  74. }
  75. static inline uint64_t hash_djb2_one_64(uint64_t p_in,uint64_t p_prev=5381) {
  76. return ((p_prev<<5)+p_prev)+p_in;
  77. }
  78. template<class T>
  79. static inline uint64_t make_uint64_t(T p_in) {
  80. union {
  81. T t;
  82. uint64_t _u64;
  83. } _u;
  84. _u._u64=0; // in case p_in is smaller
  85. _u.t=p_in;
  86. return _u._u64;
  87. }
  88. #endif