[utils] Adding _copy and _new_copy methods to vectors (the former copies data to a pre-allocated vector, the latter allocates a new vector)
This commit is contained in:
@@ -498,13 +498,6 @@ inline void char_array_terminate(char_array *array) {
|
|||||||
char_array_push(array, '\0');
|
char_array_push(array, '\0');
|
||||||
}
|
}
|
||||||
|
|
||||||
inline char_array *char_array_copy(char_array *array) {
|
|
||||||
char_array *copy = char_array_new_size(array->m);
|
|
||||||
memcpy(copy->a, array->a, array->n);
|
|
||||||
copy->n = array->n;
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void char_array_cat(char_array *array, char *str) {
|
inline void char_array_cat(char_array *array, char *str) {
|
||||||
char_array_strip_nul_byte(array);
|
char_array_strip_nul_byte(array);
|
||||||
char_array_append(array, str);
|
char_array_append(array, str);
|
||||||
|
|||||||
16
src/vector.h
16
src/vector.h
@@ -36,7 +36,21 @@
|
|||||||
} \
|
} \
|
||||||
static inline void name##_resize(name *array, size_t size) { \
|
static inline void name##_resize(name *array, size_t size) { \
|
||||||
kv_resize(type, *array, size); \
|
kv_resize(type, *array, size); \
|
||||||
}
|
} \
|
||||||
|
\
|
||||||
|
static inline void *name##_copy(name *dst, name *src, size_t n) { \
|
||||||
|
if (dst->m < n) name##_resize(dst, n); \
|
||||||
|
memcpy(dst->a, src->a, n * sizeof(type)); \
|
||||||
|
dst->n = n; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
static inline name *name##_new_copy(name *vector, size_t n) { \
|
||||||
|
name *cpy = name##_new_size(n); \
|
||||||
|
name##_copy(cpy, vector, n); \
|
||||||
|
return cpy; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
|
||||||
|
|
||||||
#define __VECTOR_DESTROY(name, type) \
|
#define __VECTOR_DESTROY(name, type) \
|
||||||
static inline void name##_destroy(name *array) { \
|
static inline void name##_destroy(name *array) { \
|
||||||
|
|||||||
@@ -10,13 +10,6 @@
|
|||||||
__VECTOR_BASE(name, type) \
|
__VECTOR_BASE(name, type) \
|
||||||
__VECTOR_DESTROY(name, type) \
|
__VECTOR_DESTROY(name, type) \
|
||||||
\
|
\
|
||||||
static inline name *name##_copy(name *vector, size_t n) { \
|
|
||||||
name *cpy = name##_new_size(n); \
|
|
||||||
memcpy(vector->a, cpy->a, n * sizeof(type)); \
|
|
||||||
cpy->n = n; \
|
|
||||||
return cpy; \
|
|
||||||
} \
|
|
||||||
\
|
|
||||||
static inline void type##_array_set(type *array, size_t n, type value) { \
|
static inline void type##_array_set(type *array, size_t n, type value) { \
|
||||||
for (int i = 0; i < n; i++) { \
|
for (int i = 0; i < n; i++) { \
|
||||||
array[i] = value; \
|
array[i] = value; \
|
||||||
|
|||||||
Reference in New Issue
Block a user