2023-07-29 15:14:01 +08:00
|
|
|
/**
|
|
|
|
* File: vector.h
|
|
|
|
* Created Time: 2023-07-13
|
|
|
|
* Author: Gonglja (glj0@outlook.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef VECTOR_H
|
|
|
|
#define VECTOR_H
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 定义向量类型 */
|
|
|
|
typedef struct vector {
|
|
|
|
int size; // 当前向量的大小
|
|
|
|
int capacity; // 当前向量的容量
|
|
|
|
int depth; // 当前向量的深度
|
|
|
|
void **data; // 指向数据的指针数组
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 析构向量 */
|
|
|
|
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--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:48:10 +08:00
|
|
|
/* 添加元素(拷贝方式)到向量尾部 */
|
|
|
|
void vectorPushback(vector *v, void *elem, int elemSize) {
|
2023-07-29 15:14:01 +08:00
|
|
|
if (v->size == v->capacity) {
|
|
|
|
v->capacity *= 2;
|
|
|
|
v->data = realloc(v->data, v->capacity * sizeof(void *));
|
|
|
|
}
|
2023-08-03 14:48:10 +08:00
|
|
|
void *tmp = malloc(sizeof(char) * elemSize);
|
|
|
|
memcpy(tmp, elem, elemSize);
|
|
|
|
v->data[v->size++] = tmp;
|
2023-07-29 15:14:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 从向量尾部弹出元素 */
|
|
|
|
void vectorPopback(vector *v) {
|
|
|
|
if (v->size != 0) {
|
2023-08-03 14:48:10 +08:00
|
|
|
free(v->data[v->size - 1]);
|
2023-07-29 15:14:01 +08:00
|
|
|
v->size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 清空向量 */
|
|
|
|
void vectorClear(vector *v) {
|
|
|
|
delVector(v);
|
|
|
|
v->size = 0;
|
|
|
|
v->capacity = 4;
|
|
|
|
v->depth = 1;
|
|
|
|
v->data = malloc(v->capacity * sizeof(void *));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取向量的大小 */
|
|
|
|
int vectorSize(vector *v) {
|
|
|
|
return v->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取向量的尾元素 */
|
|
|
|
void *vectorBack(vector *v) {
|
|
|
|
return v->data[v->size];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取向量的头元素 */
|
|
|
|
void *vectorFront(vector *v) {
|
|
|
|
return v->data[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 打印函数, 需传递一个打印变量的函数进来 */
|
|
|
|
/* 当前仅支持打印深度为 1 的 vector*/
|
|
|
|
void printVector(vector *v, void (*printFunc)(vector *v, void *p)) {
|
|
|
|
if (v) {
|
|
|
|
if (v->depth == 0) {
|
|
|
|
return;
|
|
|
|
} else if (v->depth == 1) {
|
|
|
|
for (int i = 0; i < v->size; i++) {
|
2023-08-03 14:48:10 +08:00
|
|
|
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(",");
|
2023-07-29 15:14:01 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < v->size; i++) {
|
|
|
|
printVector(v->data[i], printFunc);
|
|
|
|
}
|
|
|
|
v->depth--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 当前仅支持打印深度为 2 的 vector */
|
|
|
|
void printVectorMatrix(vector *vv, void (*printFunc)(vector *v, void *p)) {
|
2023-08-03 14:48:10 +08:00
|
|
|
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");
|
2023-07-29 15:14:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // VECTOR_H
|