c
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct vector {
bool empty;
bool full;
size_t sizeof_data;
void *data;
void *top;
void *tail;
size_t len;
size_t capacity;
};
// NOTE: init capacity what you need
// NOTE: really using index
struct vector *VectorNew(size_t data, size_t capacity) {
struct vector *instance = (struct vector *)calloc(sizeof(struct vector), 1);
if (instance == 0) {
return 0;
}
instance->data = calloc(data, capacity);
instance->empty = true;
instance->len = 1;
instance->sizeof_data = data;
instance->top = instance->data;
instance->tail = instance->data;
instance->capacity = capacity;
return instance;
}
int32_t VectorPush(struct vector *vec, void *data) {
if (vec == 0 || data == 0) {
return -1;
}
vec->empty = false;
if (vec->len >= vec->capacity) {
vec->full = true;
return -2;
}
vec->len++;
memcpy(vec->tail, data, vec->sizeof_data);
vec->tail += vec->sizeof_data;
return 0;
}
// NOTE: check again
int32_t isVectorFull(struct vector *vec) {
if (vec == 0) {
return -1;
}
if (vec->len >= vec->capacity)
vec->full = true;
return vec->full;
}
int32_t isVectorEmpty(struct vector *vec) {
if (vec == 0) {
return -1;
}
if (vec->len == 0) {
vec->empty = true;
}
return vec->empty;
}
// NOTE: inefficiency
int32_t VectorPop(struct vector *vec, void *data) {
if (vec == 0 || data == 0) {
return -1;
}
if ((--vec->len) <= 1) {
vec->empty = true;
return -2;
}
memcpy(data, vec->top, vec->sizeof_data);
memmove(vec->top, vec->top + vec->sizeof_data, (vec->len) * vec->sizeof_data);
vec->tail = vec->top + vec->sizeof_data * vec->len;
return 0;
}
int32_t VectorPop_back(struct vector *vec, void *data) {
if (vec == 0 || data == 0) {
return -1;
}
if (vec->len < 1) {
vec->len = 1;
vec->empty = true;
return -2;
}
memcpy(data, vec->tail - vec->sizeof_data, vec->sizeof_data);
vec->len--;
vec->tail = vec->top + vec->len * vec->sizeof_data;
return 0;
}
int32_t VectorSize(struct vector *vec) {
if (vec == 0) {
return -1;
}
return vec->len;
}
struct vector *VectorTail(struct vector *vec) {
return vec->tail - vec->sizeof_data;
}
struct vector *VectorTop(struct vector *vec) { return vec->top; }
// NOTE: inefficiency
int32_t VectorDelete(struct vector *vec, int32_t index) {
if (vec == 0) {
return -1;
}
if (index > vec->len - 1) {
return -2;
}
vec->len--;
memmove(vec->data + index * vec->sizeof_data,
vec->data + (index + 1) * (vec->sizeof_data),
vec->sizeof_data * index);
vec->len = vec->len < 1 ? 1 : vec->len;
vec->top = vec->data;
vec->tail = vec->top + vec->sizeof_data * vec->len;
return 0;
}
int32_t VectorGet(struct vector *vec, int32_t index, void *data) {
if (vec == 0) {
return 0;
}
if (vec->len == 1) {
return 0;
}
if (index != 0 && index > vec->len - 1) {
return 0;
}
memcpy(data, vec->data + vec->sizeof_data * index, vec->sizeof_data);
return 0;
}
int32_t VectorSet(struct vector *vec, int32_t index, void *data) {
if (vec == 0 && data != 0) {
return -1;
}
if (vec->len == 1) {
return -3;
}
if (index != 0 && index > vec->len - 1) {
return -2;
}
memcpy(vec->data + index * vec->sizeof_data, data, vec->sizeof_data);
return 0;
}
int32_t VectorFree(struct vector *vec) {
if (vec != 0)
free(vec);
vec = 0;
return 0;
}
int32_t VectorResize(struct vector *vec, size_t capacity) {
if (vec == 0) {
return -1;
}
if (capacity <= vec->capacity) {
return 0;
}
vec->data = realloc(vec->data, capacity * vec->sizeof_data);
if (vec->data == 0) {
return -1;
}
vec->top = vec->data;
vec->tail = vec->top + vec->sizeof_data * vec->len;
vec->capacity = capacity;
return 0;
}
struct testData {
char name[5];
int32_t i;
void *ptr;
};
int main(void) {
struct vector *vec = VectorNew(sizeof(struct testData), 4);
if (vec == 0) {
printf("create vector err\n");
return 0;
}
struct testData tmp[] = {"1234", 1};
struct testData tmp_1[] = {"6790", 2};
struct testData tmp_2[] = {"abde", 3};
struct testData tmp_3[] = {"fgij", 4};
VectorPush(vec, tmp);
VectorPush(vec, tmp_1);
VectorPush(vec, tmp_2);
// char b;
// while (VectorPop(vec, &b) == 0) {
// printf("b number:%d\n", b);
// }
// printf("vec len %ld\n", vec->len);
// if (VectorGet(vec, 0) == 0 && isVectorEmpty(vec)) {
// printf("VEC EMPTY \n");
// }
struct testData ctmp;
VectorPop_back(vec, &ctmp);
printf("Pop back data %s\n", ctmp.name);
for (int32_t i = 0; i < VectorSize(vec); i++) {
VectorGet(vec, i, &ctmp);
printf("ctmp %s\n", ctmp.name);
}
return 0;
}
有些算法容器,还真不是,随便用,那vector 来说,pop 的效率 和 resize 的效率都很低
关键C 没有可用,放心的 算法容器库,就目前
POP POP_BACK GET SET PUSH 都是没问题,突然发现一个小问题 无语