-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.h
45 lines (35 loc) · 1.58 KB
/
hashmap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdlib.h>
#include <string.h>
#ifndef HASHMAP
#define HASHMAP
// Hash Map Callbacks
typedef void* (*ResolveCollisionCallback)(void* old_data, void* new_data);
typedef void (*DestroyDataCallback)(void* data);
// Hash Map
typedef struct HashMap HashMap;
HashMap* create_hashmap(size_t key_space);
void insert_data(HashMap* hm, const char* key, void* data, ResolveCollisionCallback resolve_collision);
void* get_data(HashMap* hm, const char* key);
void iterate(HashMap* hm, void (*callback)(const char* key, void* data));
void remove_data(HashMap* hm, const char* key, DestroyDataCallback destroy_data);
void delete_hashmap(HashMap* hm, DestroyDataCallback destroy_data);
// Conversion
//LinkedList hm_to_linkedlist();
//void** hm_to_array(HashMap* hm);
// Bucket
typedef struct Bucket Bucket;
int get_bucket_index(HashMap* hm, const char* key);
Bucket* get_bucket_list(HashMap* hm, const char* key);
Bucket* get_bucket_prev(HashMap* hm, Bucket* bucket);
Bucket* get_bucket(HashMap* hm, const char* key);
void add_bucket(HashMap* hm, Bucket* bucket);
void remove_bucket(HashMap* hm, Bucket* bucket, DestroyDataCallback destroy_data);
void remove_buckets(HashMap* hm, Bucket* bucket, DestroyDataCallback destroy_data);
void destroy_bucket(Bucket*, DestroyDataCallback destroy_data);
// Hash Function
typedef unsigned int (*HashFunction)(const char* key);
void set_hash_function(HashMap* hm, HashFunction hash_function);
unsigned int hash(const char* key);
// Since the library function is not included by the compiler
char *strdup (const char *s);
#endif