[utils] util headers
This commit is contained in:
42
src/collections.h
Normal file
42
src/collections.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#ifndef COLLECTIONS_H
|
||||||
|
#define COLLECTIONS_H
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "klib/khash.h"
|
||||||
|
#include "vector.h"
|
||||||
|
|
||||||
|
// Init collections used in multiple places
|
||||||
|
|
||||||
|
// Maps
|
||||||
|
|
||||||
|
KHASH_MAP_INIT_INT(int_int, uint32_t)
|
||||||
|
|
||||||
|
#define kh_char_hash_func(key) (uint32_t)(key)
|
||||||
|
#define kh_char_hash_equal(a, b) ((a) == (b))
|
||||||
|
|
||||||
|
KHASH_INIT(char_int, char, uint32_t, 1, kh_char_hash_func, kh_char_hash_equal)
|
||||||
|
KHASH_INIT(uchar_int, unsigned char, uint32_t, 1, kh_char_hash_func, kh_char_hash_equal)
|
||||||
|
|
||||||
|
KHASH_MAP_INIT_STR(str_int, uint32_t)
|
||||||
|
KHASH_MAP_INIT_INT(int_str, char *)
|
||||||
|
KHASH_MAP_INIT_STR(str_str, char *)
|
||||||
|
|
||||||
|
// Vectors
|
||||||
|
|
||||||
|
VECTOR_INIT(int32_array, int32_t)
|
||||||
|
VECTOR_INIT(uint32_array, uint32_t)
|
||||||
|
VECTOR_INIT(char_array, char)
|
||||||
|
VECTOR_INIT(uchar_array, unsigned char)
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
66
src/vector.h
Normal file
66
src/vector.h
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
#ifndef VECTOR_H
|
||||||
|
#define VECTOR_H
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#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##_clear(name *array) { \
|
||||||
|
kv_clear(*array); \
|
||||||
|
} \
|
||||||
|
static inline void name##_resize(name *array, size_t size) { \
|
||||||
|
kv_resize(type, *array, size); \
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define __VECTOR_DESTROY(name, type) \
|
||||||
|
static inline void name##_destroy(name *array) { \
|
||||||
|
if (array == NULL) return; \
|
||||||
|
kv_destroy(*array); \
|
||||||
|
free(array); \
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define __VECTOR_DESTROY_FREE_DATA(name, type, free_func) \
|
||||||
|
static inline void name##_destroy(name *array) { \
|
||||||
|
if (array == NULL) return; \
|
||||||
|
for (int i = 0; i < array->n; i++) { \
|
||||||
|
free_func(array->a[i]); \
|
||||||
|
} \
|
||||||
|
kv_destroy(*array); \
|
||||||
|
free(array); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define VECTOR_INIT(name, type) \
|
||||||
|
__VECTOR_BASE(name, type) \
|
||||||
|
__VECTOR_DESTROY(name, type)
|
||||||
|
|
||||||
|
#define VECTOR_INIT_FREE_DATA(name, type, free_func) \
|
||||||
|
__VECTOR_BASE(name, type) \
|
||||||
|
__VECTOR_DESTROY_FREE_DATA(name, type, free_func)
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user