104 lines
2.2 KiB
C
104 lines
2.2 KiB
C
#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 |