From 1f512b105f12b376f4de9af2b476662bd6c08b60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=BD=9C=E5=8B=8B?= <59754483+Zuoxun@users.noreply.github.com> Date: Sat, 7 Oct 2023 13:43:30 +0800 Subject: [PATCH] refactor: Extend vector.h implementation (#818) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update vector.h 增加功能列表: 获取向量的第 i 个元素 设置向量的第 i 个元素 向量扩容 向量缩容 向量插入元素 向量删除元素 向量交换元素 向量是否为空 向量是否已满 向量是否相等 对向量内部进行排序(升序/降序) 对向量某段数据排序(升序/降序) * Create hanota.c * 新增binary_search_recur.c * Update vector.h * Delete codes/c/chapter_divide_and_conquer directory * Update vector.h * Create binary_search_recur.c * Delete codes/chapter_divide_and_conquer directory * Update vector.h * Update vector.h --- codes/c/utils/vector.h | 127 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 122 insertions(+), 5 deletions(-) diff --git a/codes/c/utils/vector.h b/codes/c/utils/vector.h index 39bbf65d2..511b92a9b 100644 --- a/codes/c/utils/vector.h +++ b/codes/c/utils/vector.h @@ -1,7 +1,7 @@ /** * File: vector.h * Created Time: 2023-07-13 - * Author: Gonglja (glj0@outlook.com) + * Author: Zuoxun (845242523@qq.com)、Gonglja (glj0@outlook.com) */ #ifndef VECTOR_H @@ -29,6 +29,21 @@ vector *newVector() { return v; } +/* 构造向量,指定大小、元素默认值 */ +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; +} + /* 析构向量 */ void delVector(vector *v) { if (v) { @@ -83,21 +98,123 @@ int vectorSize(vector *v) { /* 获取向量的尾元素 */ void *vectorBack(vector *v) { - return v->data[v->size]; + int n = v->size; + return n > 0 ? v->data[n - 1] : NULL; } /* 获取向量的头元素 */ void *vectorFront(vector *v) { - return v->data[0]; + return v->size > 0 ? v->data[0] : NULL; +} + +/* 获取向量下标 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]; +} + +/* 设置向量下标 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; +} + +/* 向量扩容 */ +void vectorExpand(vector *v) { + v->capacity *= 2; + v->data = realloc(v->data, v->capacity * sizeof(void *)); +} + +/* 向量缩容 */ +void vectorShrink(vector *v) { + v->capacity /= 2; + v->data = realloc(v->data, v->capacity * sizeof(void *)); +} + +/* 在向量下标 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++; +} + +/* 删除向量下标 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--; + } +} + +/* 向量交换元素 */ +void vectorSwap(vector *v, int i, int j) { + void *tmp = v->data[i]; + v->data[i] = v->data[j]; + v->data[j] = tmp; +} + +/* 向量是否为空 */ +bool vectorEmpty(vector *v) { + return v->size == 0; +} + +/* 向量是否已满 */ +bool vectorFull(vector *v) { + return v->size == v->capacity; +} + +/* 向量是否相等 */ +bool vectorEqual(vector *v1, vector *v2) { + if (v1->size != v2->size) { + printf("size 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 %d not equal\n", i); + return false; + } + } + return true; +} + +/* 对向量内部进行排序 */ +void vectorSort(vector *v, int (*cmp)(const void *, const void *)) { + qsort(v->data, v->size, sizeof(void *), cmp); } /* 打印函数, 需传递一个打印变量的函数进来 */ -/* 当前仅支持打印深度为 1 的 vector*/ +/* 当前仅支持打印深度为 1 的 vector */ 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("["); @@ -139,4 +256,4 @@ void printVectorMatrix(vector *vv, void (*printFunc)(vector *v, void *p)) { } #endif -#endif // VECTOR_H \ No newline at end of file +#endif // VECTOR_H