| 1 |
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
| 2 |
/*
|
| 3 |
* hash_dict
|
| 4 |
* - hash-map based dict feature
|
| 5 |
*
|
| 6 |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
|
| 7 |
*/
|
| 8 |
|
| 9 |
#ifndef _HASH_DICT_H_
|
| 10 |
#define _HASH_DICT_H_
|
| 11 |
|
| 12 |
#include "memory_pool.h"
|
| 13 |
#include <stdint.h>
|
| 14 |
|
| 15 |
enum hash_dict_constant_t
|
| 16 |
{
|
| 17 |
HASH_DICT_BUCKET_SIZE = 65536,
|
| 18 |
HASH_DICT_BUCKET_MAX_COUNT = 1024,
|
| 19 |
};
|
| 20 |
|
| 21 |
struct hash_item_t
|
| 22 |
{
|
| 23 |
uint64_t key;
|
| 24 |
int64_t value;
|
| 25 |
struct hash_item_t *p_next;
|
| 26 |
};
|
| 27 |
typedef struct hash_item_t HASH_ITEM;
|
| 28 |
|
| 29 |
struct hash_dict_t
|
| 30 |
{
|
| 31 |
HASH_ITEM **buckets[HASH_DICT_BUCKET_MAX_COUNT];
|
| 32 |
unsigned int bucket_count;
|
| 33 |
unsigned int prime_index;
|
| 34 |
MEMORY_POOL *p_item_pool;
|
| 35 |
unsigned int item_count;
|
| 36 |
};
|
| 37 |
typedef struct hash_dict_t HASH_DICT;
|
| 38 |
|
| 39 |
extern HASH_DICT *hash_dict_create(int item_count_limit);
|
| 40 |
extern void hash_dict_destroy(HASH_DICT *p_dict);
|
| 41 |
|
| 42 |
// Return 1 if existing key updated, 0 if new key added, -1 on error
|
| 43 |
extern int hash_dict_set(HASH_DICT *p_dict, uint64_t key, int64_t value);
|
| 44 |
|
| 45 |
// Return 1 if existing key updated, 0 if key not exist, -1 on error
|
| 46 |
extern int hash_dict_inc(HASH_DICT *p_dict, uint64_t key, int64_t value_inc);
|
| 47 |
|
| 48 |
// Return 1 if key found, 0 if key not exist, -1 on error
|
| 49 |
extern int hash_dict_get(HASH_DICT *p_dict, uint64_t key, int64_t *p_value);
|
| 50 |
|
| 51 |
// Return 1 if key deleted, 0 if key not exist, -1 on error
|
| 52 |
extern int hash_dict_del(HASH_DICT *p_dict, uint64_t key);
|
| 53 |
|
| 54 |
inline unsigned int hash_dict_item_count(HASH_DICT *p_dict)
|
| 55 |
{
|
| 56 |
if (p_dict == NULL)
|
| 57 |
{
|
| 58 |
return 0;
|
| 59 |
}
|
| 60 |
return p_dict->item_count;
|
| 61 |
}
|
| 62 |
|
| 63 |
#endif //_HASH_DICT_H_
|