[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:
Al
2015-09-06 20:46:29 -07:00
parent 49fe504201
commit 9d2ca08fc2
3 changed files with 43 additions and 43 deletions

View File

@@ -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);

View File

@@ -36,7 +36,21 @@
} \
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) { \

View File

@@ -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; \