Files
hello-algo/ko/codes/c/utils/vector.h
T
Yudong Jin 2f561d3d51 feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition

* Use UTF-8 for Python CI output on Windows

* Remove print-book promotion from the Korean site

* fix(ko): preserve emphasis before Korean particles
2026-10-29 03:46:43 +08:00

260 lines
6.4 KiB
C

/**
* File: vector.h
* Created Time: 2023-07-13
* Author: Zuoxun ([email protected])、Gonglja ([email protected])
*/
#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;
}
/* 크기와 원소 기본값을 지정하여 벡터 생성 */
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) {
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--;
}
}
}
/* 벡터 끝에 원소를 복사하여 추가한다 */
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;
}
/* 벡터 끝에서 원소를 꺼낸다 */
void vectorPopback(vector *v) {
if (v->size != 0) {
free(v->data[v->size - 1]);
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) {
int n = v->size;
return n > 0 ? v->data[n - 1] : NULL;
}
/* 벡터의 첫 원소 반환 */
void *vectorFront(vector *v) {
return v->size > 0 ? v->data[0] : NULL;
}
/* 벡터에서 인덱스가 pos인 원소 반환 */
void *vectorAt(vector *v, int pos) {
if (pos < 0 || pos >= v->size) {
printf("vectorAt: 범위를 벗어났다\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: 범위를 벗어났다\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("크기가 다르다\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("데이터 %d의 값이 다르다\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만 출력할 수 있다 */
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--;
}
}
}
/* 현재는 깊이가 2인 vector만 출력할 수 있다 */
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