/* This software is licensed by the MIT License, see LICENSE file */ /* Copyright © 2022 Gregory Lirent */ #include #include #include #include "bits/cstring.h" #include "bits/memory.h" #ifndef LIBCDSB_VTYPE_H #define LIBCDSB_VTYPE_H typedef enum libcdsb_value_types { VTYPE_POINTER = 0, VTYPE_BOOLEAN = 1, VTYPE_UINT8 = 2, VTYPE_UINT16 = 3, VTYPE_UINT32 = 4, VTYPE_UINT64 = 5, VTYPE_INT8 = 6, VTYPE_INT16 = 7, VTYPE_INT32 = 8, VTYPE_INT64 = 9, VTYPE_FLOAT = 10, VTYPE_DOUBLE = 11, VTYPE_LDOUBLE = 12, VTYPE_STRING = 13, VTYPE_MAP = 14, VTYPE_ARRAY = 15, VTYPE_LIST = 16, VTYPE_SET = 17, VTYPE_DICT = 18, } vtype; /*#####################################################################################################################*/ struct libcdsb_string { char* buffer; }; struct libcdsb_array { void* mem; size_t size; vtype type; }; struct libcdsb_map { struct libcdsb_map_node* root; vtype type; }; struct libcdsb_set { struct libcdsb_rbtree_node* root; vtype type; }; struct libcdsb_list { struct libcdsb_list_node* last; struct libcdsb_list_node* first; }; struct libcdsb_dict { struct libcdsb_dict_node** nodes; size_t capacity; size_t size; }; /*#####################################################################################################################*/ typedef void* vtype_pointer; typedef bool vtype_bool; typedef uint_least8_t vtype_uint8; typedef uint_least16_t vtype_uint16; typedef uint_least32_t vtype_uint32; typedef uint_least64_t vtype_uint64; typedef int_least8_t vtype_int8; typedef int_least16_t vtype_int16; typedef int_least32_t vtype_int32; typedef int_least64_t vtype_int64; typedef float vtype_float; typedef double vtype_double; typedef long double vtype_ldouble; typedef size_t vtype_hash; typedef struct libcdsb_array vtype_array; typedef struct libcdsb_map vtype_map; typedef struct libcdsb_set vtype_set; typedef struct libcdsb_list vtype_list; typedef struct libcdsb_dict vtype_dict; typedef struct libcdsb_string vtype_string; extern const char* libcdsb_vtype_name (vtype t) Warn_unused_result__; extern const char* libcdsb_vtype_stringify(const void* value, vtype t) Warn_unused_result__; /*#####################################################################################################################*/ extern size_t string_size (const vtype_string* x) Pure__ Warn_unused_result__ Nonnull__(1); extern size_t array_nmemb (const vtype_array* x) Pure__ Warn_unused_result__ Nonnull__(1); extern size_t list_size (const vtype_list* x) Pure__ Warn_unused_result__ Nonnull__(1); extern size_t map_size (const vtype_map* x) Pure__ Warn_unused_result__ Nonnull__(1); extern size_t vset_size (const vtype_set* x) Pure__ Warn_unused_result__ Nonnull__(1); inline size_t string_nmemb (const vtype_string* x) Always_inline__ Pure__ Warn_unused_result__ Nonnull__(1); inline size_t array_size (const vtype_array* x) Always_inline__ Pure__ Warn_unused_result__ Nonnull__(1); inline size_t dict_size (const vtype_dict* x) Always_inline__ Pure__ Warn_unused_result__ Nonnull__(1); inline size_t dict_capacity(const vtype_dict* x) Always_inline__ Pure__ Warn_unused_result__ Nonnull__(1); extern int string_case_compare(const vtype_string* s0, const vtype_string* s1) Pure__ Warn_unused_result__ Nonnull__(1,2); extern int string_compare (const vtype_string* s0, const vtype_string* s1) Pure__ Warn_unused_result__ Nonnull__(1,2); extern int array_compare (const vtype_array* s0, const vtype_array* s1) Pure__ Warn_unused_result__ Nonnull__(1,2); extern int list_compare (const vtype_list* s0, const vtype_list* s1) Pure__ Warn_unused_result__ Nonnull__(1,2); extern int map_compare (const vtype_map* s0, const vtype_map* s1) Pure__ Warn_unused_result__ Nonnull__(1,2); extern int vset_compare (const vtype_set* s0, const vtype_set* s1) Pure__ Warn_unused_result__ Nonnull__(1,2); extern int dict_compare (const vtype_dict* s0, const vtype_dict* s1) Pure__ Warn_unused_result__ Nonnull__(1,2); inline vtype_string string_copy (const vtype_string* s) Always_inline__ Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_array array_copy (const vtype_array* s) Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_list list_copy (const vtype_list* s) Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_map map_copy (const vtype_map* s) Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_set vset_copy (const vtype_set* s) Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_dict dict_copy (const vtype_dict* s) Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_list dict_copy_keys(const vtype_dict* s) Pure__ Warn_unused_result__ Nonnull__(1); #define map_copy_keys(s) vset_copy(_LIBCDSB_nothing(s)) inline vtype_string* string_duplicate (const vtype_string* s) Always_inline__ Warn_unused_result__ Nonnull__(1); extern vtype_array* array_duplicate (const vtype_array* s) Warn_unused_result__ Nonnull__(1); extern vtype_list* list_duplicate (const vtype_list* s) Warn_unused_result__ Nonnull__(1); extern vtype_map* map_duplicate (const vtype_map* s) Warn_unused_result__ Nonnull__(1); extern vtype_set* vset_duplicate (const vtype_set* s) Warn_unused_result__ Nonnull__(1); extern vtype_dict* dict_duplicate (const vtype_dict* s) Warn_unused_result__ Nonnull__(1); extern vtype_list* dict_duplicate_keys(const vtype_dict* s) Warn_unused_result__ Nonnull__(1); #define map_duplicate_keys(s) vset_duplicate(_LIBCDSB_nothing(s)) inline void string_copy_init (vtype_string* x, const vtype_string* s) Always_inline__ Nonnull__(1,2); extern void array_copy_init (vtype_array* x, const vtype_array* s) Nonnull__(1,2); extern void list_copy_init (vtype_list* x, const vtype_list* s) Nonnull__(1,2); extern void map_copy_init (vtype_map* x, const vtype_map* s) Nonnull__(1,2); extern void vset_copy_init (vtype_set* x, const vtype_set* s) Nonnull__(1,2); extern void dict_copy_init (vtype_dict* x, const vtype_dict* s) Nonnull__(1,2); extern void dict_init_keys (vtype_list* x, const vtype_dict* s) Nonnull__(1,2); #define map_copy_init_keys(x, s) vset_copy_init(x, _LIBCDSB_nothing(s)) inline void string_free(vtype_string* x) Always_inline__; extern void array_free (vtype_array* x); extern void list_free (vtype_list* x); extern void map_free (vtype_map* x); extern void vset_free (vtype_set* x); extern void dict_free (vtype_dict* x); extern vtype_hash string_hash(const vtype_string* s) Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_hash array_hash (const vtype_array* s) Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_hash list_hash (const vtype_list* s) Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_hash map_hash (const vtype_map* s) Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_hash vset_hash (const vtype_set* s) Pure__ Warn_unused_result__ Nonnull__(1); extern vtype_hash dict_hash (const vtype_dict* s) Pure__ Warn_unused_result__ Nonnull__(1); /*#####################################################################################################################*/ inline size_t array_size (const vtype_array* x) { return x->size; } inline size_t dict_size (const vtype_dict* x) { return x->size; } inline size_t dict_capacity(const vtype_dict* x) { return x->capacity; } inline size_t string_nmemb (const vtype_string* x) { return (x->buffer) ? libcdsb_strlen(x->buffer) : 0; } inline void string_free ( vtype_string* x) { if (x) { libcdsb_free(x->buffer); x->buffer = 0; } } inline vtype_string string_copy(const vtype_string* s) { vtype_string x = { .buffer = libcdsb_strdup(s->buffer) }; return x; } inline vtype_string* string_duplicate(const vtype_string* s) { void** x = libcdsb_malloc(sizeof(*x)); *x = libcdsb_strdup(s->buffer); return (void*)x; } inline void string_copy_init(vtype_string* x, const vtype_string* s) { x->buffer = libcdsb_strdup(s->buffer); } #endif /* LIBCDSB_VTYPE_H */