1 #ifndef MBA_HASHMAP_H
2 #define MBA_HASHMAP_H
3 
4 /* hashmap - a rehashing hash map
5  */
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 #ifndef LIBMBA_API
12 #ifdef WIN32
13 # ifdef LIBMBA_EXPORTS
14 #  define LIBMBA_API  __declspec(dllexport)
15 # else /* LIBMBA_EXPORTS */
16 #  define LIBMBA_API  __declspec(dllimport)
17 # endif /* LIBMBA_EXPORTS */
18 #else /* WIN32 */
19 # define LIBMBA_API extern
20 #endif /* WIN32 */
21 #endif /* LIBMBA_API */
22 
23 #include <mba/iterator.h>
24 #include <mba/allocator.h>
25 
26 #if USE_WCHAR
27 #define hash_text hash_wcs
28 #define cmp_text cmp_wcs
29 #else
30 #define hash_text hash_str
31 #define cmp_text cmp_str
32 #endif
33 
34 typedef unsigned long (*hash_fn)(const void *object, void *context);
35 typedef int (*cmp_fn)(const void *object1, const void *object2, void *context);
36 
37 extern const int table_sizes[];
38 
39 struct entry;
40 
41 struct hashmap {
42 	int table_size_index;
43 	ref_t hash;
44 	ref_t cmp;
45 	ref_t context;
46 	unsigned int size;
47 	unsigned int load_factor_high;
48 	unsigned int load_factor_low;
49 	ptrdiff_t al;
50 	ref_t table;
51 };
52 
53 LIBMBA_API unsigned long hash_str(const void *str, void *context);
54 LIBMBA_API unsigned long hash_wcs(const void *wcs, void *context);
55 LIBMBA_API int cmp_str(const void *object1, const void *object2, void *context);
56 LIBMBA_API int cmp_wcs(const void *object1, const void *object2, void *context);
57 
58 LIBMBA_API int hashmap_init(struct hashmap *h,
59 		unsigned int load_factor,
60 		hash_fn hash,
61 		cmp_fn cmp,
62 		void *context,
63 		struct allocator *al);
64 
65 LIBMBA_API int hashmap_deinit(struct hashmap *h, del_fn key_del, del_fn data_del, void *context);
66 LIBMBA_API struct hashmap *hashmap_new(hash_fn hash, cmp_fn cmp, void *context, struct allocator *al);
67 LIBMBA_API int hashmap_del(struct hashmap *h, del_fn key_del, del_fn data_del, void *context);
68 LIBMBA_API int hashmap_clear(struct hashmap *h, del_fn key_del, del_fn data_del, void *context);
69 LIBMBA_API int hashmap_clean(struct hashmap *h);
70 
71 LIBMBA_API int hashmap_put(struct hashmap *h, void *key, void *data);
72 LIBMBA_API int hashmap_is_empty(struct hashmap *h);
73 LIBMBA_API unsigned int hashmap_size(struct hashmap *h);
74 LIBMBA_API void *hashmap_get(const struct hashmap *h, const void *key);
75 LIBMBA_API void hashmap_iterate(void *h, iter_t *iter);
76 LIBMBA_API void *hashmap_next(void *h, iter_t *iter);
77 LIBMBA_API int hashmap_remove(struct hashmap *h, void **key, void **data);
78 
79 #ifdef __cplusplus
80 }
81 #endif
82 
83 #endif /* MBA_HASHMAP_H */
84 
85