总目录链接==>> AutoSAR入门和实战系列总目录
当数据经常从一个地方移动到另一个地方或从一个进程移动到另一个进程或被频繁访问时,它不能存储在永久性内存位置【 permanent memory locations】(例如硬盘驱动器)中,因为它们需要时间来检索数据。这种类型的数据需要快速访问,并存储在临时内存位置,例如称为缓冲区的 RAM。
缓冲区示例
- 当在线播放任何视频时,数据(音频和视频)会在播放视频之前进行缓冲。在此缓冲过程中,数据被下载并存储在 RAM 中,并在需要时进行访问。
- Word 文档在保存文档之前将内容和用户所做的更改存储在缓冲区中。
什么是循环缓冲区?
循环缓冲区或环形缓冲区是一个循环队列,允许以连续的方式使用内存。循环缓冲区遵循 FIFO 原则,即先进先出。
循环缓冲区可以通过两种方式实现,使用数组或链表。
方法 1:使用数组
由于添加的元素类型未知,因此在构造函数中初始化空对象数组及其容量。维护两个指针,即 head 和 tail,用于元素的插入和删除。head 指向第一个元素,tail 指向最后一个元素。
如下图的索引index和其对应的值的循环缓冲区:
图 使用数组的循环缓冲区
插入元素
最初,头部为 0,尾部为 -1,大小为 0。
使用以下公式计算需要插入元素的索引:
int index = (tail + 1) % capacity
array[index] = element;
尾指针和缓冲区的大小在插入元素时递增 1。当数组的大小等于其容量时,缓冲区已满,无法容纳更多元素。
删除元素
检索头指针处的元素并将头指针递增 1,则缓冲区的大小递减 1。
int index = head % capacity;
E element = (E) array[index];
例子:
输入: [5, 6, 7, 1 ,4]
输出:元素按顺序打印:-
5
6
7
1
4
下面是上述方法的实现
// Java program to implement a
// Circular Buffer using an array
import java.io.*;
import java.lang.*;
class CircularBuffer {
// Initial Capacity of Buffer
private int capacity = 0;
// Initial Size of Buffer
private int size = 0;
// Head pointer
private int head = 0;
// Tail pointer
private int tail = -1;
private Object[] array;
// Constructor
CircularBuffer(int capacity)
{
// Initializing the capacity of the array
this.capacity = capacity;
// Initializing the array
array = new Object[capacity];
}
// Addition of elements
public void add(Object element) throws Exception
{
// Calculating the index to add the element
int index = (tail + 1) % capacity;
// Size of the array increases as elements are added
size++;
// Checking if the array is full
if (size == capacity) {
throw new Exception("Buffer Overflow");
}
// Storing the element in the array
array[index] = element;
// Incrementing the tail pointer to point
// to the element added currently
tail++;
}
// Deletion of elements
public Object get() throws Exception
{
// Checking if the array is empty
if (size == 0) {
throw new Exception("Empty Buffer");
}
// Calculating the index of the element to be
// deleted
int index = head % capacity;
// Getting the element
Object element = array[index];
// Incrementing the head pointer to point
// to the next element
head++;
// Decrementing the size of the array as the
// elements are deleted
size--;
// Returning the first element
return element;
}
// Retrieving the first element without deleting it
public Object peek() throws Exception
{
// Checking if the array is empty
if (size == 0) {
throw new Exception("Empty Buffer");
}
// Calculating the index of the
// element to be deleted
int index = head % capacity;
// Getting the element
Object element = array[index];
// Returning the element
return element;
}
// Checking if the array is empty
public boolean isEmpty() { return size == 0; }
// Size of the array
public int size() { return size; }
}
class Main {
public static void main(String[] args) throws Exception
{
// Creating the Circular Buffer
CircularBuffer cb = new CircularBuffer(10);
// Adding elements to the circular Buffer
cb.add(5);
cb.add(6);
cb.add(7);
cb.add(1);
cb.add(4);
// Printing the elements
System.out.println(
"The elements are printed in the order :-");
System.out.println(cb.get());
System.out.println(cb.get());
System.out.println(cb.get());
System.out.println(cb.get());
System.out.println(cb.get());
}
}
输出
元素按顺序打印:- 5 6 7 1 4
时间复杂度: O(1),用于插入和删除。
方法 2:使用链表
创建了一个通用节点类,它充当创建循环缓冲区的辅助类。
维护两个指针,即 head 和 tail,用于元素的插入和删除。head 指向第一个元素,tail 指向最后一个元素。
使用链表的循环缓冲区
插入元素:
- 最初 head 和 tail 为空,大小为 0。
- 元素添加到链表的尾部,尾部的引用更改为头指针。
- 缓冲区的大小随着元素添加到链接列表中而增加。
- 当数组的大小等于其容量时,缓冲区已满,无法容纳更多元素。
删除元素:
检索头指针处的元素,并且头指针的引用更改为下一个元素和缓冲区的大小减一。
例子 :
输入: [5, 6, 7, 1 ,4] 输出:元素按顺序打印:
5
6
7
1
4
下面是上述方法的实现:
// Java program to implement a Circular
// Buffer using a Linked List
// A Generic Node class is used to create a Linked List
class Node<E> {
// Data Stored in each Node of the Linked List
E data;
// Pointer to the next node in the Linked List
Node<E> next;
// Node class constructor used to initializes
// the data in each Node
Node(E data) { this.data = data; }
}
class CircularBufferLL<E> {
// Head node
Node<E> head;
// Tail Node
Node<E> tail;
int size = 0;
int capacity = 0;
// Constructor
CircularBufferLL(int capacity)
{
this.capacity = capacity;
}
// Addition of Elements
public void add(E element) throws Exception
{
// Size of buffer increases as elements
// are added to the Linked List
size++;
// Checking if the buffer is full
if (size == capacity) {
throw new Exception("Buffer Overflow");
}
// Checking if the buffer is empty
if (head == null) {
head = new Node<>(element);
tail = head;
return;
}
// Node element to be linked
Node<E> temp = new Node<>(element);
// Referencing the last element to the head node
temp.next = head;
// Updating the tail reference to the
// latest node added
tail.next = temp;
// Updating the tail to the latest node added
tail = temp;
}
// Retrieving the head element
public E get() throws Exception
{
// Checking if the buffer is empty
if (size == 0) {
throw new Exception("Empty Buffer");
}
// Getting the element
E element = head.data;
// Updating the head pointer
head = head.next;
// Updating the tail reference to
// the new head pointer
tail.next = head;
// Decrementing the size
size--;
if (size == 0) {
// Removing any references present
// when the buffer becomes empty
head = tail = null;
}
return element;
}
// Retrieving the head element without deleting
public E peek() throws Exception
{
// Checking if the buffer is empty
if (size == 0) {
throw new Exception("Empty Buffer");
}
// Getting the element
E element = head.data;
return element;
}
// Checking if the buffer is empty
public boolean isEmpty() { return size == 0; }
// Retrieving the size of the buffer
public int size() { return size; }
}
class GFG {
public static void main(String[] args) throws Exception
{
// Creating the Circular Buffer
CircularBufferLL<Integer> cb
= new CircularBufferLL<>(10);
// Adding elements to the circular Buffer
cb.add(5);
cb.add(6);
cb.add(7);
cb.add(1);
cb.add(4);
// Printing the elements
System.out.println(
"The elements are printed in the order :-");
System.out.println(cb.get());
System.out.println(cb.get());
System.out.println(cb.get());
System.out.println(cb.get());
System.out.println(cb.get());
}
}
输出
元素按顺序打印:5 6 7 1 4
时间复杂度: O(1),用于插入和删除。