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
Prev Previous commit
Next Next commit
Functions declarations in header file and definitions in c file
  • Loading branch information
pranavboyapati committed Dec 4, 2024
commit 54ab7cf5854ed042b02938551d74ad8d2f7306d2
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
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 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(struct 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(struct 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)
}
}
57 changes: 3 additions & 54 deletions Data_Buffer/data_buffer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*
** In order to use the data buffer functionality, include this header file in your C program
*/


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

Expand All @@ -17,59 +12,13 @@ struct 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
struct Buffer CreateNewBuffer(int bufferSize)
{
struct Buffer buffer;

buffer.queue = (int*)malloc(sizeof(int) * bufferSize);
buffer.capacity = bufferSize;
buffer.elementsInQueue = 0;

return buffer;
}
struct 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(struct Buffer b, int dataPoint)
{
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++;
}
}
void Enqueue(struct 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(struct 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)
}

}
int Dequeue(struct Buffer b);