forked from bangoc/cgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtvector.h
95 lines (78 loc) · 2.3 KB
/
gtvector.h
1
2
3
4
5
6
7
8
9
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
/*
(C) 2021 Nguyen Ba Ngoc (bangoc)
*/
#ifndef GT_VECTOR_H_
#define GT_VECTOR_H_
#include "gtype.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
typedef gtype *vector_t;
// ========== Khai báo hàm ===============
static vector_t gtv_create();
static size_t gtv_capacity(vector_t const v);
static size_t gtv_size(vector_t const v);
static int gtv_grow(vector_t *v, size_t newcapacity);
static int gtv_push_back(vector_t *v, gtype value);
static void gtv_free(vector_t *v);
static size_t gtv_setsize(vector_t const v, size_t newsize);
static size_t gtv_set_value_with_index(vector_t *v, gtype value, long idx);
// ========== Macro viết nhanh ===========
#define gtv_val_at(vec, i, typname) ((typname)vec[i].v)
#define gtv_ref_at(vec, i, typname) ((typname)&vec[i].v)
// ========== Định nghĩa hàm =============
static vector_t gtv_create() {
return (vector_t)&((size_t*)calloc(2, sizeof(size_t)))[2];
}
static size_t gtv_capacity(vector_t const v) {
return ((v) ? ((size_t *)(v))[-1] : (size_t)0);
}
static size_t gtv_size(vector_t const v) {
return ((v) ? ((size_t *)(v))[-2] : (size_t)0);
}
static int gtv_grow(vector_t *v, size_t newcapacity) {
size_t capacity = gtv_capacity(*v);
if (capacity < newcapacity) {
const size_t __sz = newcapacity * sizeof(gtype) + 2 * sizeof(size_t);
size_t *__p1 = &((size_t *)(*v))[-2];
size_t *__p2 = (size_t *)realloc(__p1, (__sz));
assert(__p2);
if (!__p2) {
return 1;
}
(*v) = (vector_t)(&__p2[2]);
__p2[1] = newcapacity;
}
return 0;
}
static int gtv_push_back(vector_t *v, gtype value) {
size_t __cap = gtv_capacity(*v);
if (__cap <= gtv_size(*v)) {
gtv_grow(v, !__cap ? __cap + 1 : __cap * 2);
}
vector_t vec = *v;
vec[gtv_size(vec)] = value;
((size_t*)vec)[-2] += 1;
return 0;
}
static void gtv_free(vector_t *v) {
void *p = &(((size_t*)(*v))[-2]);
free(p);
*v = NULL;
}
static size_t gtv_setsize(vector_t const v, size_t newsize) {
size_t capacity = gtv_capacity(v);
if (newsize <= capacity && v) {
((size_t *)(v))[-2] = (newsize);
return newsize;
}
return 0;
}
static size_t gtv_set_value_with_index(vector_t *v, gtype value, long idx) {
if (idx >= gtv_capacity(*v)) {
gtv_grow(v, 2 * idx + 1);
}
(*v)[idx] = value;
return gtv_capacity(*v);
}
#endif // GT_VECTOR_H_