[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');
|
||||
}
|
||||
|
||||
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) {
|
||||
char_array_strip_nul_byte(array);
|
||||
char_array_append(array, str);
|
||||
|
||||
72
src/vector.h
72
src/vector.h
@@ -8,35 +8,49 @@
|
||||
#include "klib/kvec.h"
|
||||
|
||||
// Wrapper around kvec.h to provide dynamically allocated vectors
|
||||
#define __VECTOR_BASE(name, type) typedef kvec_t(type) name; \
|
||||
static inline name *name##_new(void) { \
|
||||
name *array = malloc(sizeof(name)); \
|
||||
if (array == NULL) return NULL; \
|
||||
kv_init(*array); \
|
||||
return array; \
|
||||
} \
|
||||
static inline name *name##_new_size(size_t size) { \
|
||||
name *array = name##_new(); \
|
||||
kv_resize(type, *array, size); \
|
||||
return array; \
|
||||
} \
|
||||
static inline void name##_push(name *array, type value) { \
|
||||
kv_push(type, *array, value); \
|
||||
} \
|
||||
static inline void name##_extend(name *array, name *other) { \
|
||||
for (int i = 0; i < other->n; i++) { \
|
||||
kv_push(type, *array, *(other->a + i)); \
|
||||
} \
|
||||
} \
|
||||
static inline type name##_pop(name *array) { \
|
||||
return kv_pop(*array); \
|
||||
} \
|
||||
static inline void name##_clear(name *array) { \
|
||||
kv_clear(*array); \
|
||||
} \
|
||||
static inline void name##_resize(name *array, size_t size) { \
|
||||
kv_resize(type, *array, size); \
|
||||
}
|
||||
#define __VECTOR_BASE(name, type) typedef kvec_t(type) name; \
|
||||
static inline name *name##_new(void) { \
|
||||
name *array = malloc(sizeof(name)); \
|
||||
if (array == NULL) return NULL; \
|
||||
kv_init(*array); \
|
||||
return array; \
|
||||
} \
|
||||
static inline name *name##_new_size(size_t size) { \
|
||||
name *array = name##_new(); \
|
||||
kv_resize(type, *array, size); \
|
||||
return array; \
|
||||
} \
|
||||
static inline void name##_push(name *array, type value) { \
|
||||
kv_push(type, *array, value); \
|
||||
} \
|
||||
static inline void name##_extend(name *array, name *other) { \
|
||||
for (int i = 0; i < other->n; i++) { \
|
||||
kv_push(type, *array, *(other->a + i)); \
|
||||
} \
|
||||
} \
|
||||
static inline type name##_pop(name *array) { \
|
||||
return kv_pop(*array); \
|
||||
} \
|
||||
static inline void name##_clear(name *array) { \
|
||||
kv_clear(*array); \
|
||||
} \
|
||||
static inline void name##_resize(name *array, size_t 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) \
|
||||
static inline void name##_destroy(name *array) { \
|
||||
|
||||
@@ -10,13 +10,6 @@
|
||||
__VECTOR_BASE(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) { \
|
||||
for (int i = 0; i < n; i++) { \
|
||||
array[i] = value; \
|
||||
|
||||
Reference in New Issue
Block a user