有关LZF算法的相关解析文档比较少,但是Apple对LZF的开源,可以让我们对该算法进行一个简单的解析。LZFSE 基于 Lempel-Ziv ,并使用了有限状态熵编码。LZF采用类似lz77和lzss的混合编码。使用3种“起始标记”来代表每段输出的数据串。

接下来看一下开源的LZF算法的实现源码。

1.定义的全局字段:

        private readonly long[] _hashTable = new long[Hsize];private const uint Hlog = 14;private const uint Hsize = (1 << 14);private const uint MaxLit = (1 << 5);private const uint MaxOff = (1 << 13);private const uint MaxRef = ((1 << 8) + (1 << 3));

2.使用LibLZF算法压缩数据:

        /// <summary>/// 使用LibLZF算法压缩数据/// </summary>/// <param name="input">需要压缩的数据</param>/// <param name="inputLength">要压缩的数据的长度</param>/// <param name="output">引用将包含压缩数据的缓冲区</param>/// <param name="outputLength">压缩缓冲区的长度(应大于输入缓冲区)</param>/// <returns>输出缓冲区中压缩归档的大小</returns>public int Compress(byte[] input, int inputLength, byte[] output, int outputLength){Array.Clear(_hashTable, 0, (int)Hsize);uint iidx = 0;uint oidx = 0;var hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]);var lit = 0;for (; ; ){if (iidx < inputLength - 2){hval = (hval << 8) | input[iidx + 2];long hslot = ((hval ^ (hval << 5)) >> (int)(((3 * 8 - Hlog)) - hval * 5) & (Hsize - 1));var reference = _hashTable[hslot];_hashTable[hslot] = iidx;long off;if ((off = iidx - reference - 1) < MaxOff&& iidx + 4 < inputLength&& reference > 0&& input[reference + 0] == input[iidx + 0]&& input[reference + 1] == input[iidx + 1]&& input[reference + 2] == input[iidx + 2]){uint len = 2;var maxlen = (uint)inputLength - iidx - len;maxlen = maxlen > MaxRef ? MaxRef : maxlen;if (oidx + lit + 1 + 3 >= outputLength)return 0;dolen++;while (len < maxlen && input[reference + len] == input[iidx + len]);if (lit != 0){output[oidx++] = (byte)(lit - 1);lit = -lit;dooutput[oidx++] = input[iidx + lit];while ((++lit) != 0);}len -= 2;iidx++;if (len < 7){output[oidx++] = (byte)((off >> 8) + (len << 5));}else{output[oidx++] = (byte)((off >> 8) + (7 << 5));output[oidx++] = (byte)(len - 7);}output[oidx++] = (byte)off;iidx += len - 1;hval = (uint)(((input[iidx]) << 8) | input[iidx + 1]);hval = (hval << 8) | input[iidx + 2];_hashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - Hlog)) - hval * 5) & (Hsize - 1))] = iidx;iidx++;hval = (hval << 8) | input[iidx + 2];_hashTable[((hval ^ (hval << 5)) >> (int)(((3 * 8 - Hlog)) - hval * 5) & (Hsize - 1))] = iidx;iidx++;continue;}}else if (iidx == inputLength)break;lit++;iidx++;if (lit != MaxLit) continue;if (oidx + 1 + MaxLit >= outputLength)return 0;output[oidx++] = (byte)(MaxLit - 1);lit = -lit;dooutput[oidx++] = input[iidx + lit];while ((++lit) != 0);}if (lit == 0) return (int)oidx;if (oidx + lit + 1 >= outputLength)return 0;output[oidx++] = (byte)(lit - 1);lit = -lit;dooutput[oidx++] = input[iidx + lit];while ((++lit) != 0);return (int)oidx;}

3.使用LibLZF算法重载压缩数据:

        /// <summary>/// 使用LibLZF算法解压缩数据/// </summary>/// <param name="input">参考数据进行解压缩</param>/// <param name="inputLength">要解压缩的数据的长度</param>/// <param name="output">引用包含解压缩数据的缓冲区</param>/// <param name="outputLength">输出缓冲区中压缩归档的大小</param>/// <returns>返回解压缩大小</returns>public int Decompress(byte[] input, int inputLength, byte[] output, int outputLength){uint iidx = 0;uint oidx = 0;do{uint ctrl = input[iidx++];if (ctrl < (1 << 5)){ctrl++;if (oidx + ctrl > outputLength){return 0;}dooutput[oidx++] = input[iidx++];while ((--ctrl) != 0);}else{var len = ctrl >> 5;var reference = (int)(oidx - ((ctrl & 0x1f) << 8) - 1);if (len == 7)len += input[iidx++];reference -= input[iidx++];if (oidx + len + 2 > outputLength){return 0;}if (reference < 0){return 0;}output[oidx++] = output[reference++];output[oidx++] = output[reference++];dooutput[oidx++] = output[reference++];while ((--len) != 0);}}while (iidx < inputLength);return (int)oidx;}

以上是LZF算法的代码。

转载于:https://blog.51cto.com/pengze0902/1865669

Apple的LZF算法解析相关推荐

  1. Apple LZF算法解析

    有关LZF算法的相关解析文档比较少,但是Apple对LZF的开源,可以让我们对该算法进行一个简单的解析.LZFSE 基于 Lempel-Ziv ,并使用了有限状态熵编码.LZF采用类似lz77和lzs ...

  2. 回归算法分类,常用回归算法解析

    回归算法分类,常用回归算法解析 回归是数学建模.分类和预测中最古老但功能非常强大的工具之一.回归在工程.物理学.生物学.金融.社会科学等各个领域都有应用,是数据科学家常用的基本工具. 回归通常是机器学 ...

  3. CVPR目标检测与实例分割算法解析:FCOS(2019),Mask R-CNN(2019),PolarMask(2020)

    CVPR目标检测与实例分割算法解析:FCOS(2019),Mask R-CNN(2019),PolarMask(2020) 目标检测:FCOS(CVPR 2019) 目标检测算法FCOS(FCOS: ...

  4. 10没有基于策略的qos_分布式QoS算法解析

    QoS对于服务多租户多业务的整体系统来说,不管对网络还是存储,都格外重要,没有QoS,会造成不同租户及业务之间对资源的抢占,用户A用爽了,用户B却遭了殃,频频投诉,这是系统管理员最头疼的事情.我们今天 ...

  5. python终结一个循环额_Python语言入门之内存管理方式和垃圾回收算法解析

    本文主要向大家介绍了Python语言入门之内存管理方式和垃圾回收算法解析,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助. 在列表,元组,实例,类,字典和函数中存在循环引用问题.有 ...

  6. 脑机接口主流算法解析课程视频汇总

    目录 讲座1--SSVEP算法解析 讲座2--ERP/P300算法解析 讲座3--运动想象算法解析 讲座4--情感脑机接口算法解析 本分享为脑机学习者Rose整理发表于公众号:脑机接口社区 .QQ交流 ...

  7. yolo类检测算法解析——yolo v3

    原文:https://www.cnblogs.com/cvtoEyes/p/8608205.html yolo类检测算法解析--yolo v3 计算机视觉的发展史可谓很长了,它的分支很多,而且理论那是 ...

  8. 【机器学习】通俗的元胞自动机算法解析和应用

    [机器学习]通俗的元胞自动机算法解析和应用 文章目录 1 元胞自动机的定义 2 元胞自动机的组成 3 元胞自动机的特征 4 Python实现元胞自动机(生命游戏) 5 总结 6 Github(华盛顿州 ...

  9. 【机器学习】树回归和聚类算法解析和应用

    [机器学习]树回归和聚类算法解析和应用 文章目录 1 树回归 2 CART ( Classification And Regression Tree) 分类回归树 3 K-means3.1 合理选择 ...

最新文章

  1. 工具箱支持汽车质量人工智能
  2. 干货丨一文看懂什么是知识图谱!
  3. 【项目实战课】基于Pytorch的SRGAN图像超分辨实战
  4. 【SpringBoot】SpingBoot整合AOP
  5. 咱也开始玩z-blog了
  6. GIT上传下载报错:[You do not have permission to pull from the repository]的解决方案!
  7. Clumsy网络模拟工具
  8. 栈溢出笔记1.4 黑掉example_2
  9. 从零开始构建HTML 5 Web页面
  10. VS2012--应用程序无法正常启动0xc000007b 的错误
  11. SAP License:与猎头的亲密接触之你不得不知的潜规则
  12. 什么软件可以测试小米四进水没有声音,小米5手机进水不用怕!牢记4个步骤救活手机...
  13. Perceptron实践
  14. 单片机C语言控制16*16LED显示屏,基于单片机的pwm控制16*16led点阵亮度调节怎么做啊,...
  15. 机器学习读书笔记:假设检验(一)
  16. (读后摘抄)《计算机程序设计语言的发展》_王汝传
  17. 奇异值分解及几何意义
  18. Gopher Daily (2020.05.02) ʕ◔ϖ◔ʔ
  19. python 不能读取xlsx格式的excel解决方案,安装1.2.0版本
  20. java常用时间工具类

热门文章

  1. mvc的宿舍管理系统源码 基于jsp_[源码和文档分享]基于JSP的MVC框架实现的图书推荐系统展示平台网站...
  2. 如何拼通网络ip地址_如何解决IP地址冲突
  3. mysql 查询不使用索引_简单的mySQL查询不使用索引
  4. java 数据库 流式查询_关于mybatis:强大MyBatis-三种流式查询方法
  5. 【java】兴唐第十九节课(内部类)
  6. nodejs回调函数理解
  7. C3P0_and_pro.properties配置文档代码
  8. cordova 环境配制和创建插件
  9. Apache Unable to find the wrapper https - did you forget to enable it when you configured PHP?
  10. iOS 开发者账号 到期续费问题