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
81 changes: 81 additions & 0 deletions data_buffer.h
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved
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,81 @@
/*
** In order to use the data buffer functionality, include this header file in your C program
*/


#include <stdlib.h>
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved

//create a struct for the buffer
struct Buffer
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved
{
int capacity;
int elementsInQueue;
int* queue;
};


//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
struct Buffer CreateNewBuffer(int bufferSize)
{
struct 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 returns the size of the specified buffer
int GetBufferSize(struct Buffer b)
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved
{
return b.elementsInQueue;
}


//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 AddElementToBuffer(struct Buffer b, int dataPoint)
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved
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 GetFirstFromBuffer(struct Buffer b)
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved
pranavboyapati marked this conversation as resolved.
Show resolved Hide resolved
{
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)
}

}