用c语言实现了一个简单的Vector,支持泛型,能动态的改变自身大小的容器
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
typedef struct Vector {
void* items; // 迭代器
size_t items_size; // 单个元素大小
size_t size; // 使用的大小(有多少个成员)
size_t capacity; // 总容量
} Vector;
// 构造函数 实现vector初始化容量 分配内存
void Vector_constructor(Vector* vector, size_t items_size, size_t capacity) {
vector->items_size = items_size;
vector->size = 0;
vector->capacity = capacity;
vector->items = malloc(capacity * items_size);
if (vector->items == NULL) {
printf("Vector_constructor error!");
exit(0);
}
}
//析构函数 实现vector 释放内存,容量初始化
void Vector_destructor(Vector* vector) {
free(vector->items);
vector->items = NULL;
vector->size = 0;
vector->capacity = 0;
}
// 返回使用的大小
size_t Vector_size(Vector* vector) {
return vector->size;
}
//返回容量
size_t Vector_capacity(Vector* vector) {
return vector->capacity;
}
//返回数据指针
void* Vector_data(Vector* vector) {
return vector->items;
}
// realloc 重新分配内存
void Vector_resize(Vector* vector, size_t newSize) {
if (newSize > Vector_capacity(vector)) {
void* new_items = realloc(Vector_data(vector), newSize * vector->items_size);
if (!new_items) {
printf("realloc error!\n");
return;
}
vector->items = new_items;
vector->capacity = newSize;
}
vector->size = newSize;
}
// 返回一个位置的指针,如果越界就扩容
void* Vector_at(Vector* vector, size_t index) {
if (index >= Vector_capacity(vector)) {
size_t newSize = index + 1;
Vector_resize(vector, newSize);
}
return (char*)vector->items + (index * vector->items_size);
}
//插入元素
void Vector_set(Vector* vector, size_t index, void* data) {
if (index >= vector->size) {
Vector_resize(vector, index + 1);
vector->size = index + 1;
}
void* item_ptr = (char*)vector->items + (index * vector->items_size);
memcpy(item_ptr, data, vector->items_size);
}
int main() {
Vector v;
Vector_constructor(&v, sizeof(int), 2);
int value0 = 0, value1 = 1;
// 添加两个元素
Vector_set(&v, 0, &value0);
Vector_set(&v, 1, &value1);
//单元测试
//测试大小 测试数据有没有被添加成功
assert(Vector_size(&v) == 2);
for (int i = 0; i < 2; i++) {
assert(("Test data", *((int*)Vector_data(&v) + i) == i));
}
//单元测试
for (size_t i = 0; i < Vector_size(&v); i++) {
printf("%d ", *((int*)Vector_data(&v) + i));
}
printf("\n");
printf("Vector_size: %d", Vector_size(&v));
printf("\n");
int valueN[15];
for (int i = 2; i < 15; i++) {
valueN[i] = i;
Vector_set(&v, i, &valueN[i]);
}
//单元测试
//测试大小 测试数组有没有成功扩容
assert(Vector_size(&v) == 15);
for (int i = 0; i < 15; i++) {
assert(("Test data", *((int*)Vector_data(&v) + i) == i));
}
//单元测试
for (size_t i = 0; i < Vector_size(&v); i++) {
printf("%d ", *((int*)Vector_data(&v) + i));
}
printf("\n");
printf("Vector_size: %d", Vector_size(&v));
printf("\n");
Vector_constructor(&v, sizeof(char), 2);
char* s = (char*)"abc";
for (size_t i = 0; i < 3; i++)
{
Vector_set(&v,i,s+i);
}
//单元测试
//测试大小 测试泛型
assert(Vector_size(&v) == 3);
for (int i = 0; i < 3; i++) {
assert(("Test data", *((char*)Vector_data(&v) + i) == s[i]));
}
//单元测试
for (size_t i = 0; i < Vector_size(&v); i++) {
printf("%c ", *((char*)Vector_data(&v) + i));
}
printf("\n");
return 0;
}
标签:Vector,实现,items,printf,vector,简单,data,size
From: https://www.cnblogs.com/nyyyddddn/p/17810807.html