From 71b59b60e2a9146affa5219136dc1386837f1b2a Mon Sep 17 00:00:00 2001 From: Lorenzo Bianchi Date: Thu, 20 Apr 2023 12:40:51 +0200 Subject: [PATCH] Added initial files --- include/arch.h | 10 +++ include/coor.h | 9 +++ include/priority_queue.h | 40 +++++++++++ include/queue.h | 48 +++++++++++++ include/vector.h | 104 +++++++++++++++++++++++++++ src/priority_queue.c | 130 ++++++++++++++++++++++++++++++++++ src/queue.c | 87 +++++++++++++++++++++++ src/vector.c | 147 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 575 insertions(+) create mode 100644 include/arch.h create mode 100644 include/coor.h create mode 100644 include/priority_queue.h create mode 100644 include/queue.h create mode 100644 include/vector.h create mode 100644 src/priority_queue.c create mode 100644 src/queue.c create mode 100644 src/vector.c diff --git a/include/arch.h b/include/arch.h new file mode 100644 index 0000000..e643d97 --- /dev/null +++ b/include/arch.h @@ -0,0 +1,10 @@ +#ifndef ARCH_H +#define ARCH_H + +typedef struct arch{ + int cost; + int a; + int b; +} arch; + +#endif \ No newline at end of file diff --git a/include/coor.h b/include/coor.h new file mode 100644 index 0000000..95f801a --- /dev/null +++ b/include/coor.h @@ -0,0 +1,9 @@ +#ifndef COOR_H +#define COOR_H + +typedef struct coor{ + int x; + int y; +} coor; + +#endif \ No newline at end of file diff --git a/include/priority_queue.h b/include/priority_queue.h new file mode 100644 index 0000000..5f5c3e9 --- /dev/null +++ b/include/priority_queue.h @@ -0,0 +1,40 @@ +#ifndef PRIORITY_QUEUE_H +#define PRIORITY_QUEUE_H + +#define PRIORITY_QUEUE_INIT_GROWTH_FACT 2 + +#define priority_queue(pq, compare) priority_queue* pq = newPriority_queue(compare); + +typedef struct priority_queue{ + void** at; + int capacity; + int size; + + int growthFactor; + + void (*toString)(); + bool (*compare)(); +} priority_queue; + + +priority_queue* newPriority_queue(bool (*compare)()); + +void priority_queueResize(priority_queue* v, int capacity); + +void priority_queueClear(priority_queue* pq); + +void priority_queuePush(priority_queue* pq, void* element); + +void priority_queueFree(priority_queue* pq); + +void priority_queueShrink_to_fit(priority_queue* pq); + +bool priority_queueIsEmpty(priority_queue* pq); + +void* priority_queueTop(priority_queue* pq); + +void priority_queuePop(priority_queue* pq); + +void priority_queuePrint(priority_queue* pq); + +#endif \ No newline at end of file diff --git a/include/queue.h b/include/queue.h new file mode 100644 index 0000000..4db3302 --- /dev/null +++ b/include/queue.h @@ -0,0 +1,48 @@ +#ifndef QUEUE_H +#define QUEUE_H + +#include + +/* + * Macro to create a new queue. + */ +#define queue(q) queue* q = newQueue(); + +typedef struct queue_element{ + void* next; + void* element; +} queue_element; + +typedef struct queue{ + + int size; /* The number of at in use */ + queue_element* front; /* pointer to the first element of the queue */ + queue_element* back; /* pointer to the back element of the queue */ + + void (*toString)(); /* Pointer to toString function */ +} queue; + + +/* + * Creates new queue. + */ +queue* newQueue(); + +/* + * Adds a new element to the queue. + */ +void queuePush(queue* q, void* element); + +void queuePrint(queue* q); + +void queuePop(queue* q); + +bool queueIsEmpty(queue* q); + +void queueMerge(queue* q1, queue* q2); + +void queueClear(queue* q); + +void queueFree(queue* q); + +#endif \ No newline at end of file diff --git a/include/vector.h b/include/vector.h new file mode 100644 index 0000000..e4b6468 --- /dev/null +++ b/include/vector.h @@ -0,0 +1,104 @@ +#ifndef VECTOR_H +#define VECTOR_H + +#include + +/* + * Implementation notes: + * --------------------------------------------------------------------------- + * Use vector(vec_name) to declare a new vector (no manual allocation needed). + * the elements of the Vector are stored in a dynamic array. + * If the space in the array is ever exhausted, the implementation + * multiplies the array's capacity by the growthFactor (2 by default). + * + * To use vectorSort, the compare functions needs to be defined. + * To use vectorPrint, the toString function needs to be defined. + */ + + +/* + * Macro to declare a new vector. + */ +#define vector(vec) vector* vec = newVector(); + +typedef struct vector{ + + void** at; /* A dynamic array of the pointers to at */ + int capacity; /* The allocated size of the array */ + int size; /* The number of at in use */ + + int growthFactor; /* How much space the vector allocates when full */ + + void (*toString)(); /* Pointer to toString function */ + bool (*compare)(); /* Pointer to comparator compare */ +} vector; + + +/* + * Creates new vector. + */ +vector* newVector(); + +/* + * Adds a new value at the end of the vector. + */ +void vectorPush_back(vector* v, void* element); + +/* + * Removes the last element. + */ +void vectorPop_back(vector* v); + +/* + * Inserts a new element in the vector. + */ +void vectorInsert(vector* v, void* element, int pos); + +/* + * Removes an element in the vector. + */ +void vectorErase(vector* v, int pos); + +/* + * Resizes the capacity. + */ +void vectorResize(vector* v, int capacity); + +/* + * Clears all elements. + */ +void vectorClear(vector* v); + +/* + * Swaps two elements. + */ +void vectorSwap(void** a, void** b); + +/* + * Converts the vector to a printable string representation. + * ToString need to be defined. + */ +void vectorPrint(vector* v); + +/* + * Sorts the vector using merge sort. + * Compare needs to be defined. + */ +void vectorSort(vector* v); + +/* + * Makes vector capacity equal to size. + */ +void vectorShrink_to_fit(vector* v); + +/* + * Returns true if this vector contains no element. + */ +bool vectorIsEmpty(vector* v); + +/* + * Frees the allocated memory for the vector. + */ +void vectorFree(vector* v); + +#endif \ No newline at end of file diff --git a/src/priority_queue.c b/src/priority_queue.c new file mode 100644 index 0000000..9c3313d --- /dev/null +++ b/src/priority_queue.c @@ -0,0 +1,130 @@ +#include +#include +#include +#include "priority_queue.h" + + +void priority_queueFixheap(priority_queue* q, int i); +void priority_queueHeapify(priority_queue* queue, int i); +void priority_queueSwap(void** a, void** b); + +priority_queue* newPriority_queue(bool (*compare)()){ + priority_queue* pq = (priority_queue*)malloc(sizeof(priority_queue)); + pq->size = 0; + pq->capacity = 0; + pq->growthFactor = PRIORITY_QUEUE_INIT_GROWTH_FACT; + + pq->compare = compare; + pq->toString = NULL; + + return pq; +} + +void priority_queuePush(priority_queue* pq, void* element){ + if(pq->size == pq->capacity){ + if(pq->capacity) + priority_queueResize(pq, pq->capacity * pq->growthFactor); + else + priority_queueResize(pq, 1); + } + + pq->at[pq->size] = element; + pq->size++; + + priority_queueFixheap(pq, pq->size-1); + +} + +void priority_queueFixheap(priority_queue* pq, int i){ + if(!i) + return; + if(pq->compare(pq->at[(i-1)/2], pq->at[i])) + priority_queueSwap(pq->at[i], pq->at[(i-1)/2]); + + priority_queueFixheap(pq, (i-1)/2); +} + +void priority_queuePop(priority_queue* pq){ + if(!pq->size){ + fprintf(stderr, "POP ON EMPTY PRIORITY_QUEUE\n"); + return; + } + + priority_queueSwap(pq->at[0], pq->at[pq->size-1]); + pq->size--; + priority_queueHeapify(pq, 0); +} + +void priority_queueHeapify(priority_queue* pq, int i){ + if(i >= (pq->size)/2) + return; + + int minson; + if(pq->size-1 == 2*i+1 || !pq->compare(pq->at[2*i+1], pq->at[2*i+2])) + minson = 2*i+1; + else + minson = 2*i+2; + + if(pq->compare(pq->at[i], pq->at[minson])){ + priority_queueSwap(pq->at[i], pq->at[minson]); + priority_queueHeapify(pq, minson); + } +} + +void priority_queueSwap(void** a, void** b){ + void* tmp = *a; + *a = *b; + *b = tmp; +} + +void priority_queueResize(priority_queue* pq, int capacity){ + for(int i=pq->size; i>capacity; i--) + free(pq->at[i-1]); + + if(capacity){ + void **at = realloc(pq->at, sizeof(void *) * capacity); + pq->at = at; + pq->capacity = capacity; + }else{ + free(pq->at); + } +} + +void priority_queueClear(priority_queue* pq){ + priority_queueResize(pq, 0); + pq->size = 0; +} + +void priority_queueFree(priority_queue* pq){ + priority_queueClear(pq); + free(pq); +} + +void priority_queueShrink_to_fit(priority_queue* pq){ + priority_queueResize(pq, pq->size); +} + +bool priority_queueIsEmpty(priority_queue* pq){ + return pq->size > 0 ? false : true; +} + +void* priority_queueTop(priority_queue* pq){ + if(!pq->size){ + fprintf(stderr, "PRIORITY_QUEUE IS EMPTY\n"); + return NULL; + } + + return pq->at[0]; +} + +void priority_queuePrint(priority_queue* pq){ + if(pq->toString == NULL){ + fprintf(stderr, "TOSTRING NOT DEFINED\n"); + return; + } + + for(int i=0; isize; i++) + pq->toString(pq->at[i]); + + printf("\n"); +} \ No newline at end of file diff --git a/src/queue.c b/src/queue.c new file mode 100644 index 0000000..68d40c0 --- /dev/null +++ b/src/queue.c @@ -0,0 +1,87 @@ +#include +#include +#include "queue.h" + +queue* newQueue(){ + queue* tmp = (queue*)malloc(sizeof(queue)); + tmp->size = 0; + + tmp->toString = NULL; + tmp->front = NULL; + tmp->back = NULL; + + return tmp; +} + +void queuePush(queue* q, void* element){ + queue_element* qel = (queue_element*)malloc(sizeof(queue_element)); + qel->element = element; + qel->next = NULL; + + if(queueIsEmpty(q)){ + q->front = qel; + q->back = qel; + }else{ + q->back->next = qel; + q->back = qel; + } + + q->size++; +} + +void queuePrint(queue* q){ + if(q->toString == NULL){ + fprintf(stderr, "TOSTRING NOT DEFINED\n"); + return; + } + + queue_element* tmp = q->front; + while(tmp){ + q->toString(tmp->element); + tmp = tmp->next; + printf("\n"); + } + + printf("\n"); +} + +void queuePop(queue* q){ + if(queueIsEmpty(q)){ + fprintf(stderr, "QUEUE IS EMTPY\n"); + return; + } + + queue_element* tmp = q->front->next; + free(q->front->element); + free(q->front); + q->front = tmp; + + q->size--; +} + +bool queueIsEmpty(queue* q){ + return q->size == 0; +} + +void queueMerge(queue* q1, queue* q2){ + q1->back->next = q2->front; + q1->size += q2->size; + q2->size = 0; +} + +void queueClear(queue* q){ + queue_element* tmp = q->front, *tp; + while(tmp){ + tp = tmp->next; + free(tmp); + tmp = tp; + } + + q->front = NULL; + q->size = 0; +} + +void queueFree(queue* q){ + queueClear(q); + free(q); +} diff --git a/src/vector.c b/src/vector.c new file mode 100644 index 0000000..fca82ac --- /dev/null +++ b/src/vector.c @@ -0,0 +1,147 @@ +#include +#include +#include "vector.h" + +void mergeSort(vector* v, bool (*compare)(), int i, int j); + +vector* newVector(){ + vector* v = (vector *)malloc(sizeof(vector)); + v->size = 0; + v->capacity = 0; + + v->growthFactor = 2; + + v->compare = NULL; + v->toString = NULL; + + return v; +} + +void vectorPush_back(vector* v, void* element){ + if(v->size == v->capacity){ + if(v->capacity) + vectorResize(v, v->capacity * v->growthFactor); + else + vectorResize(v, 1); + } + + v->at[v->size] = element; + v->size++; +} + +void vectorPop_back(vector* v){ + if(!v->size) + return; + + free(v->at[v->size-1]); + v->size--; +} + +void vectorInsert(vector* v, void* element, int pos){ + if(pos > v->size || pos < 0){ + fprintf(stderr, "INSERT INVALID\n"); + return; + } + + vectorPush_back(v, v->at[v->size-1]); + for(int i=v->size-1; i>pos; i--) + v->at[i] = v->at[i-1]; + + v->at[pos] = element; +} + +void vectorErase(vector* v, int pos){ + if(pos > v->size || pos < 0){ + fprintf(stderr, "ERASE INVALID\n"); + return; + } + + free(v->at[pos]); + + for(int i=pos; isize; i++){ + v->at[i] = v->at[i+1]; + } + v->size--; +} + +void vectorResize(vector* v, int capacity){ + for(int i=v->size; i>capacity; i--) + free(v->at[i-1]); + + + void **at = realloc(v->at, sizeof(void *) * capacity); + v->at = at; + v->capacity = capacity; +} + +void vectorClear(vector* v){ + vectorResize(v, 0); + v->size = 0; +} + +void vectorSwap(void** a, void** b){ + void* tmp = *a; + *a = *b; + *b = tmp; +} + +void vectorPrint(vector* v){ + if(v->toString == NULL){ + fprintf(stderr, "TOSTRING NOT DEFINED\n"); + return; + } + + for(int i=0; isize; i++) + v->toString(v->at[i]); + + printf("\n"); +} + +void vectorSort(vector* v){ + if(!v->compare){ + fprintf(stderr, "compare NOT DEFINED\n"); + return; + } + + mergeSort(v, v->compare, 0, v->size); +} + +void mergeSort(vector* v, bool (*compare)(), int i, int j){ + if(i == j-1) + return; + + mergeSort(v, compare, i, (i+j)/2); + mergeSort(v, compare, (i+j)/2, j); + + vector(tmp); + int h = i, k = (i+j)/2; + + while(h<(i+j)/2 || kat[h], v->at[k]))){ + vectorPush_back(tmp, v->at[k]); + k++; + }else{ + vectorPush_back(tmp, v->at[h]); + h++; + } + } + + for(h=i; hat[h] = tmp->at[h-i]; + + free(tmp->at); + free(tmp); +} + +void vectorShrink_to_fit(vector* v){ + vectorResize(v, v->size); +} + +bool vectorIsEmpty(vector* v){ + return v->size > 0 ? false : true; +} + +void vectorFree(vector *v){ + vectorClear(v); + free(v); +}