免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2397 | 回复: 0
打印 上一主题 下一主题

[C] 分享一个variable byte code 数字变长压缩代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-24 14:10 |只看该作者 |倒序浏览
比之前的定长压缩平均压缩比要高, 速度要快一点. 具体使用可以看 zvbcode.c  呵呵...
URL
http://libibase.googlecode.com/svn/trunk/devel/utils/zvbcode.h
http://libibase.googlecode.com/svn/trunk/devel/utils/zvbcode.c

  1. #ifndef _ZVBCODE_H
  2. #define _ZVBCODE_H
  3. #define VBUC(p) ((unsigned char *)p)
  4. #define LL(p) ((unsigned long long)p)
  5. /* compress variable byte code
  6. * (long/long long) int n = 100000, *np = &n;
  7. * char *p = buf;
  8. * ZVBCODE(np, p);
  9. *
  10. * */
  11. #define ZVBCODE(np, p)                                                                      \
  12. do                                                                                          \
  13. {                                                                                           \
  14.     do                                                                                      \
  15.     {                                                                                       \
  16.         *VBUC(p) = *VBUC(np);                                                               \
  17.         *np >>= 7;                                                                          \
  18.         if(*np) *VBUC(p) |= 0x80;                                                           \
  19.         ++p;                                                                                \
  20.     }while(*np > 0);                                                                        \
  21. }while(0)

  22. /* uncompress variable byte code
  23. * (long/long long) int n = 0, *np = &n;
  24. * int x = 0;
  25. * char *p = buf;
  26. * UZVBCODE(p, x, np);
  27. *
  28. * */
  29. #define UZVBCODE(p, x, np)                                                                  \
  30. do                                                                                          \
  31. {                                                                                           \
  32.     x = 0;                                                                                  \
  33.     do                                                                                      \
  34.     {                                                                                       \
  35.         *np |= ((*VBUC(p) & 0x7F) << x);                                                    \
  36.         x += 7;                                                                             \
  37.     }while((*VBUC(p++) & 0x80));                                                            \
  38. }while(0)

  39. /* compress z10 */
  40. #define ZVB10(np, p)                                                                        \
  41. do                                                                                          \
  42. {                                                                                           \
  43.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  44.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  45.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  46.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  47.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  48.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  49.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  50.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  51.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  52.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  53. }while(0)

  54. /* compress z9 */
  55. #define ZVB9(np, p)                                                                         \
  56. do                                                                                          \
  57. {                                                                                           \
  58.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  59.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  60.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  61.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  62.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  63.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  64.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  65.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  66.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  67. }while(0)

  68. /* compress z8 */
  69. #define ZVB8(np, p)                                                                         \
  70. do                                                                                          \
  71. {                                                                                           \
  72.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  73.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  74.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  75.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  76.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  77.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  78.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  79.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  80. }while(0)

  81. /* compress z7 */
  82. #define ZVB7(np, p)                                                                         \
  83. do                                                                                          \
  84. {                                                                                           \
  85.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  86.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  87.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  88.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  89.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  90.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  91.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  92. }while(0)

  93. /* compress z6 */
  94. #define ZVB6(np, p)                                                                         \
  95. do                                                                                          \
  96. {                                                                                           \
  97.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  98.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  99.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  100.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  101.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  102.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  103. }while(0)

  104. /* compress z5 */
  105. #define ZVB5(np, p)                                                                         \
  106. do                                                                                          \
  107. {                                                                                           \
  108.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  109.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  110.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  111.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  112.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  113. }while(0)

  114. /* compress z4 */
  115. #define ZVB4(np, p)                                                                         \
  116. do                                                                                          \
  117. {                                                                                           \
  118.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  119.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  120.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  121.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  122. }while(0)

  123. /* compress z3 */
  124. #define ZVB3(np, p)                                                                         \
  125. do                                                                                          \
  126. {                                                                                           \
  127.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  128.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  129.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  130. }while(0)

  131. /* compress z2 */
  132. #define ZVB2(np, p)                                                                         \
  133. do                                                                                          \
  134. {                                                                                           \
  135.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  136.     *VBUC(p++) = (*VBUC(np) | 0x80); *np >>= 7;                                             \
  137. }while(0)

  138. /* compress */
  139. #define ZVB(np, p)                                                                          \
  140. do                                                                                          \
  141. {                                                                                           \
  142.    if(LL(*np) >= 0x8000000000000000llu){ZVB10(np, p);}                                      \
  143.    else if(LL(*np) >= 0x100000000000000llu){ZVB9(np, p);}                                   \
  144.    else if(LL(*np) >= 0x2000000000000llu){ZVB8(np, p);}                                     \
  145.    else if(LL(*np) >= 0x40000000000llu){ZVB7(np, p);}                                       \
  146.    else if(LL(*np) >= 0x800000000llu){ZVB6(np, p);}                                         \
  147.    else if(LL(*np) >= 0x10000000llu){ZVB5(np, p);}                                          \
  148.    else if(LL(*np) >= 0x200000llu){ZVB4(np, p);}                                            \
  149.    else if(LL(*np) >= 0x4000llu){ZVB3(np, p);}                                              \
  150.    else if(LL(*np) >= 0x80llu){ZVB2(np, p);}                                                \
  151.    else {*VBUC(p++) = (*VBUC(np) | 0x80);}                                                  \
  152. }while(0)

  153. #define LL2DOUBLE(llp, dp)                                                                  \
  154. do                                                                                          \
  155. {                                                                                           \
  156.     VBUC(dp)[0] = VBUC(llp)[7];                                                             \
  157.     VBUC(dp)[1] = VBUC(llp)[6];                                                             \
  158.     VBUC(dp)[2] = VBUC(llp)[5];                                                             \
  159.     VBUC(dp)[3] = VBUC(llp)[4];                                                             \
  160.     VBUC(dp)[4] = VBUC(llp)[3];                                                             \
  161.     VBUC(dp)[5] = VBUC(llp)[2];                                                             \
  162.     VBUC(dp)[6] = VBUC(llp)[1];                                                             \
  163.     VBUC(dp)[7] = VBUC(llp)[0];                                                             \
  164. }while(0)

  165. #define DOUBLE2LL(dp, llp)                                                                  \
  166. do                                                                                          \
  167. {                                                                                           \
  168.     VBUC(llp)[0] = VBUC(dp)[7];                                                             \
  169.     VBUC(llp)[1] = VBUC(dp)[6];                                                             \
  170.     VBUC(llp)[2] = VBUC(dp)[5];                                                             \
  171.     VBUC(llp)[3] = VBUC(dp)[4];                                                             \
  172.     VBUC(llp)[4] = VBUC(dp)[3];                                                             \
  173.     VBUC(llp)[5] = VBUC(dp)[2];                                                             \
  174.     VBUC(llp)[6] = VBUC(dp)[1];                                                             \
  175.     VBUC(llp)[7] = VBUC(dp)[0];                                                             \
  176. }while(0)
  177. #endif
复制代码

[ 本帖最后由 redor 于 2008-11-11 10:17 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP