Skip to content

Commit

Permalink
feat: generic queue
Browse files Browse the repository at this point in the history
  • Loading branch information
EDB02 committed Oct 19, 2023
1 parent 2a61a03 commit f800040
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
36 changes: 36 additions & 0 deletions generic-queue/generic_queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "generic_queue.h"

void GENQ_init(generic_queue_t *q, size_t elem_size)
{
q->queue_elem_size = elem_size;
q->tail = q->head = q->cnt_elems = 0;
q->max_elems = GENQ_SIZE/q->queue_elem_size;
}
bool GENQ_is_empty(generic_queue_t *q)
{
return q->cnt_elems == 0;
}
bool GENQ_is_full(generic_queue_t *q)
{
return q->cnt_elems >= q->max_elems;
}
bool GENQ_pop(generic_queue_t *q, void *e)
{
if(GENQ_is_empty(q)) return 0;
q->cnt_elems--;

memcpy(e, q->queue+q->head*q->queue_elem_size, q->queue_elem_size);
q->head++;
q->head %= q->max_elems;
return 1;
}
bool GENQ_push(generic_queue_t *q, void *e)
{
if(GENQ_is_full(q)) return 0;
q->cnt_elems++;

memcpy(q->queue+q->tail*q->queue_elem_size, e, q->queue_elem_size);
q->tail++;
q->tail %= q->max_elems;
return 1;
}
25 changes: 25 additions & 0 deletions generic-queue/generic_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef _GENERIC_QUEUE
#define _GENERIC_QUEUE

#include "inttypes.h"
#include "stdbool.h"
#include "stdlib.h"
#include "string.h"

#define GENQ_SIZE 1000

typedef struct{
size_t head, tail;
uint8_t queue[GENQ_SIZE];
size_t queue_elem_size;
size_t max_elems;
int cnt_elems;
} generic_queue_t;

void GENQ_init(generic_queue_t *, size_t);
bool GENQ_is_empty(generic_queue_t*);
bool GENQ_is_full(generic_queue_t*);
bool GENQ_pop(generic_queue_t*, void *);
bool GENQ_push(generic_queue_t*, void *);

#endif
79 changes: 79 additions & 0 deletions generic-queue/test/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "stdio.h"
#include "../generic_queue.h"

generic_queue_t q;

#define n 10000

typedef struct{
long int v;
char prova[5];
} test;

int main()
{
GENQ_init(&q, sizeof(test));

test v[n];
char prova[] = "asdaa";
for(int i=0;i<n;i++)
{
v[i].v = i+1;
memcpy(v[i].prova, prova, sizeof(char)*5);
}
int tot = GENQ_SIZE / sizeof(test);
int cnt = 0;
for(int i=0;i<n;i++)
{
if(!GENQ_push(&q, v+i))
{
//printf("errore push\n");
break;
}
else
cnt++;
}
int check = (cnt == tot);
//printf("pushed %d elements\n", cnt);
for(int i=0;i<10;i++)
{
test tmp;
if(!GENQ_pop(&q, &tmp))
{
printf("error\n");
return 11;
}
cnt--;
}
for(int i=0;i<n;i++)
{
if(!GENQ_push(&q, v+i))
{
//printf("errore push\n");
break;
}
else
cnt++;
}
check = check || (cnt == tot);
cnt = 0;
for(int i=0;i<n;i++)
{
test tmp;
if(GENQ_pop(&q, &tmp))
{
//printf("%d %s\n", tmp.v, tmp.prova);
cnt++;
}
else
{
//printf("errore pop\n");
break;
}
}
check = check || (cnt == 0);
printf("maximum elements: %d\n", tot);
if(check) printf("test passed\n");
else printf("test not passed\n");
//printf("popped %d elements\n", cnt);
}

0 comments on commit f800040

Please sign in to comment.