尝试在 C 中制作模板
Trying to make templates in C
我使用宏在 C 中创建了一个通用向量。这个概念是否可行,或者我是否会因为考虑它而获得单程篝火之旅?
#ifndef VECTOR_H
#define VECTOR_H
#define vector_at(vector, pos) ((vector).data[pos])
#define VECTOR_DEFINITION(type)\
typedef struct {\
size_t size;\
size_t capacity;\
type *data;\
} vector_ ## type ## _t;\
void vector_ ## type ## _reserve(vector_ ## type ## _t *vector, size_t size) {\
size_t capacity = 1;\
while (capacity < size) capacity *= 2;\
if (size == 0) capacity = 0;\
if (capacity != vector->capacity)\
{\
vector->capacity = capacity;\
if (size == 0) {\
free(vector->data);\
vector->data = NULL;\
} else {\
vector->data = realloc(vector->data, vector->capacity * sizeof(type));\
}\
}\
}\
void vector_ ## type ## _resize(vector_ ## type ## _t *vector, size_t size) {\
vector->size = size;\
vector_ ## type ## _reserve(vector, size);\
}\
void vector_ ## type ## _push_back(vector_ ## type ## _t *vector, type value) {\
if (vector->size >= vector->capacity) {\
if (vector->capacity == 0) vector->capacity = 1;\
else vector->capacity *= 2;\
vector->data = realloc(vector->data, vector->capacity * sizeof(type));\
}\
vector->data[vector->size++] = value;\
}\
type vector_ ## type ## _pop_back(vector_ ## type ## _t *vector) {\
return vector->data[--vector->size];\
}\
void vector_ ## type ## _init(vector_ ## type ## _t *vector, size_t size) {\
vector->size = size;\
vector->capacity = 0;\
vector->data = NULL;\
vector_ ## type ## _reserve(vector, size);\
}\
void vector_ ## type ## _destroy(vector_ ## type ## _t *vector) {\
free(vector->data);\
vector->size = 0;\
vector->capacity = 0;\
vector->data = NULL;\
}\
#endif
代码可以这样使用:
#include <stdio.h>
#include <stdlib.h>
#include "vector.h"
typedef unsigned int uint;
typedef char* str;
VECTOR_DEFINITION(uint)
VECTOR_DEFINITION(str)
int main()
{
vector_uint_t vector;
vector_uint_init(&vector, 10);
for (unsigned int i = 0; i < vector.size; ++i)
vector.data[i] = i;
for (unsigned int i = 0; i < 10; ++i)
vector_uint_push_back(&vector, i);
for (unsigned int i = 0; i < vector.size; ++i)
printf("%d ", vector.data[i]);
printf("\n");
vector_uint_destroy(&vector);
vector_str_t sentence;
vector_str_init(&sentence, 0);
vector_str_push_back(&sentence, "Hello");
vector_str_push_back(&sentence, "World!");
vector_str_push_back(&sentence, "How");
vector_str_push_back(&sentence, "are");
vector_str_push_back(&sentence, "you?");
for (unsigned int i = 0; i < sentence.size; ++i)
printf("%s ", sentence.data[i]);
printf("\n");
vector_str_destroy(&sentence);
return 0;
}
并产生以下输出:
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
Hello World! How are you?
此外,是否有更好的方法来实现具有类型安全性的通用容器?
评论者认为这个概念是可行的,因此我会考虑回答这个问题。
更多信息,您可以查看以下链接:
- pseudo-generics in C
- simulation of templates in C
谢谢。
我使用宏在 C 中创建了一个通用向量。这个概念是否可行,或者我是否会因为考虑它而获得单程篝火之旅?
#ifndef VECTOR_H
#define VECTOR_H
#define vector_at(vector, pos) ((vector).data[pos])
#define VECTOR_DEFINITION(type)\
typedef struct {\
size_t size;\
size_t capacity;\
type *data;\
} vector_ ## type ## _t;\
void vector_ ## type ## _reserve(vector_ ## type ## _t *vector, size_t size) {\
size_t capacity = 1;\
while (capacity < size) capacity *= 2;\
if (size == 0) capacity = 0;\
if (capacity != vector->capacity)\
{\
vector->capacity = capacity;\
if (size == 0) {\
free(vector->data);\
vector->data = NULL;\
} else {\
vector->data = realloc(vector->data, vector->capacity * sizeof(type));\
}\
}\
}\
void vector_ ## type ## _resize(vector_ ## type ## _t *vector, size_t size) {\
vector->size = size;\
vector_ ## type ## _reserve(vector, size);\
}\
void vector_ ## type ## _push_back(vector_ ## type ## _t *vector, type value) {\
if (vector->size >= vector->capacity) {\
if (vector->capacity == 0) vector->capacity = 1;\
else vector->capacity *= 2;\
vector->data = realloc(vector->data, vector->capacity * sizeof(type));\
}\
vector->data[vector->size++] = value;\
}\
type vector_ ## type ## _pop_back(vector_ ## type ## _t *vector) {\
return vector->data[--vector->size];\
}\
void vector_ ## type ## _init(vector_ ## type ## _t *vector, size_t size) {\
vector->size = size;\
vector->capacity = 0;\
vector->data = NULL;\
vector_ ## type ## _reserve(vector, size);\
}\
void vector_ ## type ## _destroy(vector_ ## type ## _t *vector) {\
free(vector->data);\
vector->size = 0;\
vector->capacity = 0;\
vector->data = NULL;\
}\
#endif
代码可以这样使用:
#include <stdio.h>
#include <stdlib.h>
#include "vector.h"
typedef unsigned int uint;
typedef char* str;
VECTOR_DEFINITION(uint)
VECTOR_DEFINITION(str)
int main()
{
vector_uint_t vector;
vector_uint_init(&vector, 10);
for (unsigned int i = 0; i < vector.size; ++i)
vector.data[i] = i;
for (unsigned int i = 0; i < 10; ++i)
vector_uint_push_back(&vector, i);
for (unsigned int i = 0; i < vector.size; ++i)
printf("%d ", vector.data[i]);
printf("\n");
vector_uint_destroy(&vector);
vector_str_t sentence;
vector_str_init(&sentence, 0);
vector_str_push_back(&sentence, "Hello");
vector_str_push_back(&sentence, "World!");
vector_str_push_back(&sentence, "How");
vector_str_push_back(&sentence, "are");
vector_str_push_back(&sentence, "you?");
for (unsigned int i = 0; i < sentence.size; ++i)
printf("%s ", sentence.data[i]);
printf("\n");
vector_str_destroy(&sentence);
return 0;
}
并产生以下输出:
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
Hello World! How are you?
此外,是否有更好的方法来实现具有类型安全性的通用容器?
评论者认为这个概念是可行的,因此我会考虑回答这个问题。
更多信息,您可以查看以下链接:
- pseudo-generics in C
- simulation of templates in C
谢谢。