• 分享一个生成哈希值的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

  • 相关阅读:
    使用OfficeTool免费安装Office
    match-sorter 插件
    通过FIR滤波器的输出,出现信号的延迟,校正信号的延迟量
    基于springboot vue mysql的在线拍卖系统 全套代码 全套文档
    网络套接字编程(三)
    抖音团购跟小程序团购小程序开发有什么区别?
    12.v3+ts的watch
    初识Linux:目录&路径
    使用cpolar内网端口映射技术实现U8用友ERP本地部署的异地访问
    Avalonia的UI组件
  • 原文地址:https://blog.csdn.net/u013001137/article/details/134536410