//
// Created by Ht on 2021/2/6.
//

#ifndef CMDNS_CM_HASHTABLE_H
#define CMDNS_CM_HASHTABLE_H

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "cmdns_hal_intf.h"
/**
 * 需要改造，添加指针、遍历等工具
 */

#ifdef __cplusplus
extern "C"
{
#endif

#define CMHASHSIZE 101

typedef struct _node{
    char *name;
    void *payload;
    struct _node *next;
} CmHashNode;

typedef struct _hashtable {
    unsigned char  auto_release_;
    unsigned int   size_;
    CmHashNode     **table_;
} CmHashTable;

JE_API CmHashTable* initCmHashtable(CmHashTable** table);

JE_API CmHashTable* initCmHashtableEx(CmHashTable** table, unsigned char autoRelease);

JE_API unsigned int hashValueCm(CmHashTable* table, char *key);

JE_API CmHashNode* cmLookup(CmHashTable* table, char *key);

JE_API char* strdupCm(const char *s);

JE_API void* removeCmHashtable(CmHashTable* table, char* name);

JE_API void* getCmHashtable(CmHashTable* table, char* name);

JE_API void* putCmHashtable(CmHashTable* table, char* name, void* data);

/* A pretty useless but good debugging function,
which simply displays the hashtable in (key.value) pairs
*/

typedef CMBool (*NodeAccesser)(const char* key, void* node);
typedef CMBool (*NodeIdxAccesser)(unsigned int idx, const char* key, void* node);

JE_API void enumerateOnTable(CmHashTable* table, NodeAccesser accesser);
JE_API void enumerateOnTableEx(CmHashTable* table, NodeIdxAccesser accesser);

JE_API void releaseHashtable(CmHashTable* table);

JE_API void cleanHashTable(CmHashTable* table);


#ifdef DEBUG
CMBool __cmPrintTable(unsigned int idx, const char* key, void* node);
#define PrintCmHashtable(table) enumerateOnTableEx((table), __cmPrintTable)
#else
#define PrintCmHashtable(table)
#endif

#ifdef __cplusplus
}
#endif

#endif //CMDNS_CM_HASHTABLE_H
