mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-11 08:20:38 +08:00
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
260 lines
6.4 KiB
C
260 lines
6.4 KiB
C
/**
|
|
* File: vector.h
|
|
* Created Time: 2023-07-13
|
|
* Author: Zuoxun (845242523@qq.com)、Gonglja (glj0@outlook.com)
|
|
*/
|
|
|
|
#ifndef VECTOR_H
|
|
#define VECTOR_H
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/* Define the vector type */
|
|
typedef struct vector {
|
|
int size; // Current vector size
|
|
int capacity; // Current vector capacity
|
|
int depth; // Current vector depth
|
|
void **data; // Array of pointers to the data
|
|
} vector;
|
|
|
|
/* Construct a vector */
|
|
vector *newVector() {
|
|
vector *v = malloc(sizeof(vector));
|
|
v->size = 0;
|
|
v->capacity = 4;
|
|
v->depth = 1;
|
|
v->data = malloc(v->capacity * sizeof(void *));
|
|
return v;
|
|
}
|
|
|
|
/* Construct a vector with the specified size and default element value */
|
|
vector *_newVector(int size, void *elem, int elemSize) {
|
|
vector *v = malloc(sizeof(vector));
|
|
v->size = size;
|
|
v->capacity = size;
|
|
v->depth = 1;
|
|
v->data = malloc(v->capacity * sizeof(void *));
|
|
for (int i = 0; i < size; i++) {
|
|
void *tmp = malloc(sizeof(char) * elemSize);
|
|
memcpy(tmp, elem, elemSize);
|
|
v->data[i] = tmp;
|
|
}
|
|
return v;
|
|
}
|
|
|
|
/* Destroy the vector */
|
|
void delVector(vector *v) {
|
|
if (v) {
|
|
if (v->depth == 0) {
|
|
return;
|
|
} else if (v->depth == 1) {
|
|
for (int i = 0; i < v->size; i++) {
|
|
free(v->data[i]);
|
|
}
|
|
free(v);
|
|
} else {
|
|
for (int i = 0; i < v->size; i++) {
|
|
delVector(v->data[i]);
|
|
}
|
|
v->depth--;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Append a copy of an element to the vector */
|
|
void vectorPushback(vector *v, void *elem, int elemSize) {
|
|
if (v->size == v->capacity) {
|
|
v->capacity *= 2;
|
|
v->data = realloc(v->data, v->capacity * sizeof(void *));
|
|
}
|
|
void *tmp = malloc(sizeof(char) * elemSize);
|
|
memcpy(tmp, elem, elemSize);
|
|
v->data[v->size++] = tmp;
|
|
}
|
|
|
|
/* Pop the last element from the vector */
|
|
void vectorPopback(vector *v) {
|
|
if (v->size != 0) {
|
|
free(v->data[v->size - 1]);
|
|
v->size--;
|
|
}
|
|
}
|
|
|
|
/* Clear the vector */
|
|
void vectorClear(vector *v) {
|
|
delVector(v);
|
|
v->size = 0;
|
|
v->capacity = 4;
|
|
v->depth = 1;
|
|
v->data = malloc(v->capacity * sizeof(void *));
|
|
}
|
|
|
|
/* Get the vector size */
|
|
int vectorSize(vector *v) {
|
|
return v->size;
|
|
}
|
|
|
|
/* Get the last element of the vector */
|
|
void *vectorBack(vector *v) {
|
|
int n = v->size;
|
|
return n > 0 ? v->data[n - 1] : NULL;
|
|
}
|
|
|
|
/* Get the first element of the vector */
|
|
void *vectorFront(vector *v) {
|
|
return v->size > 0 ? v->data[0] : NULL;
|
|
}
|
|
|
|
/* Get the vector element at index pos */
|
|
void *vectorAt(vector *v, int pos) {
|
|
if (pos < 0 || pos >= v->size) {
|
|
printf("vectorAt: out of range\n");
|
|
return NULL;
|
|
}
|
|
return v->data[pos];
|
|
}
|
|
|
|
/* Set the vector element at index pos */
|
|
void vectorSet(vector *v, int pos, void *elem, int elemSize) {
|
|
if (pos < 0 || pos >= v->size) {
|
|
printf("vectorSet: out of range\n");
|
|
return;
|
|
}
|
|
free(v->data[pos]);
|
|
void *tmp = malloc(sizeof(char) * elemSize);
|
|
memcpy(tmp, elem, elemSize);
|
|
v->data[pos] = tmp;
|
|
}
|
|
|
|
/* Grow the vector capacity */
|
|
void vectorExpand(vector *v) {
|
|
v->capacity *= 2;
|
|
v->data = realloc(v->data, v->capacity * sizeof(void *));
|
|
}
|
|
|
|
/* Shrink the vector capacity */
|
|
void vectorShrink(vector *v) {
|
|
v->capacity /= 2;
|
|
v->data = realloc(v->data, v->capacity * sizeof(void *));
|
|
}
|
|
|
|
/* Insert an element at vector index pos */
|
|
void vectorInsert(vector *v, int pos, void *elem, int elemSize) {
|
|
if (v->size == v->capacity) {
|
|
vectorExpand(v);
|
|
}
|
|
for (int j = v->size; j > pos; j--) {
|
|
v->data[j] = v->data[j - 1];
|
|
}
|
|
void *tmp = malloc(sizeof(char) * elemSize);
|
|
memcpy(tmp, elem, elemSize);
|
|
v->data[pos] = tmp;
|
|
v->size++;
|
|
}
|
|
|
|
/* Remove the element at vector index pos */
|
|
void vectorErase(vector *v, int pos) {
|
|
if (v->size != 0) {
|
|
free(v->data[pos]);
|
|
for (int j = pos; j < v->size - 1; j++) {
|
|
v->data[j] = v->data[j + 1];
|
|
}
|
|
v->size--;
|
|
}
|
|
}
|
|
|
|
/* Swap vector elements */
|
|
void vectorSwap(vector *v, int i, int j) {
|
|
void *tmp = v->data[i];
|
|
v->data[i] = v->data[j];
|
|
v->data[j] = tmp;
|
|
}
|
|
|
|
/* Check whether the vector is empty */
|
|
bool vectorEmpty(vector *v) {
|
|
return v->size == 0;
|
|
}
|
|
|
|
/* Check whether the vector is full */
|
|
bool vectorFull(vector *v) {
|
|
return v->size == v->capacity;
|
|
}
|
|
|
|
/* Check whether the vectors are equal */
|
|
bool vectorEqual(vector *v1, vector *v2) {
|
|
if (v1->size != v2->size) {
|
|
printf("Sizes are not equal\n");
|
|
return false;
|
|
}
|
|
for (int i = 0; i < v1->size; i++) {
|
|
void *a = v1->data[i];
|
|
void *b = v2->data[i];
|
|
if (memcmp(a, b, sizeof(a)) != 0) {
|
|
printf("Data at index %d are not equal\n", i);
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/* Sort the elements within the vector */
|
|
void vectorSort(vector *v, int (*cmp)(const void *, const void *)) {
|
|
qsort(v->data, v->size, sizeof(void *), cmp);
|
|
}
|
|
|
|
/* Print the vector using a supplied element-printing function */
|
|
/* Currently supports printing only vectors of depth 1 */
|
|
void printVector(vector *v, void (*printFunc)(vector *v, void *p)) {
|
|
if (v) {
|
|
if (v->depth == 0) {
|
|
return;
|
|
} else if (v->depth == 1) {
|
|
if(v->size == 0) {
|
|
printf("\n");
|
|
return;
|
|
}
|
|
for (int i = 0; i < v->size; i++) {
|
|
if (i == 0) {
|
|
printf("[");
|
|
} else if (i == v->size - 1) {
|
|
printFunc(v, v->data[i]);
|
|
printf("]\r\n");
|
|
break;
|
|
}
|
|
printFunc(v, v->data[i]);
|
|
printf(",");
|
|
}
|
|
} else {
|
|
for (int i = 0; i < v->size; i++) {
|
|
printVector(v->data[i], printFunc);
|
|
}
|
|
v->depth--;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Currently supports printing only vectors of depth 2 */
|
|
void printVectorMatrix(vector *vv, void (*printFunc)(vector *v, void *p)) {
|
|
printf("[\n");
|
|
for (int i = 0; i < vv->size; i++) {
|
|
vector *v = (vector *)vv->data[i];
|
|
printf(" [");
|
|
for (int j = 0; j < v->size; j++) {
|
|
printFunc(v, v->data[j]);
|
|
if (j != v->size - 1)
|
|
printf(",");
|
|
}
|
|
printf("],");
|
|
printf("\n");
|
|
}
|
|
printf("]\n");
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif // VECTOR_H
|