From 44c32b82b0c3a04740f90282bd8fcec95cfb0f8e Mon Sep 17 00:00:00 2001 From: Marrub Date: Sun, 23 Oct 2016 21:22:44 -0400 Subject: [PATCH] HashMap: Make Lth_HashMapFind generic Manifest: Fix Lth_Resource not handling char * --- lithos_c/inc/Lth.h | 1 + lithos_c/inc/Lth_hashmap.h | 52 ++++++++++++++++++++++++++++++++----- lithos_c/inc/Lth_manifest.h | 4 +-- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/lithos_c/inc/Lth.h b/lithos_c/inc/Lth.h index b5117a6..bb81fe1 100644 --- a/lithos_c/inc/Lth.h +++ b/lithos_c/inc/Lth.h @@ -17,6 +17,7 @@ #include #include #include +#include #include diff --git a/lithos_c/inc/Lth_hashmap.h b/lithos_c/inc/Lth_hashmap.h index a3cc017..93406d2 100644 --- a/lithos_c/inc/Lth_hashmap.h +++ b/lithos_c/inc/Lth_hashmap.h @@ -16,6 +16,22 @@ #ifndef lithos3__Lth_hashmap_h #define lithos3__Lth_hashmap_h +#define Lth_GenFind(hashexpr) \ + Lth_HashMapElem *elem = Lth_HashFindItr(map, (hashexpr)); \ + if(elem == NULL) return NULL; \ + return elem->value + +#define Lth_HashMapFind(map, expr) \ + _Generic((expr)+0, \ + __str: Lth_HashMapFind_str, \ + char *: Lth_HashMapFind_char, \ + char const *: Lth_HashMapFind_char, \ + wchar_t *: Lth_HashMapFind_wchar, \ + wchar_t const *: Lth_HashMapFind_wchar, \ + size_t: Lth_HashMapFind_hash, \ + wchar_t: Lth_HashMapFind_hash, \ + int: Lth_HashMapFind_hash)((map), (expr)) + // Type Definitions ----------------------------------------------------------| @@ -36,7 +52,7 @@ typedef struct Lth_HashMapElem // table // // read-only -// elements +// elem // typedef struct Lth_HashMap { @@ -118,13 +134,37 @@ static inline Lth_HashMapElem *Lth_HashFindItr(Lth_HashMap *map, size_t hash) } // -// Lth_HashMapFind +// Lth_HashMapFind_char // -static inline void *Lth_HashMapFind(Lth_HashMap *map, char const *key) +static inline void *Lth_HashMapFind_char(Lth_HashMap *map, char const *key) { - Lth_HashMapElem *elem = Lth_HashFindItr(map, Lth_Hash_char(key)); - if(elem == NULL) return NULL; - return elem->value; + Lth_GenFind(Lth_Hash_char(key)); } +// +// Lth_HashMapFind_str +// +static inline void *Lth_HashMapFind_str(Lth_HashMap *map, __str key) +{ + Lth_GenFind(Lth_Hash_str(key)); +} + +// +// Lth_HashMapFind_wchar +// +static inline void *Lth_HashMapFind_wchar(Lth_HashMap *map, wchar_t const *key) +{ + Lth_GenFind(Lth_Hash_wchar(key)); +} + +// +// Lth_HashMapFind_hash +// +static inline void *Lth_HashMapFind_hash(Lth_HashMap *map, size_t key) +{ + Lth_GenFind(key); +} + +#undef Lth_GenFind + #endif//lithos3__Lth_hashmap_h diff --git a/lithos_c/inc/Lth_manifest.h b/lithos_c/inc/Lth_manifest.h index 9379c14..f70c334 100644 --- a/lithos_c/inc/Lth_manifest.h +++ b/lithos_c/inc/Lth_manifest.h @@ -23,8 +23,8 @@ _Generic((val)+0, \ int: Lth_ManifestNew_Integ, \ _Accum: Lth_ManifestNew_Fixed, \ - char const *: Lth_ManifestNew_Strng \ - )((name), (val)) + char *: Lth_ManifestNew_Strng, \ + char const *: Lth_ManifestNew_Strng)((name), (val)) // Type Definitions ----------------------------------------------------------|