[utils] Removing kvec and using similar implementation with pointers that can be passed around
This commit is contained in:
114
src/vector.h
114
src/vector.h
@@ -1,66 +1,70 @@
|
|||||||
#ifndef VECTOR_H
|
#ifndef VECTOR_H
|
||||||
#define VECTOR_H
|
#define VECTOR_H
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "klib/kvec.h"
|
|
||||||
|
|
||||||
#define DEFAULT_VECTOR_SIZE 8
|
#define DEFAULT_VECTOR_SIZE 8
|
||||||
|
|
||||||
// Wrapper around kvec.h to provide dynamically allocated vectors
|
// Based kvec.h, dynamic vectors of any type
|
||||||
#define __VECTOR_BASE(name, type) typedef kvec_t(type) name; \
|
#define __VECTOR_BASE(name, type) typedef struct { size_t n, m; type *a; } name; \
|
||||||
static inline name *name##_new(void) { \
|
static inline name *name##_new_size(size_t size) { \
|
||||||
name *array = malloc(sizeof(name)); \
|
name *array = malloc(sizeof(name)); \
|
||||||
if (array == NULL) return NULL; \
|
if (array == NULL) return NULL; \
|
||||||
kv_init(*array); \
|
array->n = array->m = 0; \
|
||||||
kv_resize(type, *array, DEFAULT_VECTOR_SIZE); \
|
array->a = NULL; \
|
||||||
return array; \
|
array->a = malloc(size * sizeof(type)); \
|
||||||
} \
|
if (array->a == NULL) return NULL; \
|
||||||
static inline name *name##_new_size(size_t size) { \
|
array->m = size; \
|
||||||
name *array = name##_new(); \
|
return array; \
|
||||||
if (size > DEFAULT_VECTOR_SIZE) { \
|
} \
|
||||||
kv_resize(type, *array, size); \
|
static inline name *name##_new(void) { \
|
||||||
} \
|
return name##_new_size(DEFAULT_VECTOR_SIZE); \
|
||||||
return array; \
|
} \
|
||||||
} \
|
static inline void name##_resize(name *array, size_t size) { \
|
||||||
static inline void name##_push(name *array, type value) { \
|
if (size <= array->m) return; \
|
||||||
kv_push(type, *array, value); \
|
type *ptr = realloc(array->a, sizeof(type) * size); \
|
||||||
} \
|
if (ptr == NULL) return; \
|
||||||
static inline void name##_extend(name *array, name *other) { \
|
array->a = ptr; \
|
||||||
for (int i = 0; i < other->n; i++) { \
|
array->m = size; \
|
||||||
kv_push(type, *array, *(other->a + i)); \
|
} \
|
||||||
} \
|
static inline void name##_push(name *array, type value) { \
|
||||||
} \
|
if (array->n == array->m) { \
|
||||||
static inline type name##_pop(name *array) { \
|
size_t size = array->m ? array->m << 1 : 2; \
|
||||||
return kv_pop(*array); \
|
type *ptr = realloc(array->a, sizeof(type) * size); \
|
||||||
} \
|
if (ptr == NULL) return; \
|
||||||
static inline void name##_clear(name *array) { \
|
array->a = ptr; \
|
||||||
kv_clear(*array); \
|
array->m = size; \
|
||||||
} \
|
} \
|
||||||
static inline void name##_resize(name *array, size_t size) { \
|
array->a[array->n++] = value; \
|
||||||
kv_resize(type, *array, size); \
|
} \
|
||||||
} \
|
static inline void name##_extend(name *array, name *other) { \
|
||||||
\
|
size_t new_size = array->n + other->n; \
|
||||||
static inline void name##_copy(name *dst, name *src, size_t n) { \
|
if (new_size >= array->m) name##_resize(array, new_size); \
|
||||||
if (dst->m < n) name##_resize(dst, n); \
|
memcpy(array->a + array->n, other->a, other->n * sizeof(type)); \
|
||||||
memcpy(dst->a, src->a, n * sizeof(type)); \
|
array->n = new_size; \
|
||||||
dst->n = n; \
|
} \
|
||||||
} \
|
static inline type name##_pop(name *array) { \
|
||||||
\
|
return array->a[--array->n]; \
|
||||||
static inline name *name##_new_copy(name *vector, size_t n) { \
|
} \
|
||||||
name *cpy = name##_new_size(n); \
|
static inline void name##_clear(name *array) { \
|
||||||
name##_copy(cpy, vector, n); \
|
array->n = 0; \
|
||||||
return cpy; \
|
} \
|
||||||
} \
|
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) { \
|
||||||
if (array == NULL) return; \
|
if (array == NULL) return; \
|
||||||
kv_destroy(*array); \
|
if (array->a != NULL) free(array->a); \
|
||||||
free(array); \
|
free(array); \
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,10 +72,12 @@
|
|||||||
#define __VECTOR_DESTROY_FREE_DATA(name, type, free_func) \
|
#define __VECTOR_DESTROY_FREE_DATA(name, type, free_func) \
|
||||||
static inline void name##_destroy(name *array) { \
|
static inline void name##_destroy(name *array) { \
|
||||||
if (array == NULL) return; \
|
if (array == NULL) return; \
|
||||||
for (int i = 0; i < array->n; i++) { \
|
if (array->a != NULL) { \
|
||||||
free_func(array->a[i]); \
|
for (int i = 0; i < array->n; i++) { \
|
||||||
|
free_func(array->a[i]); \
|
||||||
|
} \
|
||||||
} \
|
} \
|
||||||
kv_destroy(*array); \
|
free(array->a); \
|
||||||
free(array); \
|
free(array); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user