forked from justanhduc/task-spooler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec.c
More file actions
126 lines (111 loc) · 2.56 KB
/
Copy pathvec.c
File metadata and controls
126 lines (111 loc) · 2.56 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "vec.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define VEC_INITIAL_CAPACITY 8
vec_t *vec_create(void)
{
vec_t *v = (vec_t *)malloc(sizeof(vec_t));
if (!v) return NULL;
if (vec_init(v) != 0) {
free(v);
return NULL;
}
return v;
}
int vec_init(vec_t *v)
{
if (!v) return -1;
v->data = (void **)malloc(VEC_INITIAL_CAPACITY * sizeof(void *));
if (!v->data) return -1;
v->size = 0;
v->capacity = VEC_INITIAL_CAPACITY;
return 0;
}
int vec_push(vec_t *v, void *item)
{
if (!v || !v->data) return -1;
if (v->size >= v->capacity) {
if (v->capacity > SIZE_MAX / 2) return -1;
if (vec_reserve(v, v->capacity * 2) != 0) return -1;
}
v->data[v->size++] = item;
return 0;
}
void *vec_pop(vec_t *v)
{
if (!v || !v->data || v->size == 0) return NULL;
return v->data[--v->size];
}
void *vec_get(const vec_t *v, size_t index)
{
if (!v || !v->data || index >= v->size) return NULL;
return v->data[index];
}
void *vec_set(vec_t *v, size_t index, void *item)
{
if (!v || !v->data || index >= v->size) return NULL;
void *old = v->data[index];
v->data[index] = item;
return old;
}
int vec_insert(vec_t *v, size_t index, void *item)
{
if (!v || !v->data || index > v->size) return -1;
if (v->size >= v->capacity) {
if (v->capacity > SIZE_MAX / 2) return -1;
if (vec_reserve(v, v->capacity * 2) != 0) return -1;
}
memmove(&v->data[index + 1], &v->data[index], (v->size - index) * sizeof(void *));
v->data[index] = item;
v->size++;
return 0;
}
void *vec_remove(vec_t *v, size_t index)
{
if (!v || !v->data || index >= v->size) return NULL;
void *removed = v->data[index];
memmove(&v->data[index], &v->data[index + 1], (v->size - index - 1) * sizeof(void *));
v->size--;
return removed;
}
size_t vec_size(const vec_t *v)
{
return v ? v->size : 0;
}
size_t vec_capacity(const vec_t *v)
{
return v ? v->capacity : 0;
}
int vec_reserve(vec_t *v, size_t cap)
{
if (!v || !v->data) return -1;
if (cap <= v->capacity) return 0;
void **new_data = (void **)realloc(v->data, cap * sizeof(void *));
if (!new_data) return -1;
v->data = new_data;
v->capacity = cap;
return 0;
}
void vec_clear(vec_t *v)
{
if (v && v->data) {
v->size = 0;
}
}
void vec_free(vec_t *v)
{
if (v) {
free(v->data);
free(v);
}
}
void vec_destroy(vec_t *v)
{
if (v) {
free(v->data);
v->data = NULL;
v->size = 0;
v->capacity = 0;
}
}