Circular buffer

Circular buffer

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

前言:

circular buffercyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams.

Uses

The useful property of a circular buffer is that it does not need to have its elements shuffled around [又一次排列]when one is consumed. (If a non-circular buffer were used then it would be necessary to shift all elements when one is consumed.) In other words, the circular buffer is well-suited[很合适] as a FIFO [先进先出]buffer while a standard, non-circular buffer is well suited as a LIFO [后进后出]buffer.

Circular buffering makes a good implementation strategy for a queue that has fixed maximum size. Should a maximum size be adopted [被採用]for a queue, then a circular buffer is a completely ideal implementation; all queue operations are constant time. However, expanding a circular buffer requires shifting memory, which is comparatively[相对来说] costly. For arbitrarily expanding queues[可任意扩展的], a linked list approach[链表] may be preferred instead.

[本文作者franklin注解]环形buffer 应用于固定的块场合,一旦固定,这块内存就是专用,所以,不具备扩展性.

In some situations, overwriting [可覆盖性]circular buffer can be used, e.g. in multimedia. If the buffer is used as the bounded [有界性,有限制] buffer in the producer-consumer problem then it is probably desired for the producer (e.g., an audio generator) to overwrite old data if the consumer (e.g., the sound card) is unable to momentarily keep up. Also, the LZ77 family of lossless data compression algorithms operates on the assumption that strings seen more recently in a data stream are more likely to occur soon in the stream. Implementations store the most recent data in a circular buffer.

How it works

A circular buffer first starts empty and of some predefined length. For example, this is a 7-element buffer:

Circular buffer - empty.svg

Assume that a 1 is written into the middle of the buffer (exact starting location does not matter in a circular buffer):

Circular buffer - XX1XXXX.svg

Then assume that two more elements are added — 2 & 3 — which get appended after the 1:

Circular buffer - XX123XX.svg

If two elements are then removed from the buffer, the oldest values inside the buffer are removed. The two elements removed, in this case, are 1 & 2, leaving the buffer with just a 3:

Circular buffer - XXXX3XX.svg

If the buffer has 7 elements then it is completely full:

Circular buffer - 6789345.svg

A consequence of the circular buffer is that when it is full and a subsequent write is performed, then it starts overwriting the oldest data. In this case, two more elements — A & B — are added and they overwrite the 3 & 4:

[本文作者franklin注解][当环形buffer满的时候,兴许写入数据将覆盖前面数据]

Circular buffer - 6789AB5.svg

Alternatively[或者], the routines that manage the buffer could prevent overwriting the data and return an error or raise[根本就是设计成不能写入] an exception. Whether or not data is overwritten is up to the semantics of the buffer routines or the application using the circular buffer.

Finally, if two elements are now removed then what would be returned is not 3 & 4 but 5 & 6 because A & B overwrote the 3 & the 4 yielding the buffer with:

Circular buffer - X789ABX.svg


Circular buffer mechanics

What is not shown in the example above is the mechanics of how the circular buffer is managed.

Start/end pointers (head/tail)[edit]

Generally, a circular buffer requires four pointers:

  • one to the actual buffer in memory
  • one to the buffer end in memory (or alternately[取代]: the size of the buffer)
  • one to point to the start of valid data (or alternately: amount of data written to the buffer)
  • one to point to the end of valid data (or alternately: amount of data read from the buffer)

Alternatively[或者], a fixed-length buffer with two integers to keep track of indices can be used in languages that do not have pointers.

[没有指针也能够的]

Taking a couple of examples from above. (While there are numerous[庞大的] ways to label the pointers and exact semantics[精确到语意]can vary, this is one way to do it.)

This image shows a partially[部分] full buffer:

Circular buffer - XX123XX with pointers.svg

This image shows a full buffer with two elements having been overwritten:

Circular buffer - 6789AB5 with pointers.svg

What to note about the second one is that after each element is overwritten then the start pointer is incremented as well.

Difficulties

Full / Empty Buffer Distinction

A small disadvantage of relying on pointers or relative indices of the start and end of data is, that in the case the buffer is entirely full, both pointers point to the same element:

Circular buffer - 6789AB5 full.svg

This is exactly the same situation as when the buffer is empty:

Circular buffer - 6789AB5 empty.svg

[最常见的问题是,buffer的满和空的判别]

To solve this confusion there are a number of solutions:

Always Keep One Slot Open [最简单有效的解决的方法就是保持最后一个单元不写]

This design always keeps one slot unallocated. A full buffer has at most (\text{size}-1) slots. If both pointers refer to the same slot, the buffer is empty. If the end (write) pointer refers to the slot preceding the one referred to by the start (read) pointer, the buffer is full.

The advantage is:

  • The solution is simple and robust.

The disadvantages are:

  • One slot is lost, so it is a bad compromise when the buffer size is small or the slot is big or is implemented in hardware.
  • The full test requires a modulo operation

ref:

http://en.wikipedia.org/wiki/Circular_buffer

http://lmax-exchange.github.io/disruptor/

http://www.cnblogs.com/shanyou/archive/2013/02/04/2891300.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116759.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2022.01idea激活码[最新免费获取]

    (2022.01idea激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年4月1日
    151
  • 代价函数 cost function

    代价函数 cost function代价函数在监督学习的回归问题中,代价函数就是用于找到最优解的目的函数,反应了预测函数的准确性。代价函数的值越小,说明在回归问题的中,计算机程序对数据拟合的越好。也就是假设函数越正确。比如,对于这个假设函数(可以看成是求房价的假设函数):代价函数是:也就是预测值与真实值的差的平方和,再除以2m(2倍样本数量)。在假设函数中:θ0和θ1两个参数,不同的参数会有不同的假设函数如下图

    2022年6月6日
    25
  • 微信小程序从零开始开发步骤(一)_微信小程序开发零基础入门

    微信小程序从零开始开发步骤(一)_微信小程序开发零基础入门随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、博主微信(guyun297890152)、QQ技术交流群(183198395)。from:https://www.jianshu.com/p/aaef5ceb3936从零开始小程序今天一不小心拿到了小程序的内测资格,为了不辜负微信团队的信任,我决定十一奋斗一把!不过话说我可是一个And…

    2022年9月1日
    1
  • Python之文件操作大全

    Python之文件操作大全在日常工作或生活中,总避免不了需要操作文件或文件夹,比如希望找出电脑中所有临时文件并清除,或者找到指定文件夹内所有图片文件并进行重新命名等等,如果能通过Python脚本的方式解决,会大大提升相关操作效率,本文即总结使用Python进行常见操作相关知识点,方便用到的人随时查阅,不用再每次使用都要花费时间检索或查阅文档。本文主要使用os、shutil、pathlib三个包。一、文件操作1.1文件常规操作操作 代码 说明/示例 新建文件 os.mknod(dir…

    2022年5月7日
    43
  • 7000词汇这么背我比较可以接受,连续看20天足以[通俗易懂]

    7000词汇这么背我比较可以接受,连续看20天足以[通俗易懂]
    16天记住7000考研词汇(第一天)

    1.WithmyownearsIclearlyheardtheheartbeatofthenuclearbomb.
    我亲耳清楚地听到原子弹的心脏的跳动。
    2.Nextyearthebeardedbearwillbearadearbabyintherear.
    明年,长胡子的熊将在后方产一头可爱的小崽.
    3.EarlyIsearchedthr

    2022年8月24日
    5
  • jax-RPC和jax-WS比较

    jax-RPC和jax-WS比较1、JAX-RPC简介:JAX-RPC为基于SOAP(简单对象访问协议)的应用程序的开发提供了一个编程模型。JAX-RPC编程模型通过抽象SOAP协议层的运行机制与提供Java和Web服务描述语言(WSDL)间的映射服务来简化开发。通过使用JAX-RPC(JavaAPIforXML-basedRPC),已有的Java类或Java应用都能够被重新包装,并以WebServices

    2022年7月15日
    11

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号