• 分享一个生成哈希值的C代码


    代码是我在阅读assimp源码时看到的,不依赖任何第三方,可直接集成。

    1. #pragma once
    2. #ifndef AI_HASH_H_INCLUDED
    3. #define AI_HASH_H_INCLUDED
    4. #ifdef __GNUC__
    5. # pragma GCC system_header
    6. #endif
    7. #include
    8. #include
    9. #include
    10. // ------------------------------------------------------------------------------------------------
    11. // Hashing function taken from
    12. // http://www.azillionmonkeys.com/qed/hash.html
    13. // (incremental version)
    14. //
    15. // This code is Copyright 2004-2008 by Paul Hsieh. It is used here in the belief that
    16. // Assimp's license is considered compatible with Pauls's derivative license as specified
    17. // on his web page.
    18. //
    19. // (stdint.h should have been been included here)
    20. // ------------------------------------------------------------------------------------------------
    21. #undef get16bits
    22. #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
    23. || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
    24. #define get16bits(d) (*((const uint16_t *) (d)))
    25. #endif
    26. #if !defined (get16bits)
    27. #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
    28. +(uint32_t)(((const uint8_t *)(d))[0]) )
    29. #endif
    30. // ------------------------------------------------------------------------------------------------
    31. inline uint32_t SuperFastHash (const char * data, uint32_t len = 0, uint32_t hash = 0) {
    32. uint32_t tmp;
    33. int rem;
    34. if (!data) return 0;
    35. if (!len)len = (uint32_t)::strlen(data);
    36. rem = len & 3;
    37. len >>= 2;
    38. /* Main loop */
    39. for (;len > 0; len--) {
    40. hash += get16bits (data);
    41. tmp = (get16bits (data+2) << 11) ^ hash;
    42. hash = (hash << 16) ^ tmp;
    43. data += 2*sizeof (uint16_t);
    44. hash += hash >> 11;
    45. }
    46. /* Handle end cases */
    47. switch (rem) {
    48. case 3: hash += get16bits (data);
    49. hash ^= hash << 16;
    50. hash ^= abs(data[sizeof(uint16_t)]) << 18;
    51. hash += hash >> 11;
    52. break;
    53. case 2: hash += get16bits (data);
    54. hash ^= hash << 11;
    55. hash += hash >> 17;
    56. break;
    57. case 1: hash += *data;
    58. hash ^= hash << 10;
    59. hash += hash >> 1;
    60. }
    61. /* Force "avalanching" of final 127 bits */
    62. hash ^= hash << 3;
    63. hash += hash >> 5;
    64. hash ^= hash << 4;
    65. hash += hash >> 17;
    66. hash ^= hash << 25;
    67. hash += hash >> 6;
    68. return hash;
    69. }
    70. #endif // !! AI_HASH_H_INCLUDED

  • 相关阅读:
    数据分箱(分层)的几种方法
    java 遍历文件夹目录树形结构并在控制台输出且保存到本地文件
    【CVPR 2022】HDR-NeRF: High Dynamic Range Neural Radiance Fields
    视觉语言模型能多好地看到图像细节? [单位:蒙纳士大学]
    排序:归并(Merge)排序算法分析
    MyBatis 关联映射
    c#中的析构函数
    v3 + ts 商城项目的支付模块
    红日靶场五(vulnstack5)渗透分析
    工程项目安全管理系统安质保智慧建造云平台3.0帮建设单位管好工程项目
  • 原文地址:https://blog.csdn.net/u013001137/article/details/134536410