Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data buffer #18

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
63 changes: 63 additions & 0 deletions Data_Buffer/data_buffer.c
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "data_buffer.h"


//this function creates a new buffer of size bufferSize and returns the buffer
//this specific implementation allows the size of the buffer to vary for each buffer created
Buffer_t CreateNewBuffer(int bufferSize)
{
Buffer_t buffer;

int array[bufferSize];

buffer.queue = array;
buffer.capacity = bufferSize;
buffer.elementsInQueue = 0;

return buffer;
}


//this function adds a new data point to the end of the buffer queue, removing the first data point in the queue if the buffer is full
void Enqueue(Buffer_t* b, int dataPoint)
{
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved
if (b->elementsInQueue == b->capacity)
{
for (int i = 1; i < b->elementsInQueue; i++)
{
b->queue[i - 1] = b->queue[i];
}
b->elementsInQueue--;

b->queue[b->elementsInQueue] = dataPoint;
b->elementsInQueue++;
}
else
{
b->queue[b->elementsInQueue] = dataPoint;
b->elementsInQueue++;
}
}


//this function returns the first data point in the buffer and then removes it from the buffer, assuming the buffer is not empty
//returns -1 if the buffer is empty as an error indicator
int Dequeue(Buffer_t* b)
{
int* msg;
if (b->elementsInQueue > 0)
{
int firstElement = b->queue[0];

for (int i = 1; i < b->elementsInQueue; i++)
{
b->queue[i - 1] = b->queue[i];
}
b->elementsInQueue--;

msg = firstElement;
}
else
{
msg = DEQUEUE_FAIL;
}
}
39 changes: 39 additions & 0 deletions Data_Buffer/data_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef DATA_BUFFER_H
#define DATA_BUFFER_H

#include <stdlib.h>
#include <stdint.h>

//create a struct for the buffer
typedef struct Buffer
{
int capacity;
int elementsInQueue;
int* queue;
} Buffer_t;

typedef enum BufferStatus
{
BUFFER_CREATION_SUCCESS,
BUFFER_CREATION_FAIL,
ENQUEUE_SUCCESS,
ENQUEUE_FAIL,
DEQUEUE_SUCCESS,
DEQUEUE_FAIL
} BufferStatus_t;


//this function creates a new buffer of size bufferSize and returns the buffer
//this specific implementation allows the size of the buffer to vary for each buffer created
Buffer_t CreateNewBuffer(int bufferSize);


//this function adds a new data point to the end of the buffer queue, removing the first data point in the queue if the buffer is full
void Enqueue(Buffer_t* b, int dataPoint);


//this function returns the first data point in the buffer and then removes it from the buffer, assuming the buffer is not empty
//returns -1 if the buffer is empty as an error indicator
int Dequeue(Buffer_t* b);

#endif