SayoriOS 0.3.3

vector.c
1 // libvector - simple dynamic array (vector) library for C by NDRAEY.
2
3 #include "portability.h"
4 #include "lib/stdlib.h"
5 #include "lib/string.h"
6 #include "../include/vector.h"
7
8 vector_t* vector_new() {
9 vector_t* vec = calloc(1, sizeof *vec);
10
11 if(!vec) {
12 return 0;
13 }
14
15 vec->data = calloc(1, sizeof(size_t));
16
17 if(!vec->data) {
18 free(vec);
19
20 return 0;
21 }
22
23 vec->size = 0;
24 vec->capacity = 1;
25
26 return vec;
27 }
28
29 void vector_resize(vector_t* vec, size_t size) {
30 if(!vec)
31 return;
32
33 void* new_buf = realloc(vec->data, sizeof(size_t) * size);
34
35 if(!new_buf)
36 return;
37
38 vec->data = new_buf;
39 vec->capacity = size;
40 vec->size = size;
41 }
42
43 void vector_push_back(vector_t* vec, size_t element) {
44 if(vec->size >= vec->capacity) {
45
46 size_t new_cap = vec->capacity + ((vec->capacity + 1) / 2);
47
48 void* new_buf = realloc(vec->data, sizeof(size_t) * new_cap);
49
50 if(!new_buf)
51 return;
52
53 vec->data = new_buf;
54 vec->capacity = new_cap;
55 }
56
57 vec->data[vec->size++] = element;
58 }
59
60 vector_result_t vector_pop_back(vector_t* vec) {
61 if(!vec)
62 return VEC_ERR;
63
64 if(vec->size == 0)
65 return VEC_ERR;
66
67 return (vector_result_t){false, vec->data[(vec->size--) - 1]};
68 }
69
70 vector_result_t vector_get(vector_t* vec, size_t index) {
71 if(!vec || index >= vec->size)
72 return VEC_ERR;
73
74 return VEC_OK(vec->data[index]);
75 }
76
77 void vector_erase_all(vector_t* vec) {
78 if(!vec)
79 return;
80
81 free(vec->data);
82
83 vec->data = calloc(1, sizeof(size_t));
84 vec->size = 0;
85 vec->capacity = 1;
86 }
87
88 vector_result_t vector_erase_nth(vector_t* vec, size_t n) {
89 if(!vec)
90 return VEC_ERR;
91
92 if(n >= vec->size)
93 return VEC_ERR;
94
95 size_t val = vec->data[n];
96
97 for(size_t i = n; i < vec->size - 1; i++) {
98 vec->data[i] = vec->data[i + 1];
99 }
100
101 vec->size--;
102
103 return VEC_OK(val);
104 }
105
106 vector_t* vector_clone(vector_t* original) {
107 if(!original)
108 return 0;
109
110 vector_t* new_vec = calloc(1, sizeof *new_vec);
111
112 new_vec->data = calloc(original->capacity, sizeof(size_t));
113
114 memcpy(new_vec->data, original->data, sizeof(size_t) * original->capacity);
115
116 new_vec->size = original->size;
117 new_vec->capacity = original->capacity;
118
119 return new_vec;
120 }
121
122 void vector_shrink_fit(vector_t* original) {
123 if(!original)
124 return;
125
126 void* data = realloc(original->data, original->size);
127
128 if(!data)
129 return;
130
131 original->data = data;
132 original->capacity = original->size;
133 }
134
135 void vector_swap(vector_t* vec, size_t first, size_t second) {
136 if(!vec)
137 return;
138
139 if(first >= vec->size || second >= vec->size)
140 return;
141
142
143 size_t b = vec->data[second];
144 vec->data[second] = vec->data[first];
145 vec->data[first] = b;
146 }
147
148
149
150 void vector_insert(vector_t* vec, size_t index, size_t value) {
151 if(!vec)
152 return;
153
154 if(index >= vec->size)
155 return;
156
157 vector_resize(vec, vec->size + 1);
158
159 for(size_t i = vec->size - 1; i > index; i--) {
160 vec->data[i] = vec->data[i - 1];
161 }
162
163 vec->data[index] = value;
164 }
165
166 void vector_destroy(vector_t* vec) {
167 if(!vec)
168 return;
169
170 free(vec->data);
171
172 free(vec);
173 }
memcpy
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173
vector_result
Definition: vector.h:14
vector
Definition: vector.h:7