- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#define GEN_NAME(type) struct myvec_ ## type
#define MK_VEC_TYPE(type) GEN_NAME(type) {size_t sz; type arr[];};
#define MK_VEC_NEW(type) \
GEN_NAME(type) *myvec_new_ ## type (size_t num) \
{ \
struct myvec_ ## type *tmp = malloc(sizeof(type) * num); \
if (tmp == NULL) \
{ \
return NULL; \
} \
tmp->sz = num; \
return tmp; \
}
#define MK_VEC_DELETE(type) \
void myvec_delete_ ## type (GEN_NAME(type) *v) \
{ \
free(v); \
}
#define MK_VEC_GET(type) \
type myvec_get_ ## type (GEN_NAME(type) *v, size_t pos) \
{ \
if(pos < v->sz) \
{ \
return v->arr[pos]; \
} \
else \
{ \
exit(-1); \
} \
}
// исключения - говно
#define MK_VEC_SET(type) \
void myvec_set_ ## type (GEN_NAME(type) *v, size_t pos, type val) \
{ \
if(pos < v->sz) \
{ \
v->arr[pos] = val; \
} \
else \
{ \
exit(-1); \
} \
}
#define MK_VEC_GETSZ(type) \
size_t myvec_getsz_ ## type (GEN_NAME(type) v) \
{ \
return v.sz; \
}
#define MK_SHIT(type) \
MK_VEC_TYPE(type) \
MK_VEC_NEW(type) \
MK_VEC_DELETE(type) \
MK_VEC_GET(type) \
MK_VEC_GETSZ(type) \
MK_VEC_SET(type)
MK_SHIT(int)
MK_SHIT(float)
MK_SHIT(double)
#define test(a) _Generic(a, int : 1, GEN_NAME(int) : 2, default : 0)
#define MTD_C(val,mtd) _Generic( val,\
GEN_NAME(int): myvec_ ## mtd ##_int, \
GEN_NAME(float): myvec_ ## mtd ##_float, \
GEN_NAME(double): myvec_ ## mtd ##_double, \
default: 0) // хуй там!
#define GET(vec,pos) MTD_C(vec,get)(&vec,pos)
#define SET(vec, pos, val) MTD_C(vec,set)(&vec,pos,val)
#define GETSZ(vec) MTD_C(vec,getsz)(vec)
int main(void)
{
GEN_NAME(int) *vec1 = myvec_new_int(10);
SET(*vec1, 0, 123);
size_t size = GETSZ(*vec1);
printf("vector size is %zu\n", size);
printf("vector vec1[0] is %d\n", GET(*vec1,0));
return 0;
}