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
62 changes: 62 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,62 @@
#include <stdlib.h>
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved
#include <stdint.h>
#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 CreateNewBuffer(int bufferSize)
{
Buffer buffer;

buffer.queue = (int*)malloc(sizeof(int) * bufferSize);
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved
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* 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 the max value for int if the buffer is empty as an error indicator
int Dequeue(Buffer* b)
{
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--;

return firstElement;
}
else
{
return -1; //returns -1 to indicate an error (since all expected values should be positive)
}
}
24 changes: 24 additions & 0 deletions Data_Buffer/data_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdlib.h>
#include <stdint.h>

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


//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 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* 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 the max value for int if the buffer is empty as an error indicator
int Dequeue(Buffer* b);
81 changes: 0 additions & 81 deletions data_buffer.h
pranavboyapati marked this conversation as resolved.
Outdated
Show resolved Hide resolved
pranavboyapati marked this conversation as resolved.
Outdated
Show resolved Hide resolved
pranavboyapati marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.