Added initial files
This commit is contained in:
parent
a23ddc8017
commit
71b59b60e2
|
@ -0,0 +1,10 @@
|
||||||
|
#ifndef ARCH_H
|
||||||
|
#define ARCH_H
|
||||||
|
|
||||||
|
typedef struct arch{
|
||||||
|
int cost;
|
||||||
|
int a;
|
||||||
|
int b;
|
||||||
|
} arch;
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef COOR_H
|
||||||
|
#define COOR_H
|
||||||
|
|
||||||
|
typedef struct coor{
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
} coor;
|
||||||
|
|
||||||
|
#endif
|
|
@ -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
|
|
@ -0,0 +1,48 @@
|
||||||
|
#ifndef QUEUE_H
|
||||||
|
#define QUEUE_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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
|
|
@ -0,0 +1,104 @@
|
||||||
|
#ifndef VECTOR_H
|
||||||
|
#define VECTOR_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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
|
|
@ -0,0 +1,130 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#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; i<pq->size; i++)
|
||||||
|
pq->toString(pq->at[i]);
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
}
|
|
@ -0,0 +1,87 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#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);
|
||||||
|
}
|
|
@ -0,0 +1,147 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#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; i<v->size; 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; i<v->size; 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 || k<j){
|
||||||
|
if(h==(i+j)/2 || (k<j && compare(v->at[h], v->at[k]))){
|
||||||
|
vectorPush_back(tmp, v->at[k]);
|
||||||
|
k++;
|
||||||
|
}else{
|
||||||
|
vectorPush_back(tmp, v->at[h]);
|
||||||
|
h++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(h=i; h<j; h++)
|
||||||
|
v->at[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);
|
||||||
|
}
|
Loading…
Reference in New Issue